Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "dsa_key.h" |
| 18 | #include "dsa_operation.h" |
| 19 | #include "openssl_utils.h" |
| 20 | #include "unencrypted_key_blob.h" |
| 21 | |
| 22 | namespace keymaster { |
| 23 | |
| 24 | const uint32_t DSA_DEFAULT_KEY_SIZE = 2048; |
| 25 | |
| 26 | template <keymaster_tag_t Tag> |
| 27 | static void GetDsaParamData(const AuthorizationSet& auths, TypedTag<KM_BIGNUM, Tag> tag, |
| 28 | keymaster_blob_t* blob) { |
| 29 | if (!auths.GetTagValue(tag, blob)) |
| 30 | blob->data = NULL; |
| 31 | } |
| 32 | |
| 33 | // Store the specified DSA param in auths |
| 34 | template <keymaster_tag_t Tag> |
| 35 | static void SetDsaParamData(AuthorizationSet* auths, TypedTag<KM_BIGNUM, Tag> tag, BIGNUM* number) { |
| 36 | keymaster_blob_t blob; |
| 37 | convert_bn_to_blob(number, &blob); |
| 38 | auths->push_back(Authorization(tag, blob)); |
| 39 | delete[] blob.data; |
| 40 | } |
| 41 | |
| 42 | DsaKey* DsaKey::GenerateKey(const AuthorizationSet& key_description, const Logger& logger, |
| 43 | keymaster_error_t* error) { |
| 44 | if (!error) |
| 45 | return NULL; |
| 46 | |
| 47 | AuthorizationSet authorizations(key_description); |
| 48 | |
| 49 | uint32_t key_size = DSA_DEFAULT_KEY_SIZE; |
| 50 | if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) |
| 51 | authorizations.push_back(Authorization(TAG_KEY_SIZE, key_size)); |
| 52 | |
| 53 | UniquePtr<DSA, DSA_Delete> dsa_key(DSA_new()); |
| 54 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
| 55 | if (dsa_key.get() == NULL || pkey.get() == NULL) { |
| 56 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | // If anything goes wrong in the next section, it's a param problem. |
| 61 | *error = KM_ERROR_INVALID_DSA_PARAMS; |
| 62 | |
| 63 | keymaster_blob_t g_blob; |
| 64 | keymaster_blob_t p_blob; |
| 65 | keymaster_blob_t q_blob; |
| 66 | GetDsaParamData(authorizations, TAG_DSA_GENERATOR, &g_blob); |
| 67 | GetDsaParamData(authorizations, TAG_DSA_P, &p_blob); |
| 68 | GetDsaParamData(authorizations, TAG_DSA_Q, &q_blob); |
| 69 | |
| 70 | if (g_blob.data == NULL && p_blob.data == NULL && q_blob.data == NULL) { |
| 71 | logger.info("DSA parameters unspecified, generating them for key size %d", key_size); |
| 72 | if (!DSA_generate_parameters_ex(dsa_key.get(), key_size, NULL /* seed */, 0 /* seed_len */, |
| 73 | NULL /* counter_ret */, NULL /* h_ret */, |
| 74 | NULL /* callback */)) { |
| 75 | logger.severe("DSA parameter generation failed."); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | SetDsaParamData(&authorizations, TAG_DSA_GENERATOR, dsa_key->g); |
| 80 | SetDsaParamData(&authorizations, TAG_DSA_P, dsa_key->p); |
| 81 | SetDsaParamData(&authorizations, TAG_DSA_Q, dsa_key->q); |
| 82 | } else if (g_blob.data == NULL || p_blob.data == NULL || q_blob.data == NULL) { |
| 83 | logger.severe("Some DSA parameters provided. Provide all or none"); |
| 84 | return NULL; |
| 85 | } else { |
| 86 | // All params provided. Use them. |
| 87 | dsa_key->g = BN_bin2bn(g_blob.data, g_blob.data_length, NULL); |
| 88 | dsa_key->p = BN_bin2bn(p_blob.data, p_blob.data_length, NULL); |
| 89 | dsa_key->q = BN_bin2bn(q_blob.data, q_blob.data_length, NULL); |
| 90 | if (dsa_key->g == NULL || dsa_key->p == NULL || dsa_key->q == NULL) { |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (!DSA_generate_key(dsa_key.get())) { |
| 96 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | DsaKey* new_key = new DsaKey(dsa_key.release(), authorizations, logger); |
| 101 | *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 102 | return new_key; |
| 103 | } |
| 104 | |
| 105 | template <keymaster_tag_t T> |
| 106 | keymaster_error_t GetOrCheckDsaParam(TypedTag<KM_BIGNUM, T> tag, BIGNUM* bn, |
| 107 | AuthorizationSet* auths) { |
| 108 | keymaster_blob_t blob; |
| 109 | if (auths->GetTagValue(tag, &blob)) { |
| 110 | // value specified, make sure it matches |
| 111 | UniquePtr<BIGNUM, BIGNUM_Delete> extracted_bn(BN_bin2bn(blob.data, blob.data_length, NULL)); |
| 112 | if (extracted_bn.get() == NULL) |
| 113 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 114 | if (BN_cmp(extracted_bn.get(), bn) != 0) |
| 115 | return KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 116 | } else { |
| 117 | // value not specified, add it |
| 118 | UniquePtr<uint8_t[]> data(new uint8_t[BN_num_bytes(bn)]); |
| 119 | BN_bn2bin(bn, data.get()); |
| 120 | auths->push_back(tag, data.get(), BN_num_bytes(bn)); |
| 121 | } |
| 122 | return KM_ERROR_OK; |
| 123 | } |
| 124 | |
| 125 | static size_t calculate_key_size_in_bits(DSA* dsa_key) { |
| 126 | // Openssl provides no convenient way to get a DSA key size, but dsa_key->p is L bits long. |
| 127 | // There may be some leading zeros that mess up this calculation, but DSA key sizes are also |
| 128 | // constrained to be multiples of 64 bits. So the key size is the bit length of p rounded up to |
| 129 | // the nearest 64. |
| 130 | return ((BN_num_bytes(dsa_key->p) * 8) + 63) / 64 * 64; |
| 131 | } |
| 132 | |
| 133 | /* static */ |
| 134 | DsaKey* DsaKey::ImportKey(const AuthorizationSet& key_description, EVP_PKEY* pkey, |
| 135 | const Logger& logger, keymaster_error_t* error) { |
| 136 | if (!error) |
| 137 | return NULL; |
| 138 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 139 | |
| 140 | UniquePtr<DSA, DSA_Delete> dsa_key(EVP_PKEY_get1_DSA(pkey)); |
| 141 | if (!dsa_key.get()) |
| 142 | return NULL; |
| 143 | |
| 144 | AuthorizationSet authorizations(key_description); |
| 145 | |
| 146 | *error = GetOrCheckDsaParam(TAG_DSA_GENERATOR, dsa_key->g, &authorizations); |
| 147 | if (*error != KM_ERROR_OK) |
| 148 | return NULL; |
| 149 | |
| 150 | *error = GetOrCheckDsaParam(TAG_DSA_P, dsa_key->p, &authorizations); |
| 151 | if (*error != KM_ERROR_OK) |
| 152 | return NULL; |
| 153 | |
| 154 | *error = GetOrCheckDsaParam(TAG_DSA_Q, dsa_key->q, &authorizations); |
| 155 | if (*error != KM_ERROR_OK) |
| 156 | return NULL; |
| 157 | |
| 158 | uint32_t key_size_in_bits; |
| 159 | if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size_in_bits)) { |
| 160 | // key_bits specified, make sure it matches the key. |
| 161 | if (key_size_in_bits != calculate_key_size_in_bits(dsa_key.get())) { |
| 162 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 163 | return NULL; |
| 164 | } |
| 165 | } else { |
| 166 | // key_size_bits not specified, add it. |
| 167 | authorizations.push_back(TAG_KEY_SIZE, calculate_key_size_in_bits(dsa_key.get())); |
| 168 | } |
| 169 | |
| 170 | keymaster_algorithm_t algorithm; |
| 171 | if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) { |
| 172 | if (algorithm != KM_ALGORITHM_DSA) { |
| 173 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 174 | return NULL; |
| 175 | } |
| 176 | } else { |
| 177 | authorizations.push_back(TAG_ALGORITHM, KM_ALGORITHM_DSA); |
| 178 | } |
| 179 | |
| 180 | // Don't bother with the other parameters. If the necessary padding, digest, purpose, etc. are |
| 181 | // missing, the error will be diagnosed when the key is used (when auth checking is |
| 182 | // implemented). |
| 183 | *error = KM_ERROR_OK; |
| 184 | return new DsaKey(dsa_key.release(), authorizations, logger); |
| 185 | } |
| 186 | |
| 187 | DsaKey::DsaKey(const UnencryptedKeyBlob& blob, const Logger& logger, keymaster_error_t* error) |
| 188 | : AsymmetricKey(blob, logger) { |
| 189 | if (error) |
| 190 | *error = LoadKey(blob); |
| 191 | } |
| 192 | |
Shawn Willden | 9659921 | 2014-09-26 12:07:44 -0600 | [diff] [blame] | 193 | Operation* DsaKey::CreateOperation(keymaster_purpose_t purpose, keymaster_error_t* error) { |
| 194 | keymaster_digest_t digest = KM_DIGEST_NONE; |
| 195 | if (!authorizations().GetTagValue(TAG_DIGEST, &digest) || digest != KM_DIGEST_NONE) { |
| 196 | *error = KM_ERROR_UNSUPPORTED_DIGEST; |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 200 | Operation* op; |
| 201 | switch (purpose) { |
| 202 | case KM_PURPOSE_SIGN: |
Shawn Willden | 9659921 | 2014-09-26 12:07:44 -0600 | [diff] [blame] | 203 | op = new DsaSignOperation(purpose, logger_, digest, dsa_key_.release()); |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 204 | break; |
| 205 | case KM_PURPOSE_VERIFY: |
Shawn Willden | 9659921 | 2014-09-26 12:07:44 -0600 | [diff] [blame] | 206 | op = new DsaVerifyOperation(purpose, logger_, digest, dsa_key_.release()); |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 207 | break; |
| 208 | default: |
Shawn Willden | 9659921 | 2014-09-26 12:07:44 -0600 | [diff] [blame] | 209 | *error = KM_ERROR_INCOMPATIBLE_PURPOSE; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 210 | return NULL; |
| 211 | } |
| 212 | *error = op ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 213 | return op; |
| 214 | } |
| 215 | |
| 216 | bool DsaKey::EvpToInternal(const EVP_PKEY* pkey) { |
| 217 | dsa_key_.reset(EVP_PKEY_get1_DSA(const_cast<EVP_PKEY*>(pkey))); |
| 218 | return dsa_key_.get() != NULL; |
| 219 | } |
| 220 | |
| 221 | bool DsaKey::InternalToEvp(EVP_PKEY* pkey) const { |
| 222 | return EVP_PKEY_set1_DSA(pkey, dsa_key_.get()) == 1; |
| 223 | } |
| 224 | |
| 225 | } // namespace keymaster |