From: Lukasz Pawelczyk Date: Mon, 9 May 2016 13:50:58 +0000 (+0200) Subject: Add support for importing public keys from X509 certificate X-Git-Tag: accepted/tizen/common/20160810.161523~165 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=92eb4e64e351bbdf04ad7049fc5f970b51a36d8a;p=platform%2Fcore%2Fsecurity%2Fyaca.git Add support for importing public keys from X509 certificate Fix a bug with X509 not being freed and pkey not being freed in case of an error. Change-Id: I063f606b928c679c452347cc18db793fc066c509 --- diff --git a/src/key.c b/src/key.c index 3327ba4..db8b6c4 100644 --- a/src/key.c +++ b/src/key.c @@ -221,6 +221,7 @@ int import_evp(yaca_key_h *key, assert(data != NULL); assert(data_len != 0); + int ret; BIO *src = NULL; EVP_PKEY *pkey = NULL; bool private; @@ -257,6 +258,15 @@ int import_evp(yaca_key_h *key, pkey = PEM_read_bio_PUBKEY(src, NULL, NULL, NULL); private = false; } + + if (pkey == NULL) { + BIO_reset(src); + X509 *x509 = PEM_read_bio_X509(src, NULL, NULL, NULL); + if (x509 != NULL) + pkey = X509_get_pubkey(x509); + private = false; + X509_free(x509); + } } /* Possible DER */ else { @@ -292,21 +302,31 @@ int import_evp(yaca_key_h *key, break; default: - return YACA_ERROR_INVALID_ARGUMENT; + ret = YACA_ERROR_INVALID_ARGUMENT; + goto free; } - if (type != key_type) - return YACA_ERROR_INVALID_ARGUMENT; + if (type != key_type) { + ret = YACA_ERROR_INVALID_ARGUMENT; + goto free; + } nk = yaca_zalloc(sizeof(struct yaca_key_evp_s)); - if (nk == NULL) - return YACA_ERROR_OUT_OF_MEMORY; + if (nk == NULL) { + ret = YACA_ERROR_OUT_OF_MEMORY; + goto free; + } nk->evp = pkey; *key = (yaca_key_h)nk; (*key)->type = type; - return 0; + pkey = NULL; + ret = 0; + +free: + EVP_PKEY_free(pkey); + return ret; } int export_simple_raw(struct yaca_key_simple_s *simple_key,