Namyoon Woo | f442814 | 2019-10-30 19:02:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | #include "MemoryLib_fp.h" |
| 7 | #include "PolicyFidoSigned_fp.h" |
| 8 | |
| 9 | #if IS_CCE_ENABLED(PolicyFidoSigned) |
| 10 | /* |
| 11 | * Marshal output data of PolicyFidoSigned command. |
| 12 | * |
| 13 | * @param source input buffer of data to marshal |
| 14 | * @param tag Request tag |
| 15 | * @param buffer output buffer to write marshalled data |
| 16 | * @param size size of marshalled data |
| 17 | * @return actual size of response excluding the size of the parameter field. |
| 18 | */ |
| 19 | static UINT16 |
| 20 | PolicyFidoSigned_Out_Marshal(PolicyFidoSigned_Out* source, |
| 21 | TPMI_ST_COMMAND_TAG tag, |
| 22 | BYTE** buffer, |
| 23 | INT32* size) |
| 24 | { |
| 25 | if (tag == TPM_ST_SESSIONS) { |
| 26 | UINT32 param_size = 0; |
| 27 | |
| 28 | /* Add param_size=0 to indicate size of the parameter area. */ |
| 29 | UINT32_Marshal(¶m_size, buffer, size); |
| 30 | } |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Unmarshal input data of PolicyFidoSigned command. |
| 37 | * |
| 38 | * @param target input buffer to unmarshal |
| 39 | * @param req_handles |
| 40 | * @param buffer output buffer to write unmarshalled data |
| 41 | * @param size size of unmarshalled data. |
| 42 | * @return TPM_RC_SUCCESS if processed successfully, or |
| 43 | * TPM_RC_SIZE if input data size is not correct, or |
| 44 | * TPM_RC_SCHEME if Authenticator algorithm is not specified, or |
| 45 | * TPM_RC_HASH if hash is missing or wrong. |
| 46 | * |
| 47 | * |
| 48 | */ |
| 49 | static TPM_RC |
| 50 | PolicyFidoSigned_In_Unmarshal(PolicyFidoSigned_In* target, |
| 51 | TPM_HANDLE req_handles[], |
| 52 | BYTE** buffer, |
| 53 | INT32* size) |
| 54 | { |
| 55 | TPM_RC result = TPM_RC_SUCCESS; |
| 56 | int i; |
| 57 | |
| 58 | /* Get request handles from req_handles array. */ |
| 59 | target->authObject = req_handles[0]; |
| 60 | target->policySession = req_handles[1]; |
| 61 | |
| 62 | result = UINT16_Unmarshal(&target->authData.t.size, buffer, size); |
| 63 | if (result != TPM_RC_SUCCESS) |
| 64 | return result; |
| 65 | |
| 66 | if (target->authData.t.size > MAX_AUTH_DATA_SIZE || |
| 67 | target->authData.t.size == 0) |
| 68 | return TPM_RC_SIZE; |
| 69 | |
| 70 | /* Unmarshall authData. */ |
| 71 | for (i = 0; i < target->authData.t.size; ++i) { |
| 72 | result = BYTE_Unmarshal(&(target->authData.t.buffer[i]), buffer, size); |
| 73 | if (result != TPM_RC_SUCCESS) |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | /* Unmarshal request parameters. */ |
| 78 | result = UINT16_Unmarshal(&target->authDataDescrCount, buffer, size); |
| 79 | if (result != TPM_RC_SUCCESS) |
| 80 | return result; |
| 81 | |
| 82 | if (target->authDataDescrCount > sizeof(target->authDataDescr)) |
| 83 | return TPM_RC_SIZE; |
| 84 | |
| 85 | /* Unmarshall authDataDescr. */ |
| 86 | for (i = 0; i < target->authDataDescrCount; i++) { |
| 87 | result = UINT16_Unmarshal(&(target->authDataDescr[i].offset), buffer, size); |
| 88 | if (result != TPM_RC_SUCCESS) |
| 89 | return result; |
| 90 | |
| 91 | result = UINT16_Unmarshal(&(target->authDataDescr[i].size), buffer, size); |
| 92 | if (result != TPM_RC_SUCCESS) |
| 93 | return result; |
| 94 | |
| 95 | /* The range size should be non-zero. */ |
| 96 | if (target->authDataDescr[i].size == 0) |
| 97 | return TPM_RC_VALUE; |
| 98 | |
| 99 | /* The range is beyond the actual authData string */ |
| 100 | if (target->authDataDescr[i].offset + target->authDataDescr[i].size > |
| 101 | target->authData.t.size) |
| 102 | return TPM_RC_VALUE; |
| 103 | } |
| 104 | |
| 105 | result = TPMT_SIGNATURE_Unmarshal(&target->auth, buffer, size); |
| 106 | if (result != TPM_RC_SUCCESS) |
| 107 | return result; |
| 108 | |
| 109 | /* Size should be zero. */ |
| 110 | if (*size) |
| 111 | return TPM_RC_SIZE; |
| 112 | |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | TPM_RC Exec_PolicyFidoSigned(TPMI_ST_COMMAND_TAG tag, |
| 117 | BYTE** req_param_buffer, |
| 118 | INT32* req_param_buffer_size, |
| 119 | TPM_HANDLE req_handles[], |
| 120 | UINT32* resp_handle_buf_size, |
| 121 | UINT32* resp_param_buf_size) |
| 122 | { |
| 123 | TPM_RC result = TPM_RC_SUCCESS; |
| 124 | PolicyFidoSigned_In in; |
| 125 | PolicyFidoSigned_Out out; |
| 126 | BYTE* resp_buf; |
| 127 | INT32 resp_buf_size; |
| 128 | const INT32 resp_header_size = sizeof(TPM_ST) + sizeof(UINT32) + |
| 129 | sizeof(TPM_RC); |
| 130 | |
| 131 | *resp_handle_buf_size = 0; |
| 132 | *resp_param_buf_size = 0; |
| 133 | |
| 134 | /* Unmarshal request parameters to input structure. */ |
| 135 | result = PolicyFidoSigned_In_Unmarshal(&in, |
| 136 | req_handles, |
| 137 | req_param_buffer, |
| 138 | req_param_buffer_size); |
| 139 | if (result != TPM_RC_SUCCESS) |
| 140 | return result; |
| 141 | |
| 142 | /* Execute command. */ |
| 143 | result = TPM2_PolicyFidoSigned(&in, &out); |
| 144 | if (result != TPM_RC_SUCCESS) |
| 145 | return result; |
| 146 | |
| 147 | /* Marshal output structure to global response buffer. */ |
| 148 | resp_buf = MemoryGetResponseBuffer(TPM_CCE_PolicyFidoSigned) |
| 149 | + resp_header_size; |
| 150 | resp_buf_size = MAX_RESPONSE_SIZE - resp_header_size; |
| 151 | |
| 152 | *resp_param_buf_size = PolicyFidoSigned_Out_Marshal(&out, |
| 153 | tag, |
| 154 | &resp_buf, |
| 155 | &resp_buf_size); |
| 156 | return TPM_RC_SUCCESS; |
| 157 | } |
| 158 | #endif /* IS_CCE_ENABLED(PolicyFidoSigned) */ |