• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 <keymaster/km_openssl/openssl_utils.h>
18 
19 #include <keymaster/android_keymaster_utils.h>
20 #include <openssl/rand.h>
21 
22 #include <keymaster/km_openssl/openssl_err.h>
23 
24 namespace keymaster {
25 
26 constexpr uint32_t kAffinePointLength = 32;
27 
ec_get_group_size(const EC_GROUP * group,size_t * key_size_bits)28 keymaster_error_t ec_get_group_size(const EC_GROUP* group, size_t* key_size_bits) {
29     switch (EC_GROUP_get_curve_name(group)) {
30     case NID_secp224r1:
31         *key_size_bits = 224;
32         break;
33     case NID_X9_62_prime256v1:
34         *key_size_bits = 256;
35         break;
36     case NID_secp384r1:
37         *key_size_bits = 384;
38         break;
39     case NID_secp521r1:
40         *key_size_bits = 521;
41         break;
42     default:
43         return KM_ERROR_UNSUPPORTED_EC_FIELD;
44     }
45     return KM_ERROR_OK;
46 }
47 
ec_get_group(keymaster_ec_curve_t curve)48 EC_GROUP* ec_get_group(keymaster_ec_curve_t curve) {
49     switch (curve) {
50     case KM_EC_CURVE_P_224:
51         return EC_GROUP_new_by_curve_name(NID_secp224r1);
52         break;
53     case KM_EC_CURVE_P_256:
54         return EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
55         break;
56     case KM_EC_CURVE_P_384:
57         return EC_GROUP_new_by_curve_name(NID_secp384r1);
58         break;
59     case KM_EC_CURVE_P_521:
60         return EC_GROUP_new_by_curve_name(NID_secp521r1);
61         break;
62     default:
63         return nullptr;
64         break;
65     }
66 }
67 
convert_to_evp(keymaster_algorithm_t algorithm)68 static int convert_to_evp(keymaster_algorithm_t algorithm) {
69     switch (algorithm) {
70     case KM_ALGORITHM_RSA:
71         return EVP_PKEY_RSA;
72     case KM_ALGORITHM_EC:
73         return EVP_PKEY_EC;
74     default:
75         return -1;
76     };
77 }
78 
convert_pkcs8_blob_to_evp(const uint8_t * key_data,size_t key_length,keymaster_algorithm_t expected_algorithm,UniquePtr<EVP_PKEY,EVP_PKEY_Delete> * pkey)79 keymaster_error_t convert_pkcs8_blob_to_evp(const uint8_t* key_data, size_t key_length,
80                                             keymaster_algorithm_t expected_algorithm,
81                                             UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* pkey) {
82     if (key_data == nullptr || key_length <= 0) return KM_ERROR_INVALID_KEY_BLOB;
83 
84     UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> pkcs8(
85         d2i_PKCS8_PRIV_KEY_INFO(nullptr, &key_data, key_length));
86     if (pkcs8.get() == nullptr) return TranslateLastOpenSslError(true /* log_message */);
87 
88     pkey->reset(EVP_PKCS82PKEY(pkcs8.get()));
89     if (!pkey->get()) return TranslateLastOpenSslError(true /* log_message */);
90 
91     if (EVP_PKEY_type((*pkey)->type) != convert_to_evp(expected_algorithm)) {
92         LOG_E("EVP key algorithm was %d, not the expected %d", EVP_PKEY_type((*pkey)->type),
93               convert_to_evp(expected_algorithm));
94         return KM_ERROR_INVALID_KEY_BLOB;
95     }
96 
97     return KM_ERROR_OK;
98 }
99 
KeyMaterialToEvpKey(keymaster_key_format_t key_format,const KeymasterKeyBlob & key_material,keymaster_algorithm_t expected_algorithm,EVP_PKEY_Ptr * pkey)100 keymaster_error_t KeyMaterialToEvpKey(keymaster_key_format_t key_format,
101                                       const KeymasterKeyBlob& key_material,
102                                       keymaster_algorithm_t expected_algorithm,
103                                       EVP_PKEY_Ptr* pkey) {
104     if (key_format != KM_KEY_FORMAT_PKCS8) return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
105 
106     return convert_pkcs8_blob_to_evp(key_material.key_material, key_material.key_material_size,
107                                      expected_algorithm, pkey);
108 }
109 
EvpKeyToKeyMaterial(const EVP_PKEY * pkey,KeymasterKeyBlob * key_blob)110 keymaster_error_t EvpKeyToKeyMaterial(const EVP_PKEY* pkey, KeymasterKeyBlob* key_blob) {
111     int key_data_size = i2d_PrivateKey(pkey, nullptr /* key_data*/);
112     if (key_data_size <= 0) return TranslateLastOpenSslError();
113 
114     if (!key_blob->Reset(key_data_size)) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
115 
116     uint8_t* tmp = key_blob->writable_data();
117     i2d_PrivateKey(pkey, &tmp);
118 
119     return KM_ERROR_OK;
120 }
121 
122 // Remote provisioning helper function
GetEcdsa256KeyFromCert(const keymaster_blob_t * km_cert,uint8_t * x_coord,size_t x_length,uint8_t * y_coord,size_t y_length)123 keymaster_error_t GetEcdsa256KeyFromCert(const keymaster_blob_t* km_cert, uint8_t* x_coord,
124                                          size_t x_length, uint8_t* y_coord, size_t y_length) {
125     if (km_cert == nullptr || x_coord == nullptr || y_coord == nullptr) {
126         return KM_ERROR_UNEXPECTED_NULL_POINTER;
127     }
128     if (x_length != kAffinePointLength || y_length != kAffinePointLength) {
129         return KM_ERROR_INVALID_ARGUMENT;
130     }
131     const uint8_t* temp = km_cert->data;
132     X509_Ptr cert(d2i_X509(NULL, &temp, km_cert->data_length));
133     if (!cert.get()) return TranslateLastOpenSslError();
134     EVP_PKEY_Ptr pubKey(X509_get_pubkey(cert.get()));
135     if (!pubKey.get()) return TranslateLastOpenSslError();
136     EC_KEY* ecKey = EVP_PKEY_get0_EC_KEY(pubKey.get());
137     if (!ecKey) return TranslateLastOpenSslError();
138     const EC_POINT* jacobian_coords = EC_KEY_get0_public_key(ecKey);
139     if (!jacobian_coords) return TranslateLastOpenSslError();
140     bssl::UniquePtr<BIGNUM> x(BN_new());
141     bssl::UniquePtr<BIGNUM> y(BN_new());
142     BN_CTX_Ptr ctx(BN_CTX_new());
143     if (!ctx.get()) return TranslateLastOpenSslError();
144     if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ecKey), jacobian_coords, x.get(),
145                                              y.get(), ctx.get())) {
146         return TranslateLastOpenSslError();
147     }
148     uint8_t* tmp_x = x_coord;
149     if (BN_bn2binpad(x.get(), tmp_x, kAffinePointLength) != kAffinePointLength) {
150         return TranslateLastOpenSslError();
151     }
152     uint8_t* tmp_y = y_coord;
153     if (BN_bn2binpad(y.get(), tmp_y, kAffinePointLength) != kAffinePointLength) {
154         return TranslateLastOpenSslError();
155     }
156     return KM_ERROR_OK;
157 }
158 
ec_group_size_bits(EC_KEY * ec_key)159 size_t ec_group_size_bits(EC_KEY* ec_key) {
160     const EC_GROUP* group = EC_KEY_get0_group(ec_key);
161     UniquePtr<BN_CTX, BN_CTX_Delete> bn_ctx(BN_CTX_new());
162     UniquePtr<BIGNUM, BIGNUM_Delete> order(BN_new());
163     if (!EC_GROUP_get_order(group, order.get(), bn_ctx.get())) {
164         LOG_E("Failed to get EC group order", 0);
165         return 0;
166     }
167     return BN_num_bits(order.get());
168 }
169 
GenerateRandom(uint8_t * buf,size_t length)170 keymaster_error_t GenerateRandom(uint8_t* buf, size_t length) {
171     if (RAND_bytes(buf, length) != 1) return KM_ERROR_UNKNOWN_ERROR;
172     return KM_ERROR_OK;
173 }
174 
175 }  // namespace keymaster
176