TPM_RESULT TPM_ReleaseTransportSigned(TPM_KEY_HANDLE keyHandle, TPM_NONCE *antiReplay, TPM_AUTH *auth1, TPM_AUTH *auth2, TPM_MODIFIER_INDICATOR *locality, TPM_CURRENT_TICKS *currentTicks, UINT32 *sigSize, BYTE **sig) { TPM_RESULT res; TPM_KEY_DATA *key; TPM_SESSION_DATA *session; BYTE buf[30 + 20]; info("TPM_ReleaseTransportSigned()"); /* get key */ key = tpm_get_key(keyHandle); if (key == NULL) return TPM_INVALID_KEYHANDLE; /* verify authorization */ if (auth2->authHandle != TPM_INVALID_HANDLE || key->authDataUsage != TPM_AUTH_NEVER) { res = tpm_verify_auth(auth1, key->usageAuth, keyHandle); if (res != TPM_SUCCESS) return res; session = tpm_get_transport(auth2->authHandle); if (session == NULL) return TPM_INVALID_AUTHHANDLE; res = tpm_verify_auth(auth2, session->transInternal.authData, TPM_INVALID_HANDLE); if (res != TPM_SUCCESS) return (res == TPM_AUTHFAIL) ? TPM_AUTH2FAIL : res; } else { session = tpm_get_transport(auth1->authHandle); if (session == NULL) return TPM_INVALID_AUTHHANDLE; res = tpm_verify_auth(auth1, session->transInternal.authData, TPM_INVALID_HANDLE); if (res != TPM_SUCCESS) return res; } /* invalidate transport session */ auth1->continueAuthSession = FALSE; /* logging must be enabled */ if (!(session->transInternal.transPublic.transAttributes & TPM_TRANSPORT_LOG)) return TPM_BAD_MODE; *locality = tpmData.stany.flags.localityModifier; memcpy(currentTicks, &tpmData.stany.data.currentTicks, sizeof(TPM_CURRENT_TICKS)); transport_log_out(auth1->digest, &session->transInternal.transDigest); /* setup a TPM_SIGN_INFO structure */ memcpy(&buf[0], (uint8_t*)"\x00\x05TRAN", 6); memcpy(&buf[6], antiReplay->nonce, 20); memcpy(&buf[26], (uint8_t*)"\x00\x00\x00\x14", 4); memcpy(&buf[30], session->transInternal.transDigest.digest, 20); /* sign info structure */ if (key->sigScheme == TPM_SS_RSASSAPKCS1v15_SHA1) { tpm_sha1_ctx_t ctx; debug("TPM_SS_RSASSAPKCS1v15_SHA1"); tpm_sha1_init(&ctx); tpm_sha1_update(&ctx, buf, sizeof(buf)); tpm_sha1_final(&ctx, buf); res = tpm_sign(key, auth1, FALSE, buf, SHA1_DIGEST_LENGTH, sig, sigSize); } else if (key->sigScheme == TPM_SS_RSASSAPKCS1v15_INFO) { debug("TPM_SS_RSASSAPKCS1v15_INFO"); res = tpm_sign(key, auth1, TRUE, buf, sizeof(buf), sig, sigSize); } else { debug("unsupported signature scheme: %02x", key->sigScheme); res = TPM_INVALID_KEYUSAGE; } return res; }
TPM_RESULT TPM_EstablishTransport(TPM_KEY_HANDLE encHandle, TPM_TRANSPORT_PUBLIC *transPublic, UINT32 secretSize, BYTE *secret, TPM_AUTH *auth1, TPM_TRANSHANDLE *transHandle, TPM_MODIFIER_INDICATOR *locality, TPM_CURRENT_TICKS *currentTicks, TPM_NONCE *transNonceEven) { TPM_RESULT res; TPM_KEY_DATA *key; TPM_TRANSPORT_AUTH trans_auth; TPM_SESSION_DATA *session; info("TPM_EstablishTransport()"); /* setup authorization data */ if (encHandle == TPM_KH_TRANSPORT) { if (auth1->authHandle != TPM_INVALID_HANDLE) return TPM_BADTAG; if (transPublic->transAttributes & TPM_TRANSPORT_ENCRYPT) return TPM_BAD_SCHEME; if (secretSize != 20) return TPM_BAD_PARAM_SIZE; memcpy(trans_auth.authData, secret, 20); } else { /* get key and verify its usage */ key = tpm_get_key(encHandle); if (key == NULL) return TPM_INVALID_KEYHANDLE; if (key->keyUsage != TPM_KEY_STORAGE && key->keyUsage != TPM_KEY_LEGACY) return TPM_INVALID_KEYUSAGE; /* verify authorization */ if (key->authDataUsage != TPM_AUTH_NEVER) { res = tpm_verify_auth(auth1, key->usageAuth, encHandle); if (res != TPM_SUCCESS) return res; if (decrypt_transport_auth(key, secret, secretSize, &trans_auth)) return TPM_DECRYPT_ERROR; } } /* check whether the transport has to be encrypted */ if (transPublic->transAttributes & TPM_TRANSPORT_ENCRYPT) { if (tpmData.permanent.flags.FIPS && transPublic->algID == TPM_ALG_MGF1) return TPM_INAPPROPRIATE_ENC; /* until now, only MGF1 is supported */ if (transPublic->algID != TPM_ALG_MGF1) return TPM_BAD_KEY_PROPERTY; } /* initialize transport session */ tpm_get_random_bytes(transNonceEven->nonce, sizeof(transNonceEven->nonce)); *transHandle = tpm_get_free_session(TPM_ST_TRANSPORT); session = tpm_get_transport(*transHandle); if (session == NULL) return TPM_RESOURCES; session->transInternal.transHandle = *transHandle; memset(&session->transInternal.transDigest, 0, sizeof(TPM_DIGEST)); memcpy(&session->transInternal.transPublic, transPublic, sizeof_TPM_TRANSPORT_PUBLIC((*transPublic))); memcpy(&session->transInternal.transNonceEven, transNonceEven, sizeof(TPM_NONCE)); memcpy(&session->nonceEven, transNonceEven, sizeof(TPM_NONCE)); memcpy(&session->transInternal.authData, trans_auth.authData, sizeof(TPM_AUTHDATA)); *locality = tpmData.stany.flags.localityModifier; memcpy(currentTicks, &tpmData.stany.data.currentTicks, sizeof(TPM_CURRENT_TICKS)); /* perform transport logging */ if (transPublic->transAttributes & TPM_TRANSPORT_LOG) { tpm_sha1_ctx_t sha1; BYTE *ptr, buf[4 + 4 + 4 + sizeof_TPM_CURRENT_TICKS(x) + 20]; UINT32 len; /* log input */ memset(buf, 0, sizeof(buf)); transport_log_in(auth1->digest, buf, &session->transInternal.transDigest); /* compute digest of output parameters and log output */ ptr = buf; len = sizeof(buf); tpm_marshal_UINT32(&ptr, &len, TPM_SUCCESS); tpm_marshal_TPM_COMMAND_CODE(&ptr, &len, TPM_ORD_EstablishTransport); tpm_marshal_TPM_MODIFIER_INDICATOR(&ptr, &len, *locality); tpm_marshal_TPM_CURRENT_TICKS(&ptr, &len, currentTicks); tpm_marshal_TPM_NONCE(&ptr, &len, transNonceEven); tpm_sha1_init(&sha1); tpm_sha1_update(&sha1, buf, sizeof(buf)); tpm_sha1_final(&sha1, buf); transport_log_out(buf, &session->transInternal.transDigest); } /* check whether this is a exclusive transport session */ if (transPublic->transAttributes & TPM_TRANSPORT_EXCLUSIVE) { tpmData.stany.flags.transportExclusive = TRUE; tpmData.stany.data.transExclusive = *transHandle; } auth1->continueAuthSession = FALSE; return TPM_SUCCESS; }
TPM_RESULT TPM_ExecuteTransport(UINT32 inWrappedCmdSize, BYTE *inWrappedCmd, TPM_AUTH *auth1, UINT64 *currentTicks, TPM_MODIFIER_INDICATOR *locality, UINT32 *outWrappedCmdSize, BYTE **outWrappedCmd) { TPM_RESULT res; TPM_SESSION_DATA *session; TPM_REQUEST req; TPM_RESPONSE rsp; BYTE *ptr, buf[4 * 4 + 8 + 20]; UINT32 len, offset; tpm_sha1_ctx_t sha1; info("TPM_ExecuteTransport()"); /* get transport session */ session = tpm_get_transport(auth1->authHandle); if (session == NULL) return TPM_BAD_PARAMETER; /* unmarshal wrapped command */ len = inWrappedCmdSize; ptr = inWrappedCmd; if (tpm_unmarshal_TPM_REQUEST(&ptr, &len, &req)) return TPM_FAIL; /* decrypt wrapped command if needed */ ptr = tpm_malloc(req.paramSize); if (ptr == NULL) return TPM_FAIL; memcpy(ptr, req.param, req.paramSize); if (session->transInternal.transPublic.transAttributes & TPM_TRANSPORT_ENCRYPT) { if (req.ordinal == TPM_ORD_OIAP || req.ordinal == TPM_ORD_OSAP) { offset = req.paramSize; } else if (req.ordinal == TPM_ORD_DSAP) { offset = 30; } else { offset = tpm_get_in_param_offset(req.ordinal); } debug("decrypting %d bytes, starting at pos %d", req.paramSize - offset, offset); decrypt_wrapped_command(ptr + offset, req.paramSize - offset, auth1, session); } req.param = ptr; /* verify authorization */ tpm_compute_in_param_digest(&req); tpm_sha1_init(&sha1); tpm_sha1_update_be32(&sha1, TPM_ORD_ExecuteTransport); tpm_sha1_update_be32(&sha1, inWrappedCmdSize); tpm_sha1_update(&sha1, req.auth1.digest, sizeof(req.auth1.digest)); tpm_sha1_final(&sha1, auth1->digest); res = tpm_verify_auth(auth1, session->transInternal.authData, TPM_INVALID_HANDLE); if (res != TPM_SUCCESS) { tpm_free(req.param); return res; } /* nested transport sessions are not allowed */ if (req.ordinal == TPM_ORD_EstablishTransport || req.ordinal == TPM_ORD_ExecuteTransport || req.ordinal == TPM_ORD_ReleaseTransportSigned) { tpm_free(req.param); return TPM_NO_WRAP_TRANSPORT; } /* log input parameters */ if (session->transInternal.transPublic.transAttributes & TPM_TRANSPORT_LOG) { TPM_DIGEST keyDigest; compute_key_digest(&req, &keyDigest); transport_log_in(req.auth1.digest, keyDigest.digest, &session->transInternal.transDigest); } /* execute and audit command*/ tpm_audit_request(req.ordinal, &req); tpm_execute_command(&req, &rsp); tpm_audit_response(req.ordinal, &rsp); tpm_free(req.param); /* get locality and ticks */ *locality = tpmData.stany.flags.localityModifier; *currentTicks = tpmData.stany.data.currentTicks.currentTicks; /* if required, compute digest of internal output parameters */ debug("result = %d", rsp.result); if (rsp.result == TPM_SUCCESS) { if (rsp.tag == TPM_TAG_RSP_COMMAND) { rsp.auth1 = &req.auth1; tpm_compute_out_param_digest(req.ordinal, &rsp); } /* encrypt parameters */ if (session->transInternal.transPublic.transAttributes & TPM_TRANSPORT_ENCRYPT) { if (req.ordinal == TPM_ORD_OIAP || req.ordinal == TPM_ORD_OSAP) { offset = rsp.paramSize; } else if (req.ordinal == TPM_ORD_DSAP) { offset = rsp.paramSize; } else { offset = tpm_get_out_param_offset(req.ordinal); } debug("encrypting %d bytes, starting at pos %d", rsp.paramSize - offset, offset); encrypt_wrapped_command(rsp.param + offset, rsp.paramSize - offset, auth1, session); } } else { rsp.auth1 = &req.auth1; memset(rsp.auth1->digest, 0, sizeof(*rsp.auth1->digest)); } /* marshal response */ *outWrappedCmdSize = len = rsp.size; *outWrappedCmd = ptr = tpm_malloc(len); if (ptr == NULL) { tpm_free(rsp.param); return TPM_FAIL; } tpm_marshal_TPM_RESPONSE(&ptr, &len, &rsp); debug("marshalling done."); /* log output parameters */ if (session->transInternal.transPublic.transAttributes & TPM_TRANSPORT_LOG) { transport_log_out(rsp.auth1->digest, &session->transInternal.transDigest); } tpm_free(rsp.param); /* compute digest of output parameters */ ptr = buf; len = sizeof(buf); tpm_marshal_UINT32(&ptr, &len, TPM_SUCCESS); tpm_marshal_TPM_COMMAND_CODE(&ptr, &len, TPM_ORD_ExecuteTransport); tpm_marshal_UINT64(&ptr, &len, *currentTicks); tpm_marshal_TPM_MODIFIER_INDICATOR(&ptr, &len, *locality); tpm_marshal_UINT32(&ptr, &len, *outWrappedCmdSize); memcpy(ptr, rsp.auth1->digest, sizeof(rsp.auth1->digest)); tpm_sha1_init(&sha1); tpm_sha1_update(&sha1, buf, sizeof(buf)); tpm_sha1_final(&sha1, auth1->digest); return TPM_SUCCESS; }
TPM_RESULT TPM_SaveContext(TPM_HANDLE handle, TPM_RESOURCE_TYPE resourceType, const BYTE label[16], UINT32 *contextSize, TPM_CONTEXT_BLOB *contextBlob) { TPM_CONTEXT_SENSITIVE context; TPM_SESSION_DATA *session = NULL; TPM_DAA_SESSION_DATA *sessionDAA = NULL; TPM_KEY_DATA *key = NULL; int i = 0; info("TPM_SaveContext() resourceType = %08x", resourceType); /* setup context data */ context.tag = TPM_TAG_CONTEXT_SENSITIVE; context.resourceType = resourceType; if (resourceType == TPM_RT_AUTH || resourceType == TPM_RT_TRANS) { session = (resourceType == TPM_RT_AUTH) ? tpm_get_auth(handle) : tpm_get_transport(handle); if (session == NULL) return TPM_INVALID_RESOURCE; /* store session data */ memcpy(&context.internalData.session, session, sizeof(TPM_SESSION_DATA)); context.internalSize = sizeof_TPM_SESSION_DATA((*session)); /* set context nonce */ memcpy(&context.contextNonce, &tpmData.stany.data.contextNonceSession, sizeof(TPM_NONCE)); } else if (resourceType == TPM_RT_KEY) { key = tpm_get_key(handle); debug("resourceType = TPM_RT_KEY, handle = %08x, key = %p", handle, key); if (key == NULL) return TPM_INVALID_RESOURCE; if (key->keyControl & TPM_KEY_CONTROL_OWNER_EVICT) return TPM_OWNER_CONTROL; /* store key data (shallow copy is ok) */ memcpy(&context.internalData.key, key, sizeof(TPM_KEY_DATA)); context.internalSize = sizeof_TPM_KEY_DATA((*key)); /* set context nonce */ memcpy(&context.contextNonce, &tpmData.stclear.data.contextNonceKey, sizeof(TPM_NONCE)); } else if (resourceType == TPM_RT_DAA_TPM) { sessionDAA = tpm_get_daa(handle); if (sessionDAA == NULL) return TPM_INVALID_RESOURCE; /* store sessionDAA data */ memcpy(&context.internalData.sessionDAA, sessionDAA, sizeof(TPM_DAA_SESSION_DATA)); context.internalSize = sizeof(TPM_DAA_SESSION_DATA); /* set context nonce */ memcpy(&context.contextNonce, &tpmData.stany.data.contextNonceSession, sizeof(TPM_NONCE)); } else { return TPM_INVALID_RESOURCE; } /* setup context blob */ contextBlob->tag = TPM_TAG_CONTEXTBLOB; contextBlob->resourceType = resourceType; contextBlob->handle = handle; memset(&contextBlob->integrityDigest, 0, sizeof(TPM_DIGEST)); memcpy(contextBlob->label, label, sizeof(contextBlob->label)); contextBlob->additionalSize = TPM_SYM_KEY_SIZE; contextBlob->additionalData = tpm_malloc(contextBlob->additionalSize); if (contextBlob->additionalData == NULL) return TPM_FAIL; tpm_get_random_bytes(contextBlob->additionalData, contextBlob->additionalSize); /* increment context counter */ if (resourceType == TPM_RT_KEY) { contextBlob->contextCount = 0; } else { if (tpmData.stany.data.contextCount >= 0xfffffffc) { tpm_free(contextBlob->additionalData); return TPM_TOOMANYCONTEXTS; } contextBlob->contextCount = ++tpmData.stany.data.contextCount; for (i = 0; i < TPM_MAX_SESSION_LIST; i++) { if (tpmData.stany.data.contextList[i] == 0) break; } if (i >= TPM_MAX_SESSION_LIST) { tpm_free(contextBlob->additionalData); return TPM_NOCONTEXTSPACE; } tpmData.stany.data.contextCount++; tpmData.stany.data.contextList[i] = tpmData.stany.data.contextCount; contextBlob->contextCount = tpmData.stany.data.contextCount; } debug("context counter = %d", tpmData.stany.data.contextCount); /* encrypt sensitive data */ if (encrypt_context(contextBlob->additionalData, contextBlob->additionalSize, &context, &contextBlob->sensitiveData, &contextBlob->sensitiveSize)) { tpm_free(contextBlob->additionalData); return TPM_ENCRYPT_ERROR; } /* compute context digest */ if (compute_context_digest(contextBlob, &contextBlob->integrityDigest)) { tpm_free(contextBlob->additionalData); return TPM_FAIL; } *contextSize = sizeof_TPM_CONTEXT_BLOB((*contextBlob)); if (resourceType != TPM_RT_KEY) { /* The TPM MUST invalidate all information regarding the resource * except for information needed for reloading. */ if (resourceType != TPM_RT_DAA_TPM) session->type = TPM_ST_INVALID; else { memset(sessionDAA, 0, sizeof(TPM_DAA_SESSION_DATA)); sessionDAA->type = TPM_ST_INVALID; tpmData.stany.data.currentDAA = 0; } } return TPM_SUCCESS; }