示例#1
0
def create_quote(nonce, data=None, pcrmask=EMPTYMASK):
    quote = ""
    with tempfile.NamedTemporaryFile() as quotepath:
        keyhandle = tpm_initialize.get_tpm_metadata('aik_handle')
        aik_pw = tpm_initialize.get_tpm_metadata('aik_pw')

        if pcrmask is None:
            pcrmask = EMPTYMASK

        with tpm_exec.tpmutilLock:
            if data is not None:
                # add PCR 16 to pcrmask
                pcrmask = "0x%X" % (int(pcrmask, 0) +
                                    (1 << common.TPM_DATA_PCR))
                tpm_exec.run("pcrreset -ix %d" % common.TPM_DATA_PCR,
                             lock=False)
                tpm_exec.run(
                    "extend -ix %d -ic %s" %
                    (common.TPM_DATA_PCR, hashlib.sha1(data).hexdigest()),
                    lock=False)

            command = "tpmquote -hk %s -pwdk %s -bm %s -nonce %s -noverify -oq %s" % (
                keyhandle, aik_pw, pcrmask, nonce, quotepath.name)
            (retout, code, quoteraw) = tpm_exec.run(command,
                                                    lock=False,
                                                    outputpath=quotepath.name)
            quote = base64.b64encode(quoteraw.encode("zlib"))

    return 'r' + quote
示例#2
0
def read_ekcert_nvram():
    if common.STUB_TPM:
        return common.TEST_EK_CERT
    nvpath = None
    try:
        owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')
        #make a temp file for the quote 
        nvfd,nvpath = tempfile.mkstemp()
        
        (output,code) = tpm_exec.run("nv_readvalue -pwdo %s -in 1000f000 -cert -of %s"%(owner_pw,nvpath),raiseOnError=False)
            
        if code!=tpm_exec.EXIT_SUCESS and len(output)>0 and output[0].startswith("Error Illegal index from NV_ReadValue"):
            logger.warn("No EK certificate found in TPM NVRAM")
            return None
        elif code!=tpm_exec.EXIT_SUCESS:
            raise Exception("nv_readvalue for ekcert failed with code "+str(code)+": "+str(output))
        
        # read in the cert
        f = open(nvpath,"rb")
        ekcert = f.read()
        f.close()
        os.close(nvfd)
    finally:
        if nvpath is not None:
            os.remove(nvpath)
    return base64.b64encode(ekcert)
示例#3
0
def write_key_nvram(key):
    owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')

    # write out quote
    with tempfile.NamedTemporaryFile() as keyFile:
        keyFile.write(key)
        keyFile.flush()

        tpm_exec.run(
            "nv_definespace -pwdo %s -in 1 -sz %d -pwdd %s -per 40004" %
            (owner_pw, common.BOOTSTRAP_KEY_SIZE, owner_pw))
        tpm_exec.run("nv_writevalue -pwdd %s -in 1 -if %s" %
                     (owner_pw, keyFile.name))
    return
示例#4
0
def create_deep_quote(nonce, data=None, vpcrmask=EMPTYMASK, pcrmask=EMPTYMASK):
    quote = ""
    with tempfile.NamedTemporaryFile() as quotepath:
        # read in the vTPM key handle
        keyhandle = tpm_initialize.get_tpm_metadata('aik_handle')
        owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')
        aik_pw = tpm_initialize.get_tpm_metadata('aik_pw')

        if pcrmask is None:
            pcrmask = EMPTYMASK
        if vpcrmask is None:
            vpcrmask = EMPTYMASK

        # need to hold the lock while we reset and extend the pcr and then do the quote
        with tpm_exec.tpmutilLock:
            if data is not None:
                # add PCR 16 to pcrmask
                vpcrmask = "0x%X" % (int(vpcrmask, 0) +
                                     (1 << common.TPM_DATA_PCR))
                tpm_exec.run("pcrreset -ix %d" % common.TPM_DATA_PCR,
                             lock=False)
                tpm_exec.run(
                    "extend -ix %d -ic %s" %
                    (common.TPM_DATA_PCR, hashlib.sha1(data).hexdigest()),
                    lock=False)

            command = "deepquote -vk %s -hm %s -vm %s -nonce %s -pwdo %s -pwdk %s -oq %s" % (
                keyhandle, pcrmask, vpcrmask, nonce, owner_pw, aik_pw,
                quotepath.name)
            #print("Executing %s"%(command))
            (retout, code, quoteraw) = tpm_exec.run(command,
                                                    lock=False,
                                                    outputpath=quotepath.name)
            quote = base64.b64encode(quoteraw.encode("zlib"))

    return 'd' + quote
