]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x_pubkey.c
ENCODER: Refactor our provider encoder implementations
[thirdparty/openssl.git] / crypto / x509 / x_pubkey.c
CommitLineData
b1322259 1/*
33388b44 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
f41ac0ee
P
10/*
11 * DSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
d02b48c6 16#include <stdio.h>
b39fc560 17#include "internal/cryptlib.h"
9d6b1ce6 18#include <openssl/asn1t.h>
f0e8ae72 19#include <openssl/x509.h>
25f2138b
DMSP
20#include "crypto/asn1.h"
21#include "crypto/evp.h"
22#include "crypto/x509.h"
3c27208f
RS
23#include <openssl/rsa.h>
24#include <openssl/dsa.h>
ece9304c 25#include <openssl/encoder.h>
d02b48c6 26
29fa0a1a
DSH
27struct X509_pubkey_st {
28 X509_ALGOR *algor;
29 ASN1_BIT_STRING *public_key;
30 EVP_PKEY *pkey;
22b81444
RL
31
32 /* extra data for the callback, used by d2i_PUBKEY_ex */
33 OPENSSL_CTX *libctx;
34 const char *propq;
29fa0a1a
DSH
35};
36
7674e923 37static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
fa0a9d71 38
9d6b1ce6 39/* Minor tweak to operation: free up EVP_PKEY */
24484759 40static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e
MC
41 void *exarg)
42{
43 if (operation == ASN1_OP_FREE_POST) {
44 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
45 EVP_PKEY_free(pubkey->pkey);
d2ec189f
DSH
46 } else if (operation == ASN1_OP_D2I_POST) {
47 /* Attempt to decode public key and cache in pubkey structure. */
48 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
49 EVP_PKEY_free(pubkey->pkey);
5dc40a83 50 pubkey->pkey = NULL;
d2ec189f 51 /*
fa0a9d71
DSH
52 * Opportunistically decode the key but remove any non fatal errors
53 * from the queue. Subsequent explicit attempts to decode/use the key
54 * will return an appropriate error.
d2ec189f
DSH
55 */
56 ERR_set_mark();
fa0a9d71
DSH
57 if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1)
58 return 0;
d2ec189f 59 ERR_pop_to_mark();
0f113f3e
MC
60 }
61 return 1;
62}
d02b48c6 63
9d6b1ce6 64ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
0f113f3e
MC
65 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
66 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
d339187b 67} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
d02b48c6 68
9d6b1ce6 69IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
a8f1aabd 70IMPLEMENT_ASN1_DUP_FUNCTION(X509_PUBKEY)
d02b48c6 71
9fdcc21f 72/* TODO should better be called X509_PUBKEY_set1 */
6b691a5c 73int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
0f113f3e
MC
74{
75 X509_PUBKEY *pk = NULL;
76
77 if (x == NULL)
26a7d938 78 return 0;
0f113f3e 79
e56ba0e1
RL
80 if (pkey == NULL)
81 goto unsupported;
0f113f3e 82
e56ba0e1
RL
83 if (pkey->ameth != NULL) {
84 if ((pk = X509_PUBKEY_new()) == NULL) {
85 X509err(X509_F_X509_PUBKEY_SET, ERR_R_MALLOC_FAILURE);
86 goto error;
87 }
88 if (pkey->ameth->pub_encode != NULL) {
0f113f3e
MC
89 if (!pkey->ameth->pub_encode(pk, pkey)) {
90 X509err(X509_F_X509_PUBKEY_SET,
91 X509_R_PUBLIC_KEY_ENCODE_ERROR);
92 goto error;
93 }
94 } else {
95 X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
96 goto error;
97 }
3c6ed955 98 } else if (pkey->keymgmt != NULL) {
e56ba0e1 99 BIO *bmem = BIO_new(BIO_s_mem());
ece9304c
RL
100 const char *encprop = OSSL_ENCODER_PUBKEY_TO_DER_PQ;
101 OSSL_ENCODER_CTX *ectx =
102 OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, encprop);
e56ba0e1 103
ece9304c 104 if (OSSL_ENCODER_to_bio(ectx, bmem)) {
e56ba0e1
RL
105 const unsigned char *der = NULL;
106 long derlen = BIO_get_mem_data(bmem, (char **)&der);
107
108 pk = d2i_X509_PUBKEY(NULL, &der, derlen);
109 }
110
ece9304c 111 OSSL_ENCODER_CTX_free(ectx);
e56ba0e1 112 BIO_free(bmem);
0f113f3e
MC
113 }
114
e56ba0e1
RL
115 if (pk == NULL)
116 goto unsupported;
117
222561fe 118 X509_PUBKEY_free(*x);
e56ba0e1
RL
119 if (!EVP_PKEY_up_ref(pkey)) {
120 X509err(X509_F_X509_PUBKEY_SET, ERR_R_INTERNAL_ERROR);
121 goto error;
122 }
0f113f3e 123 *x = pk;
a076951b
RL
124
125 /*
126 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
ece9304c 127 * going through the encoder, and for all intents and purposes, it's
a076951b
RL
128 * a perfect copy of |pkey|, just not the same instance. In that case,
129 * we could simply return early, right here.
130 * However, in the interest of being cautious leaning on paranoia, some
131 * application might very well depend on the passed |pkey| being used
132 * and none other, so we spend a few more cycles throwing away the newly
133 * created |pk->pkey| and replace it with |pkey|.
134 * TODO(3.0) Investigate if it's safe to change to simply return here
135 * if |pk->pkey != NULL|.
136 */
137 if (pk->pkey != NULL)
138 EVP_PKEY_free(pk->pkey);
139
fa0a9d71 140 pk->pkey = pkey;
e56ba0e1
RL
141 return 1;
142
143 unsupported:
144 X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
222561fe 145
0f113f3e 146 error:
222561fe 147 X509_PUBKEY_free(pk);
0f113f3e
MC
148 return 0;
149}
d02b48c6 150
fa0a9d71
DSH
151/*
152 * Attempt to decode a public key.
153 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
154 * error e.g. malloc failure.
155 */
0f113f3e 156
0f113f3e 157
7674e923 158static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
7fcdbd83 159{
fa0a9d71 160 EVP_PKEY *pkey = EVP_PKEY_new();
0f113f3e 161
fa0a9d71
DSH
162 if (pkey == NULL) {
163 X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
164 return -1;
0f113f3e
MC
165 }
166
fa0a9d71
DSH
167 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
168 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
0f113f3e
MC
169 goto error;
170 }
171
fa0a9d71
DSH
172 if (pkey->ameth->pub_decode) {
173 /*
174 * Treat any failure of pub_decode as a decode error. In
175 * future we could have different return codes for decode
176 * errors and fatal errors such as malloc failure.
177 */
178 if (!pkey->ameth->pub_decode(pkey, key)) {
179 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);
0f113f3e
MC
180 goto error;
181 }
182 } else {
fa0a9d71 183 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
184 goto error;
185 }
186
fa0a9d71
DSH
187 *ppkey = pkey;
188 return 1;
0f113f3e
MC
189
190 error:
fa0a9d71
DSH
191 EVP_PKEY_free(pkey);
192 return 0;
193}
194
7674e923 195EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
fa0a9d71
DSH
196{
197 EVP_PKEY *ret = NULL;
198
199 if (key == NULL || key->public_key == NULL)
200 return NULL;
201
202 if (key->pkey != NULL)
203 return key->pkey;
204
205 /*
206 * When the key ASN.1 is initially parsed an attempt is made to
207 * decode the public key and cache the EVP_PKEY structure. If this
208 * operation fails the cached value will be NULL. Parsing continues
209 * to allow parsing of unknown key types or unsupported forms.
210 * We repeat the decode operation so the appropriate errors are left
211 * in the queue.
212 */
213 x509_pubkey_decode(&ret, key);
214 /* If decode doesn't fail something bad happened */
215 if (ret != NULL) {
216 X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
217 EVP_PKEY_free(ret);
218 }
219
220 return NULL;
0f113f3e
MC
221}
222
7674e923 223EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
c01ff880
DSH
224{
225 EVP_PKEY *ret = X509_PUBKEY_get0(key);
e9e7b5df
BE
226
227 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
228 X509err(X509_F_X509_PUBKEY_GET, ERR_R_INTERNAL_ERROR);
229 ret = NULL;
230 }
c01ff880
DSH
231 return ret;
232}
233
0f113f3e 234/*
22b81444
RL
235 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
236 * or decode as X509_PUBKEY
52664f50
DSH
237 */
238
22b81444
RL
239EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
240 OPENSSL_CTX *libctx, const char *propq)
0f113f3e 241{
22b81444
RL
242 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
243 EVP_PKEY *pktmp = NULL;
a46c9789 244 const unsigned char *q;
12a765a5 245
a46c9789 246 q = *pp;
22b81444
RL
247
248 /*
249 * If libctx or propq are non-NULL, we take advantage of the reuse
250 * feature. It's not generally recommended, but is safe enough for
251 * newly created structures.
252 */
253 if (libctx != NULL || propq != NULL) {
254 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
255 if (xpk2 == NULL) {
256 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
257 return NULL;
258 }
259 xpk2->libctx = libctx;
260 xpk2->propq = propq;
261 pxpk = &xpk2;
262 }
263 xpk = d2i_X509_PUBKEY(pxpk, &q, length);
12a765a5 264 if (xpk == NULL)
22b81444 265 goto end;
0f113f3e
MC
266 pktmp = X509_PUBKEY_get(xpk);
267 X509_PUBKEY_free(xpk);
22b81444 268 xpk2 = NULL; /* We know that xpk == xpk2 */
12a765a5 269 if (pktmp == NULL)
22b81444 270 goto end;
a46c9789 271 *pp = q;
12a765a5 272 if (a != NULL) {
0f113f3e
MC
273 EVP_PKEY_free(*a);
274 *a = pktmp;
275 }
22b81444
RL
276 end:
277 X509_PUBKEY_free(xpk2);
0f113f3e
MC
278 return pktmp;
279}
52664f50 280
22b81444
RL
281EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
282{
283 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
284}
285
9fdcc21f 286int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
0f113f3e 287{
9fdcc21f
DO
288 int ret = -1;
289
290 if (a == NULL)
0f113f3e 291 return 0;
cdb16632
RL
292 if (a->ameth != NULL) {
293 X509_PUBKEY *xpk = NULL;
294
295 if ((xpk = X509_PUBKEY_new()) == NULL)
296 return -1;
297
298 /* pub_encode() only encode parameters, not the key itself */
299 if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
300 xpk->pkey = (EVP_PKEY *)a;
301 ret = i2d_X509_PUBKEY(xpk, pp);
302 xpk->pkey = NULL;
303 }
304 X509_PUBKEY_free(xpk);
3c6ed955 305 } else if (a->keymgmt != NULL) {
ece9304c
RL
306 const char *encprop = OSSL_ENCODER_PUBKEY_TO_DER_PQ;
307 OSSL_ENCODER_CTX *ctx =
308 OSSL_ENCODER_CTX_new_by_EVP_PKEY(a, encprop);
cdb16632
RL
309 BIO *out = BIO_new(BIO_s_mem());
310 BUF_MEM *buf = NULL;
311
312 if (ctx != NULL
313 && out != NULL
ece9304c
RL
314 && OSSL_ENCODER_CTX_get_encoder(ctx) != NULL
315 && OSSL_ENCODER_to_bio(ctx, out)
cdb16632
RL
316 && BIO_get_mem_ptr(out, &buf) > 0) {
317 ret = buf->length;
318
319 if (pp != NULL) {
320 if (*pp == NULL) {
321 *pp = (unsigned char *)buf->data;
322 buf->length = 0;
323 buf->data = NULL;
324 } else {
325 memcpy(*pp, buf->data, ret);
326 *pp += ret;
327 }
328 }
329 }
330 BIO_free(out);
ece9304c 331 OSSL_ENCODER_CTX_free(ctx);
cdb16632
RL
332 }
333
0f113f3e
MC
334 return ret;
335}
336
337/*
338 * The following are equivalents but which return RSA and DSA keys
52664f50 339 */
cf1b7d96 340#ifndef OPENSSL_NO_RSA
0f113f3e
MC
341RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
342{
343 EVP_PKEY *pkey;
344 RSA *key;
345 const unsigned char *q;
12a765a5 346
0f113f3e
MC
347 q = *pp;
348 pkey = d2i_PUBKEY(NULL, &q, length);
12a765a5 349 if (pkey == NULL)
0f113f3e
MC
350 return NULL;
351 key = EVP_PKEY_get1_RSA(pkey);
352 EVP_PKEY_free(pkey);
12a765a5 353 if (key == NULL)
0f113f3e
MC
354 return NULL;
355 *pp = q;
12a765a5 356 if (a != NULL) {
0f113f3e
MC
357 RSA_free(*a);
358 *a = key;
359 }
360 return key;
361}
52664f50 362
9fdcc21f 363int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
0f113f3e
MC
364{
365 EVP_PKEY *pktmp;
366 int ret;
367 if (!a)
368 return 0;
369 pktmp = EVP_PKEY_new();
90945fa3 370 if (pktmp == NULL) {
0f113f3e 371 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 372 return -1;
0f113f3e 373 }
9fdcc21f 374 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
0f113f3e 375 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 376 pktmp->pkey.ptr = NULL;
0f113f3e
MC
377 EVP_PKEY_free(pktmp);
378 return ret;
379}
12aefe78 380#endif
52664f50 381
cf1b7d96 382#ifndef OPENSSL_NO_DSA
0f113f3e
MC
383DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
384{
385 EVP_PKEY *pkey;
386 DSA *key;
387 const unsigned char *q;
12a765a5 388
0f113f3e
MC
389 q = *pp;
390 pkey = d2i_PUBKEY(NULL, &q, length);
12a765a5 391 if (pkey == NULL)
0f113f3e
MC
392 return NULL;
393 key = EVP_PKEY_get1_DSA(pkey);
394 EVP_PKEY_free(pkey);
12a765a5 395 if (key == NULL)
0f113f3e
MC
396 return NULL;
397 *pp = q;
12a765a5 398 if (a != NULL) {
0f113f3e
MC
399 DSA_free(*a);
400 *a = key;
401 }
402 return key;
403}
52664f50 404
9fdcc21f 405int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
0f113f3e
MC
406{
407 EVP_PKEY *pktmp;
408 int ret;
409 if (!a)
410 return 0;
411 pktmp = EVP_PKEY_new();
90945fa3 412 if (pktmp == NULL) {
0f113f3e 413 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 414 return -1;
0f113f3e 415 }
9fdcc21f 416 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
0f113f3e 417 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 418 pktmp->pkey.ptr = NULL;
0f113f3e
MC
419 EVP_PKEY_free(pktmp);
420 return ret;
421}
4d94ae00
BM
422#endif
423
14a7cfb3 424#ifndef OPENSSL_NO_EC
6343829a 425EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
0f113f3e
MC
426{
427 EVP_PKEY *pkey;
428 EC_KEY *key;
429 const unsigned char *q;
12a765a5 430
0f113f3e
MC
431 q = *pp;
432 pkey = d2i_PUBKEY(NULL, &q, length);
12a765a5 433 if (pkey == NULL)
26a7d938 434 return NULL;
0f113f3e
MC
435 key = EVP_PKEY_get1_EC_KEY(pkey);
436 EVP_PKEY_free(pkey);
12a765a5 437 if (key == NULL)
26a7d938 438 return NULL;
0f113f3e 439 *pp = q;
12a765a5 440 if (a != NULL) {
0f113f3e
MC
441 EC_KEY_free(*a);
442 *a = key;
443 }
26a7d938 444 return key;
0f113f3e 445}
4d94ae00 446
9fdcc21f 447int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
0f113f3e
MC
448{
449 EVP_PKEY *pktmp;
450 int ret;
12a765a5
RS
451
452 if (a == NULL)
26a7d938 453 return 0;
0f113f3e
MC
454 if ((pktmp = EVP_PKEY_new()) == NULL) {
455 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 456 return -1;
0f113f3e 457 }
9fdcc21f 458 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
0f113f3e 459 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 460 pktmp->pkey.ptr = NULL;
0f113f3e 461 EVP_PKEY_free(pktmp);
26a7d938 462 return ret;
0f113f3e 463}
12aefe78 464#endif
448be743
DSH
465
466int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
0f113f3e
MC
467 int ptype, void *pval,
468 unsigned char *penc, int penclen)
469{
470 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
471 return 0;
472 if (penc) {
b548a1f1 473 OPENSSL_free(pub->public_key->data);
0f113f3e
MC
474 pub->public_key->data = penc;
475 pub->public_key->length = penclen;
476 /* Set number of unused bits to zero */
477 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
478 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
479 }
480 return 1;
481}
448be743
DSH
482
483int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
0f113f3e 484 const unsigned char **pk, int *ppklen,
7674e923 485 X509_ALGOR **pa, const X509_PUBKEY *pub)
0f113f3e
MC
486{
487 if (ppkalg)
488 *ppkalg = pub->algor->algorithm;
489 if (pk) {
490 *pk = pub->public_key->data;
491 *ppklen = pub->public_key->length;
492 }
493 if (pa)
494 *pa = pub->algor;
495 return 1;
496}
29fa0a1a
DSH
497
498ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
499{
500 if (x == NULL)
501 return NULL;
502 return x->cert_info.key->public_key;
503}
93f99b68
DDO
504
505/* Returns 1 for equal, 0, for non-equal, < 0 on error */
506int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
507{
508 X509_ALGOR *algA, *algB;
509 EVP_PKEY *pA, *pB;
510
511 if (a == b)
512 return 1;
513 if (a == NULL || b == NULL)
514 return 0;
515 if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
516 || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
517 return -2;
518 if (X509_ALGOR_cmp(algA, algB) != 0)
519 return 0;
520 if ((pA = X509_PUBKEY_get0(a)) == NULL
521 || (pB = X509_PUBKEY_get0(b)) == NULL)
522 return -2;
c74aaa39 523 return EVP_PKEY_eq(pA, pB);
93f99b68 524}
22b81444
RL
525
526int X509_PUBKEY_get0_libctx(OPENSSL_CTX **plibctx, const char **ppropq,
527 const X509_PUBKEY *key)
528{
529 if (plibctx)
530 *plibctx = key->libctx;
531 if (ppropq)
532 *ppropq = key->propq;
533 return 1;
534}