1515 ** limitations under the License.
1616 */
1717
18- #include < CommonUtils.h>
19- # include < regex.h >
18+ #include " CommonUtils.h"
19+
2020#include < regex.h>
21+
22+ #include < android-base/logging.h>
2123#include < android-base/properties.h>
22- #include < openssl/evp.h>
23- #include < openssl/rsa.h>
24- #include < openssl/ec.h>
24+
2525#include < openssl/bn.h>
26+ #include < openssl/ec.h>
27+ #include < openssl/evp.h>
2628#include < openssl/nid.h>
27- #include < android-base/logging.h>
29+ #include < openssl/rsa.h>
30+
31+ #include < keymaster/km_openssl/ec_key.h>
2832#include < keymaster/km_openssl/openssl_err.h>
2933#include < keymaster/km_openssl/openssl_utils.h>
3034#include < keymaster/km_openssl/rsa_key.h>
31- #include < keymaster/km_openssl/ec_key.h>
32- #include < android-base/logging.h>
3335
3436#define TAG_SEQUENCE 0x30
3537#define LENGTH_MASK 0x80
@@ -160,21 +162,22 @@ ErrorCode getEcCurve(const EC_GROUP *group, EcCurve& ecCurve) {
160162ErrorCode ecRawKeyFromPKCS8 (const std::vector<uint8_t >& pkcs8Blob, std::vector<uint8_t >& secret, std::vector<uint8_t >&
161163publicKey, EcCurve& ecCurve) {
162164 ErrorCode errorCode = ErrorCode::INVALID_KEY_BLOB;
163- EVP_PKEY *pkey = nullptr ;
164165 const uint8_t *data = pkcs8Blob.data ();
165166
166- d2i_PrivateKey (EVP_PKEY_EC, &pkey, &data, pkcs8Blob.size ());
167- if (!pkey) {
167+ EVP_PKEY* evpKey = d2i_PrivateKey (EVP_PKEY_EC, nullptr /* pkey */ , &data,
168+ pkcs8Blob.size ());
169+ if (!evpKey) {
168170 return legacy_enum_conversion (TranslateLastOpenSslError ());
169171 }
172+ UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey (evpKey);
170173
171- UniquePtr<EC_KEY, EC_KEY_Delete> ec_key (EVP_PKEY_get1_EC_KEY (pkey));
174+ UniquePtr<EC_KEY, EC_KEY_Delete> ec_key (EVP_PKEY_get1_EC_KEY (pkey. get () ));
172175 if (!ec_key.get ())
173176 return legacy_enum_conversion (TranslateLastOpenSslError ());
174177
175178 // Get EC Group
176179 const EC_GROUP *group = EC_KEY_get0_group (ec_key.get ());
177- if (group == NULL )
180+ if (group == nullptr )
178181 return errorCode;
179182
180183 if (ErrorCode::OK != (errorCode = getEcCurve (group, ecCurve))) {
@@ -183,57 +186,83 @@ publicKey, EcCurve& ecCurve) {
183186
184187 // Extract private key.
185188 const BIGNUM *privBn = EC_KEY_get0_private_key (ec_key.get ());
186- int privKeyLen = BN_num_bytes (privBn);
187- std::unique_ptr<uint8_t []> privKey (new uint8_t [privKeyLen]);
188- BN_bn2bin (privBn, privKey.get ());
189- secret.insert (secret.begin (), privKey.get (), privKey.get ()+privKeyLen);
189+ if (privBn == nullptr ) {
190+ return errorCode;
191+ }
192+ // Note that this may return fewer than 32 bytes so pad with zeroes since we
193+ // want to always return 32 bytes.
194+ size_t numBytes = BN_num_bytes (privBn);
195+ if (numBytes > 32 ) {
196+ LOG (ERROR) << " Size is " << numBytes << " , expected this to be 32 or less" ;
197+ return errorCode;
198+ }
199+ secret.resize (32 );
200+ for (size_t n = 0 ; n < 32 - numBytes; n++) {
201+ secret[n] = 0x00 ;
202+ }
203+ BN_bn2bin (privBn, secret.data () + 32 - numBytes);
190204
191205 // Extract public key.
192206 const EC_POINT *point = EC_KEY_get0_public_key (ec_key.get ());
193- int pubKeyLen=0 ;
194- pubKeyLen = EC_POINT_point2oct (group, point, POINT_CONVERSION_UNCOMPRESSED, NULL , 0 , NULL );
195- std::unique_ptr<uint8_t []> pubKey (new uint8_t [pubKeyLen]);
196- EC_POINT_point2oct (group, point, POINT_CONVERSION_UNCOMPRESSED, pubKey.get (), pubKeyLen, NULL );
197- publicKey.insert (publicKey.begin (), pubKey.get (), pubKey.get ()+pubKeyLen);
207+ int size = EC_POINT_point2oct (group, point, POINT_CONVERSION_UNCOMPRESSED, nullptr , 0 ,
208+ nullptr );
209+ if (size == 0 ) {
210+ LOG (ERROR) << " Error generating public key encoding" ;
211+ return errorCode;
212+ }
213+
214+ publicKey.resize (size);
215+ EC_POINT_point2oct (group, point, POINT_CONVERSION_UNCOMPRESSED, publicKey.data (),
216+ publicKey.size (), nullptr );
198217
199- EVP_PKEY_free (pkey);
200218 return ErrorCode::OK;
201219}
202220
203221ErrorCode rsaRawKeyFromPKCS8 (const std::vector<uint8_t >& pkcs8Blob, std::vector<uint8_t >& privateExp, std::vector<uint8_t >&
204222pubModulus) {
205223 ErrorCode errorCode = ErrorCode::INVALID_KEY_BLOB;
206- const BIGNUM *n=NULL , *e=NULL , *d=NULL ;
207- EVP_PKEY *pkey = nullptr ;
224+ const BIGNUM *n = nullptr , *d = nullptr ;
208225 const uint8_t *data = pkcs8Blob.data ();
209226
210- d2i_PrivateKey (EVP_PKEY_RSA, &pkey, &data, pkcs8Blob.size ());
211- if (!pkey) {
227+ EVP_PKEY* evpKey = d2i_PrivateKey (EVP_PKEY_RSA, nullptr /* pkey */ , &data,
228+ pkcs8Blob.size ());
229+ if (!evpKey) {
212230 return legacy_enum_conversion (TranslateLastOpenSslError ());
213231 }
232+ UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey (evpKey);
214233
215- UniquePtr<RSA, RSA_Delete> rsa_key (EVP_PKEY_get1_RSA (pkey));
234+ UniquePtr<RSA, RSA_Delete> rsa_key (EVP_PKEY_get1_RSA (pkey. get () ));
216235 if (!rsa_key.get ()) {
217236 return legacy_enum_conversion (TranslateLastOpenSslError ());
218237 }
219238
220- RSA_get0_key (rsa_key.get (), &n, &e , &d);
221- if (d != NULL && n != NULL ) {
239+ RSA_get0_key (rsa_key.get (), &n, nullptr , &d);
240+ if (d != nullptr && n != nullptr ) {
222241 /* private exponent */
223242 int privExpLen = BN_num_bytes (d);
224- std::unique_ptr<uint8_t []> privExp (new uint8_t [privExpLen]);
225- BN_bn2bin (d, privExp.get ());
243+ if (privExpLen > 256 ) {
244+ LOG (ERROR) << " Size is " << privExpLen << " , expected this to be 256 or less" ;
245+ return errorCode;
246+ }
247+ privateExp.resize (256 );
248+ for (size_t n = 0 ; n < 256 - privExpLen; n++) {
249+ privateExp[n] = 0x00 ;
250+ }
251+ BN_bn2bin (d, privateExp.data () + 256 - privExpLen);
226252 /* public modulus */
227253 int pubModLen = BN_num_bytes (n);
228- std::unique_ptr<uint8_t []> pubMod (new uint8_t [pubModLen]);
229- BN_bn2bin (n, pubMod.get ());
230-
231- privateExp.insert (privateExp.begin (), privExp.get (), privExp.get ()+privExpLen);
232- pubModulus.insert (pubModulus.begin (), pubMod.get (), pubMod.get ()+pubModLen);
254+ if (pubModLen > 256 ) {
255+ LOG (ERROR) << " Size is " << pubModLen << " , expected this to be 256 or less" ;
256+ return errorCode;
257+ }
258+ pubModulus.resize (256 );
259+ for (size_t n = 0 ; n < 256 - pubModLen; n++) {
260+ pubModulus[n] = 0x00 ;
261+ }
262+ BN_bn2bin (n, pubModulus.data () + 256 - pubModLen);
233263 } else {
234264 return errorCode;
235265 }
236- EVP_PKEY_free (pkey);
237266 return ErrorCode::OK;
238267}
239268
0 commit comments