示例#5
0
def main(argv=sys.argv):
    nonce = "NABIL"
    rsa_key = "somebase64data"

    print "creating quote..."
    quote = create_quote(nonce, rsa_key)
    print "\tDONE"

    print "checking quote..."
    if common.STUB_TPM:
        aik = '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzpAAp0TEPftgRr0z0ZYV\nBtKz3yDAYE+lH8p6gE3hRcI/Vg9ngGfQJohc9wsy4ELSSKUVMBVOkw2ITKCH3UOo\ny6J+FPApp12oYGSKxUMeHH30cVKaFSXMKSYl4J67Uufv8rhuKbcp60EJNfo8ougv\nHV1n9fwSBsmYeU8InW3cC4qcOkkQW++zeQi6HhTvrXdajTSdoH9wO8olQvx+IW4n\nmWz74vYWt5u5whyIv2wDkGlOz1x5iAAcarxS3xPuQTu/Mv9QOVNqwcvQaAolps6z\n3ckOGyRrEUS7rKkkBGX4FATUq6XhbxyJ7ZLba83jEnGS9h8EO2t9SUmp7cNxV+A7\njQIDAQAB\n-----END PUBLIC KEY-----\n'

    else:
        aik = tpm_initialize.get_tpm_metadata('aik')

    print "\tVerified %s" % check_quote(nonce, rsa_key, quote, aik)

    print "creating full quote..."
    # this is a quote for pcr 22,2
    quote = create_quote(nonce, rsa_key, "0x400004")
    print "\tDONE"

    print "checking full quote..."

    json_tpm_policy = '{"22":"ffffffffffffffffffffffffffffffffffffffff","02":"0000000000000000000000000000000000000000"}'

    tpm_policy = readPolicy(json_tpm_policy)
    print "\tVerified %s" % check_quote(nonce, rsa_key, quote, aik, tpm_policy)

    print "\n========\n\nchecking deepquote"
    TEST_DQ = 'deJxjYP7//z8jAwPD2YOCkba7vsoFf9ndFiVZtO7EGaG04uNiAn6W4rdCQuZO0FG4vW6vfZRZr9ln1+2s4mmTXYxPPXDW/Bjbcbo2tC+Q7W/tH4kvS3of1jq57gjQYWZ5OHXVvPN89ZeXnfPODjxz7tfZ0y9vvfOw+57Y/GmO7sMjxyQ6eabK7j6kHTCp8+bt+POPJksYeK3/NV2lcWfnxN4+XxWXr0zzc5WDf1jb7I1MtEosvcvREtn5MuuzJNPOnW62qk/DDhV+fiQmY6fh/IRJMEw5+VqpJkPlYR+u188lzvb2zra5+dRkJXvar+SZXId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96JA50Ns3fPG2OlCPU4bG7rHB5zr8irjWwiJFtpiE\ncQIDAQAB\n-----END PUBLIC KEY-----\n'
    TEST_VAIK = '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsWZQSYGM03DPfaN9FgH/\nCZ9KHDY79VeVqnhBFk3NcnWQ515ld9cCunqfKLdCow4G3dNmTmsNhp7nNIK4UtSl\nzDCaH2/v1jk5eAPTS0w0E50oYSIMAfN+7PQDCNlzM4mKSiP4sj4uNYj/WVbuZxCM\nb37Cdj8q1Wh+lONUnBfPhIwBnjQ1o9Gzbq2/18xLKHiJvIPeCsNlVabPbbWg26eA\n5sRqeTyx8gSKX0u6fmgrf8KmiHeau8aU131SIZgdvYMv74ZB2i4qgZpNAXK3XvU+\nay5lOaYNr2//MdSSV43hQZvyh9hSb2r4BtoJfJ5eyubPC4hRQSC55/hI+2j3x0+z\nmQIDAQAB\n-----END PUBLIC KEY-----\n'

    print "\tVerified %s" % check_deep_quote(common.TEST_DQ_NONCE, None,
                                             common.TEST_DQ, common.TEST_VAIK,
                                             common.TEST_HAIK, {}, {})

    if True:
        sys.exit(0)

    print "creating a bunch of quotes"
    for _ in range(1000):
        create_quote(nonce, rsa_key)
        check_quote(nonce, rsa_key, quote, aik, tpm_policy)
        pass
    print "done"
