2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include "crypto/asn1.h"
21 #include "crypto/evp.h"
22 #include "crypto/x509.h"
23 #include <openssl/rsa.h>
24 #include <openssl/dsa.h>
25 #include <openssl/encoder.h>
26 #include "internal/provider.h"
28 struct X509_pubkey_st {
30 ASN1_BIT_STRING *public_key;
33 /* extra data for the callback, used by d2i_PUBKEY_ex */
38 static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
40 /* Minor tweak to operation: free up EVP_PKEY */
41 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
44 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
46 if (operation == ASN1_OP_FREE_POST) {
47 EVP_PKEY_free(pubkey->pkey);
48 } else if (operation == ASN1_OP_D2I_POST) {
49 /* Attempt to decode public key and cache in pubkey structure. */
50 EVP_PKEY_free(pubkey->pkey);
53 * Opportunistically decode the key but remove any non fatal errors
54 * from the queue. Subsequent explicit attempts to decode/use the key
55 * will return an appropriate error.
58 if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1) {
59 ERR_clear_last_mark();
67 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
68 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
69 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
70 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
72 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
73 IMPLEMENT_ASN1_DUP_FUNCTION(X509_PUBKEY)
75 /* TODO should better be called X509_PUBKEY_set1 */
76 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
78 X509_PUBKEY *pk = NULL;
86 if (pkey->ameth != NULL) {
87 if ((pk = X509_PUBKEY_new()) == NULL) {
88 X509err(X509_F_X509_PUBKEY_SET, ERR_R_MALLOC_FAILURE);
91 if (pkey->ameth->pub_encode != NULL) {
92 if (!pkey->ameth->pub_encode(pk, pkey)) {
93 X509err(X509_F_X509_PUBKEY_SET,
94 X509_R_PUBLIC_KEY_ENCODE_ERROR);
98 X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
101 } else if (evp_pkey_is_provided(pkey)) {
102 const OSSL_PROVIDER *pkprov = EVP_KEYMGMT_provider(pkey->keymgmt);
103 OSSL_LIB_CTX *libctx = ossl_provider_library_context(pkprov);
104 unsigned char *der = NULL;
106 int selection = (OSSL_KEYMGMT_SELECT_PUBLIC_KEY
107 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS);
108 OSSL_ENCODER_CTX *ectx =
109 OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, "DER", selection,
112 if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
113 const unsigned char *pder = der;
115 pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
118 OSSL_ENCODER_CTX_free(ectx);
125 X509_PUBKEY_free(*x);
126 if (!EVP_PKEY_up_ref(pkey)) {
127 X509err(X509_F_X509_PUBKEY_SET, ERR_R_INTERNAL_ERROR);
133 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
134 * going through the encoder, and for all intents and purposes, it's
135 * a perfect copy of |pkey|, just not the same instance. In that case,
136 * we could simply return early, right here.
137 * However, in the interest of being cautious leaning on paranoia, some
138 * application might very well depend on the passed |pkey| being used
139 * and none other, so we spend a few more cycles throwing away the newly
140 * created |pk->pkey| and replace it with |pkey|.
141 * TODO(3.0) Investigate if it's safe to change to simply return here
142 * if |pk->pkey != NULL|.
144 if (pk->pkey != NULL)
145 EVP_PKEY_free(pk->pkey);
151 X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
154 X509_PUBKEY_free(pk);
159 * Attempt to decode a public key.
160 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
161 * error e.g. malloc failure.
165 static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
167 EVP_PKEY *pkey = EVP_PKEY_new();
170 X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
174 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
175 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
179 if (pkey->ameth->pub_decode) {
181 * Treat any failure of pub_decode as a decode error. In
182 * future we could have different return codes for decode
183 * errors and fatal errors such as malloc failure.
185 if (!pkey->ameth->pub_decode(pkey, key))
188 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
200 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
202 EVP_PKEY *ret = NULL;
204 if (key == NULL || key->public_key == NULL)
207 if (key->pkey != NULL)
211 * When the key ASN.1 is initially parsed an attempt is made to
212 * decode the public key and cache the EVP_PKEY structure. If this
213 * operation fails the cached value will be NULL. Parsing continues
214 * to allow parsing of unknown key types or unsupported forms.
215 * We repeat the decode operation so the appropriate errors are left
218 x509_pubkey_decode(&ret, key);
219 /* If decode doesn't fail something bad happened */
221 X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
228 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
230 EVP_PKEY *ret = X509_PUBKEY_get0(key);
232 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
233 X509err(X509_F_X509_PUBKEY_GET, ERR_R_INTERNAL_ERROR);
240 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
241 * or decode as X509_PUBKEY
244 EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
245 OSSL_LIB_CTX *libctx, const char *propq)
247 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
248 EVP_PKEY *pktmp = NULL;
249 const unsigned char *q;
254 * If libctx or propq are non-NULL, we take advantage of the reuse
255 * feature. It's not generally recommended, but is safe enough for
256 * newly created structures.
258 if (libctx != NULL || propq != NULL) {
259 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
261 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
264 xpk2->libctx = libctx;
268 xpk = d2i_X509_PUBKEY(pxpk, &q, length);
271 pktmp = X509_PUBKEY_get(xpk);
272 X509_PUBKEY_free(xpk);
273 xpk2 = NULL; /* We know that xpk == xpk2 */
282 X509_PUBKEY_free(xpk2);
286 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
288 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
291 int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
297 if (a->ameth != NULL) {
298 X509_PUBKEY *xpk = NULL;
300 if ((xpk = X509_PUBKEY_new()) == NULL)
303 /* pub_encode() only encode parameters, not the key itself */
304 if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
305 xpk->pkey = (EVP_PKEY *)a;
306 ret = i2d_X509_PUBKEY(xpk, pp);
309 X509_PUBKEY_free(xpk);
310 } else if (a->keymgmt != NULL) {
311 const OSSL_PROVIDER *pkprov = EVP_KEYMGMT_provider(a->keymgmt);
312 OSSL_LIB_CTX *libctx = ossl_provider_library_context(pkprov);
313 int selection = (OSSL_KEYMGMT_SELECT_PUBLIC_KEY
314 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS);
315 OSSL_ENCODER_CTX *ctx =
316 OSSL_ENCODER_CTX_new_by_EVP_PKEY(a, "DER", selection, libctx, NULL);
317 BIO *out = BIO_new(BIO_s_mem());
320 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
322 && OSSL_ENCODER_to_bio(ctx, out)
323 && BIO_get_mem_ptr(out, &buf) > 0) {
328 *pp = (unsigned char *)buf->data;
332 memcpy(*pp, buf->data, ret);
338 OSSL_ENCODER_CTX_free(ctx);
345 * The following are equivalents but which return RSA and DSA keys
347 #ifndef OPENSSL_NO_RSA
348 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
352 const unsigned char *q;
355 pkey = d2i_PUBKEY(NULL, &q, length);
358 key = EVP_PKEY_get1_RSA(pkey);
370 int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
376 pktmp = EVP_PKEY_new();
378 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
381 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
382 ret = i2d_PUBKEY(pktmp, pp);
383 pktmp->pkey.ptr = NULL;
384 EVP_PKEY_free(pktmp);
389 #ifndef OPENSSL_NO_DSA
390 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
394 const unsigned char *q;
397 pkey = d2i_PUBKEY(NULL, &q, length);
400 key = EVP_PKEY_get1_DSA(pkey);
412 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
418 pktmp = EVP_PKEY_new();
420 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
423 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
424 ret = i2d_PUBKEY(pktmp, pp);
425 pktmp->pkey.ptr = NULL;
426 EVP_PKEY_free(pktmp);
431 #ifndef OPENSSL_NO_EC
432 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
436 const unsigned char *q;
439 pkey = d2i_PUBKEY(NULL, &q, length);
442 key = EVP_PKEY_get1_EC_KEY(pkey);
454 int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
461 if ((pktmp = EVP_PKEY_new()) == NULL) {
462 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
465 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
466 ret = i2d_PUBKEY(pktmp, pp);
467 pktmp->pkey.ptr = NULL;
468 EVP_PKEY_free(pktmp);
473 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
474 int ptype, void *pval,
475 unsigned char *penc, int penclen)
477 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
480 OPENSSL_free(pub->public_key->data);
481 pub->public_key->data = penc;
482 pub->public_key->length = penclen;
483 /* Set number of unused bits to zero */
484 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
485 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
490 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
491 const unsigned char **pk, int *ppklen,
492 X509_ALGOR **pa, const X509_PUBKEY *pub)
495 *ppkalg = pub->algor->algorithm;
497 *pk = pub->public_key->data;
498 *ppklen = pub->public_key->length;
505 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
509 return x->cert_info.key->public_key;
512 /* Returns 1 for equal, 0, for non-equal, < 0 on error */
513 int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
515 X509_ALGOR *algA, *algB;
520 if (a == NULL || b == NULL)
522 if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
523 || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
525 if (X509_ALGOR_cmp(algA, algB) != 0)
527 if ((pA = X509_PUBKEY_get0(a)) == NULL
528 || (pB = X509_PUBKEY_get0(b)) == NULL)
530 return EVP_PKEY_eq(pA, pB);
533 int X509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
534 const X509_PUBKEY *key)
537 *plibctx = key->libctx;
539 *ppropq = key->propq;