示例#6
0
def create_deep_quote(nonce,data=None,vpcrmask=EMPTYMASK,pcrmask=EMPTYMASK):
    # don't deep quote when developing
    if common.DEVELOP_IN_ECLIPSE or common.STUB_TPM:
    # if not using TPM, just return a canned quote
        time.sleep(common.TEST_CREATE_DEEP_QUOTE_DELAY)
        return common.TEST_DQ
    
    quotepath = None
    try:
        # read in the vTPM key handle
        keyhandle = tpm_initialize.load_aik()
        owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')

        if pcrmask is None:
            pcrmask = EMPTYMASK
        if vpcrmask is None:
            vpcrmask = EMPTYMASK
            
        # need to hold the lock while we reset and extend the pcr and then do the quote
        with tpm_exec.tpmutilLock:
            if data is not None:
                # add PCR 16 to pcrmask
                pcrmask = "0x%X"%(int(pcrmask,0) + (1 << common.TPM_DATA_PCR))
                tpm_exec.run("pcrreset -ix %d"%common.TPM_DATA_PCR,lock=False)
                tpm_exec.run("extend -ix %d -ic %s"%(common.TPM_DATA_PCR,hashlib.sha1(data).hexdigest()),lock=False)
            
            #make a temp file for the quote 
            quotefd,quotepath = tempfile.mkstemp()
            
            command = "deepquote -vk %s -hm %s -vm %s -nonce %s -pwdo %s -oq %s" % (keyhandle, pcrmask, vpcrmask, nonce, owner_pw,quotepath)
            #print("Executing %s"%(command))
            tpm_exec.run(command,lock=False)

        # read in the quote
        f = open(quotepath,"rb")
        quote = base64.b64encode(f.read().encode("zlib"))
        f.close()
        os.close(quotefd)
    finally:
        if quotepath is not None:
            os.remove(quotepath)

    return quote
示例#7
0
def read_ekcert_nvram():
    #make a temp file for the quote
    with tempfile.NamedTemporaryFile() as nvpath:
        owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')

        (output, code, ekcert) = tpm_exec.run(
            "nv_readvalue -pwdo %s -in 1000f000 -cert -of %s" %
            (owner_pw, nvpath.name),
            raiseOnError=False,
            outputpath=nvpath.name)

        if code != tpm_exec.EXIT_SUCESS and len(output) > 0 and output[
                0].startswith("Error Illegal index from NV_ReadValue"):
            logger.warn("No EK certificate found in TPM NVRAM")
            return None
        elif code != tpm_exec.EXIT_SUCESS:
            raise Exception("nv_readvalue for ekcert failed with code " +
                            str(code) + ": " + str(output))

    return base64.b64encode(ekcert)
示例#8
0
def main(argv=sys.argv):
    nonce = "NABIL"
    rsa_key = "somebase64data"
    
    print "creating quote..."
    quote = create_quote(nonce,rsa_key)
    print "\tDONE"

    print "checking quote..."
    if common.STUB_TPM:
        aik = common.TEST_AIK
    else:
        aik = tpm_initialize.get_tpm_metadata('aik')
        
    print "\tVerified %s"%check_quote(nonce,rsa_key,quote,aik)

    print "creating full quote..."
    # this is a quote for pcr 22,2
    quote = create_quote(nonce,rsa_key,"0x400004")
    print "\tDONE"

    print "checking full quote..."
    
    json_tpm_policy = '{"22":"ffffffffffffffffffffffffffffffffffffffff","02":"0000000000000000000000000000000000000000"}'
    
    tpm_policy = readPolicy(json_tpm_policy)
    print "\tVerified %s"%check_quote(nonce,rsa_key,quote,aik,tpm_policy)
    
    print "\n========\n\nchecking deepquote"
    print "\tVerified %s"%check_deep_quote(common.TEST_DQ_NONCE, None, common.TEST_DQ, common.TEST_VAIK, common.TEST_HAIK, {}, {})
    
    if True:
        sys.exit(0)
        
    print "creating a bunch of quotes"
    for _ in range(1000):
        create_quote(nonce,rsa_key)
        check_quote(nonce,rsa_key,quote,aik,tpm_policy)
        pass
    print "done"
示例#9
0
def write_key_nvram(key):
    if common.STUB_TPM:
        storage = open("tpm_nvram","wb")
        storage.write(key)
        storage.close()
        return
          
    owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')
    keyFile = None
    try:
        # write out quote
        keyfd,keypath = tempfile.mkstemp()
        keyFile = open(keypath,"wb")
        keyFile.write(key)
        keyFile.close()
        os.close(keyfd)
        tpm_exec.run("nv_definespace -pwdo %s -in 1 -sz %d -pwdd %s -per 40004"%(owner_pw,common.BOOTSTRAP_KEY_SIZE,owner_pw))
        tpm_exec.run("nv_writevalue -pwdd %s -in 1 -if %s"%(owner_pw,keyFile.name))
    finally:
        if keyFile is not None:
            os.remove(keyFile.name)
    return
示例#10
0
def read_key_nvram():
    with tempfile.NamedTemporaryFile() as nvpath:
        owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')

        (output, code, key) = tpm_exec.run(
            "nv_readvalue -pwdd %s -in 1 -sz %d -of %s" %
            (owner_pw, common.BOOTSTRAP_KEY_SIZE, nvpath.name),
            raiseOnError=False,
            outputpath=nvpath.name)

        if code != tpm_exec.EXIT_SUCESS and len(output) > 0 and (
                output[0].startswith("Error Illegal index from NV_ReadValue")
                or output[0].startswith("Error Authentication failed")):
            logger.debug("No stored U in TPM NVRAM")
            return None
        elif code != tpm_exec.EXIT_SUCESS:
            raise Exception("nv_readvalue failed with code " + str(code) +
                            ": " + str(output))

    if len(key) != common.BOOTSTRAP_KEY_SIZE:
        logger.debug("Invalid key length from NVRAM: %d" % (len(key)))
        return None
    return key
示例#11
0
def read_key_nvram():
    if common.STUB_TPM:
        if not os.path.isfile("tpm_nvram"):
            return None
        storage = open("tpm_nvram","rb")
        key = storage.read()
        storage.close()
        return key
    nvpath = None
    try: 
        owner_pw = tpm_initialize.get_tpm_metadata('owner_pw')
        
        #make a temp file for the nvram return 
        nvfd,nvpath = tempfile.mkstemp()
        
        (output,code) = tpm_exec.run("nv_readvalue -pwdd %s -in 1 -sz %d -of %s"%(owner_pw,common.BOOTSTRAP_KEY_SIZE,nvpath),raiseOnError=False)
            
        if code!=tpm_exec.EXIT_SUCESS and len(output)>0 and (output[0].startswith("Error Illegal index from NV_ReadValue") or output[0].startswith("Error Authentication failed")):
            logger.debug("No stored U in TPM NVRAM")
            return None
        elif code!=tpm_exec.EXIT_SUCESS:
            raise Exception("nv_readvalue failed with code "+str(code)+": "+str(output))
        
        # read in the cert
        f = open(nvpath,"rb")
        key = f.read()
        f.close()
        os.close(nvfd)
    finally:
        if nvpath is not None:
            os.remove(nvpath)

    if len(key)!=common.BOOTSTRAP_KEY_SIZE:
        logger.debug("Invalid key length from NVRAM: %d"%(len(key)))
        return None
    return key