]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x_pubkey.c
ENCODER / DECODER: Add functions to encode/decode to/from a buffer
[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>
97bb8dff 26#include "internal/provider.h"
d02b48c6 27
29fa0a1a
DSH
28struct X509_pubkey_st {
29 X509_ALGOR *algor;
30 ASN1_BIT_STRING *public_key;
31 EVP_PKEY *pkey;
22b81444
RL
32
33 /* extra data for the callback, used by d2i_PUBKEY_ex */
34 OPENSSL_CTX *libctx;
35 const char *propq;
29fa0a1a
DSH
36};
37
7674e923 38static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
fa0a9d71 39
9d6b1ce6 40/* Minor tweak to operation: free up EVP_PKEY */
24484759 41static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e
MC
42 void *exarg)
43{
66066e1b
DDO
44 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
45
0f113f3e 46 if (operation == ASN1_OP_FREE_POST) {
0f113f3e 47 EVP_PKEY_free(pubkey->pkey);
d2ec189f
DSH
48 } else if (operation == ASN1_OP_D2I_POST) {
49 /* Attempt to decode public key and cache in pubkey structure. */
d2ec189f 50 EVP_PKEY_free(pubkey->pkey);
5dc40a83 51 pubkey->pkey = NULL;
d2ec189f 52 /*
fa0a9d71
DSH
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.
d2ec189f
DSH
56 */
57 ERR_set_mark();
66066e1b
DDO
58 if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1) {
59 ERR_clear_last_mark();
fa0a9d71 60 return 0;
66066e1b 61 }
d2ec189f 62 ERR_pop_to_mark();
0f113f3e
MC
63 }
64 return 1;
65}
d02b48c6 66
9d6b1ce6 67ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
0f113f3e
MC
68 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
69 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
d339187b 70} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
d02b48c6 71
9d6b1ce6 72IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
a8f1aabd 73IMPLEMENT_ASN1_DUP_FUNCTION(X509_PUBKEY)
d02b48c6 74
9fdcc21f 75/* TODO should better be called X509_PUBKEY_set1 */
6b691a5c 76int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
0f113f3e
MC
77{
78 X509_PUBKEY *pk = NULL;
79
80 if (x == NULL)
26a7d938 81 return 0;
0f113f3e 82
e56ba0e1
RL
83 if (pkey == NULL)
84 goto unsupported;
0f113f3e 85
e56ba0e1
RL
86 if (pkey->ameth != NULL) {
87 if ((pk = X509_PUBKEY_new()) == NULL) {
88 X509err(X509_F_X509_PUBKEY_SET, ERR_R_MALLOC_FAILURE);
89 goto error;
90 }
91 if (pkey->ameth->pub_encode != NULL) {
0f113f3e
MC
92 if (!pkey->ameth->pub_encode(pk, pkey)) {
93 X509err(X509_F_X509_PUBKEY_SET,
94 X509_R_PUBLIC_KEY_ENCODE_ERROR);
95 goto error;
96 }
97 } else {
98 X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
99 goto error;
100 }
3c6ed955 101 } else if (pkey->keymgmt != NULL) {
97bb8dff
RL
102 const OSSL_PROVIDER *pkprov = EVP_KEYMGMT_provider(pkey->keymgmt);
103 OPENSSL_CTX *libctx = ossl_provider_library_context(pkprov);
e56ba0e1 104 BIO *bmem = BIO_new(BIO_s_mem());
97bb8dff
RL
105 int selection = (OSSL_KEYMGMT_SELECT_PUBLIC_KEY
106 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS);
ece9304c 107 OSSL_ENCODER_CTX *ectx =
97bb8dff
RL
108 OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, "DER", selection,
109 libctx, NULL);
e56ba0e1 110
ece9304c 111 if (OSSL_ENCODER_to_bio(ectx, bmem)) {
e56ba0e1
RL
112 const unsigned char *der = NULL;
113 long derlen = BIO_get_mem_data(bmem, (char **)&der);
114
115 pk = d2i_X509_PUBKEY(NULL, &der, derlen);
116 }
117
ece9304c 118 OSSL_ENCODER_CTX_free(ectx);
e56ba0e1 119 BIO_free(bmem);
0f113f3e
MC
120 }
121
e56ba0e1
RL
122 if (pk == NULL)
123 goto unsupported;
124
222561fe 125 X509_PUBKEY_free(*x);
e56ba0e1
RL
126 if (!EVP_PKEY_up_ref(pkey)) {
127 X509err(X509_F_X509_PUBKEY_SET, ERR_R_INTERNAL_ERROR);
128 goto error;
129 }
0f113f3e 130 *x = pk;
a076951b
RL
131
132 /*
133 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
ece9304c 134 * going through the encoder, and for all intents and purposes, it's
a076951b
RL
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|.
143 */
144 if (pk->pkey != NULL)
145 EVP_PKEY_free(pk->pkey);
146
fa0a9d71 147 pk->pkey = pkey;
e56ba0e1
RL
148 return 1;
149
150 unsupported:
151 X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
222561fe 152
0f113f3e 153 error:
222561fe 154 X509_PUBKEY_free(pk);
0f113f3e
MC
155 return 0;
156}
d02b48c6 157
fa0a9d71
DSH
158/*
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.
162 */
0f113f3e 163
0f113f3e 164
7674e923 165static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
7fcdbd83 166{
fa0a9d71 167 EVP_PKEY *pkey = EVP_PKEY_new();
0f113f3e 168
fa0a9d71
DSH
169 if (pkey == NULL) {
170 X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
171 return -1;
0f113f3e
MC
172 }
173
fa0a9d71
DSH
174 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
175 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
0f113f3e
MC
176 goto error;
177 }
178
fa0a9d71
DSH
179 if (pkey->ameth->pub_decode) {
180 /*
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.
184 */
66066e1b 185 if (!pkey->ameth->pub_decode(pkey, key))
0f113f3e 186 goto error;
0f113f3e 187 } else {
fa0a9d71 188 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
189 goto error;
190 }
191
fa0a9d71
DSH
192 *ppkey = pkey;
193 return 1;
0f113f3e
MC
194
195 error:
fa0a9d71
DSH
196 EVP_PKEY_free(pkey);
197 return 0;
198}
199
7674e923 200EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
fa0a9d71
DSH
201{
202 EVP_PKEY *ret = NULL;
203
204 if (key == NULL || key->public_key == NULL)
205 return NULL;
206
207 if (key->pkey != NULL)
208 return key->pkey;
209
210 /*
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
216 * in the queue.
217 */
218 x509_pubkey_decode(&ret, key);
219 /* If decode doesn't fail something bad happened */
220 if (ret != NULL) {
221 X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
222 EVP_PKEY_free(ret);
223 }
224
225 return NULL;
0f113f3e
MC
226}
227
7674e923 228EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
c01ff880
DSH
229{
230 EVP_PKEY *ret = X509_PUBKEY_get0(key);
e9e7b5df
BE
231
232 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
233 X509err(X509_F_X509_PUBKEY_GET, ERR_R_INTERNAL_ERROR);
234 ret = NULL;
235 }
c01ff880
DSH
236 return ret;
237}
238
0f113f3e 239/*
22b81444
RL
240 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
241 * or decode as X509_PUBKEY
52664f50
DSH
242 */
243
22b81444
RL
244EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
245 OPENSSL_CTX *libctx, const char *propq)
0f113f3e 246{
22b81444
RL
247 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
248 EVP_PKEY *pktmp = NULL;
a46c9789 249 const unsigned char *q;
12a765a5 250
a46c9789 251 q = *pp;
22b81444
RL
252
253 /*
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.
257 */
258 if (libctx != NULL || propq != NULL) {
259 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
260 if (xpk2 == NULL) {
261 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
262 return NULL;
263 }
264 xpk2->libctx = libctx;
265 xpk2->propq = propq;
266 pxpk = &xpk2;
267 }
268 xpk = d2i_X509_PUBKEY(pxpk, &q, length);
12a765a5 269 if (xpk == NULL)
22b81444 270 goto end;
0f113f3e
MC
271 pktmp = X509_PUBKEY_get(xpk);
272 X509_PUBKEY_free(xpk);
22b81444 273 xpk2 = NULL; /* We know that xpk == xpk2 */
12a765a5 274 if (pktmp == NULL)
22b81444 275 goto end;
a46c9789 276 *pp = q;
12a765a5 277 if (a != NULL) {
0f113f3e
MC
278 EVP_PKEY_free(*a);
279 *a = pktmp;
280 }
22b81444
RL
281 end:
282 X509_PUBKEY_free(xpk2);
0f113f3e
MC
283 return pktmp;
284}
52664f50 285
22b81444
RL
286EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
287{
288 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
289}
290
9fdcc21f 291int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
0f113f3e 292{
9fdcc21f
DO
293 int ret = -1;
294
295 if (a == NULL)
0f113f3e 296 return 0;
cdb16632
RL
297 if (a->ameth != NULL) {
298 X509_PUBKEY *xpk = NULL;
299
300 if ((xpk = X509_PUBKEY_new()) == NULL)
301 return -1;
302
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);
307 xpk->pkey = NULL;
308 }
309 X509_PUBKEY_free(xpk);
3c6ed955 310 } else if (a->keymgmt != NULL) {
97bb8dff
RL
311 const OSSL_PROVIDER *pkprov = EVP_KEYMGMT_provider(a->keymgmt);
312 OPENSSL_CTX *libctx = ossl_provider_library_context(pkprov);
313 int selection = (OSSL_KEYMGMT_SELECT_PUBLIC_KEY
314 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS);
ece9304c 315 OSSL_ENCODER_CTX *ctx =
97bb8dff 316 OSSL_ENCODER_CTX_new_by_EVP_PKEY(a, "DER", selection, libctx, NULL);
cdb16632
RL
317 BIO *out = BIO_new(BIO_s_mem());
318 BUF_MEM *buf = NULL;
319
97bb8dff 320 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
cdb16632 321 && out != NULL
ece9304c 322 && OSSL_ENCODER_to_bio(ctx, out)
cdb16632
RL
323 && BIO_get_mem_ptr(out, &buf) > 0) {
324 ret = buf->length;
325
326 if (pp != NULL) {
327 if (*pp == NULL) {
328 *pp = (unsigned char *)buf->data;
329 buf->length = 0;
330 buf->data = NULL;
331 } else {
332 memcpy(*pp, buf->data, ret);
333 *pp += ret;
334 }
335 }
336 }
337 BIO_free(out);
ece9304c 338 OSSL_ENCODER_CTX_free(ctx);
cdb16632
RL
339 }
340
0f113f3e
MC
341 return ret;
342}
343
344/*
345 * The following are equivalents but which return RSA and DSA keys
52664f50 346 */
cf1b7d96 347#ifndef OPENSSL_NO_RSA
0f113f3e
MC
348RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
349{
350 EVP_PKEY *pkey;
351 RSA *key;
352 const unsigned char *q;
12a765a5 353
0f113f3e
MC
354 q = *pp;
355 pkey = d2i_PUBKEY(NULL, &q, length);
12a765a5 356 if (pkey == NULL)
0f113f3e
MC
357 return NULL;
358 key = EVP_PKEY_get1_RSA(pkey);
359 EVP_PKEY_free(pkey);
12a765a5 360 if (key == NULL)
0f113f3e
MC
361 return NULL;
362 *pp = q;
12a765a5 363 if (a != NULL) {
0f113f3e
MC
364 RSA_free(*a);
365 *a = key;
366 }
367 return key;
368}
52664f50 369
9fdcc21f 370int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
0f113f3e
MC
371{
372 EVP_PKEY *pktmp;
373 int ret;
374 if (!a)
375 return 0;
376 pktmp = EVP_PKEY_new();
90945fa3 377 if (pktmp == NULL) {
0f113f3e 378 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 379 return -1;
0f113f3e 380 }
9fdcc21f 381 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
0f113f3e 382 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 383 pktmp->pkey.ptr = NULL;
0f113f3e
MC
384 EVP_PKEY_free(pktmp);
385 return ret;
386}
12aefe78 387#endif
52664f50 388
cf1b7d96 389#ifndef OPENSSL_NO_DSA
0f113f3e
MC
390DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
391{
392 EVP_PKEY *pkey;
393 DSA *key;
394 const unsigned char *q;
12a765a5 395
0f113f3e
MC
396 q = *pp;
397 pkey = d2i_PUBKEY(NULL, &q, length);
12a765a5 398 if (pkey == NULL)
0f113f3e
MC
399 return NULL;
400 key = EVP_PKEY_get1_DSA(pkey);
401 EVP_PKEY_free(pkey);
12a765a5 402 if (key == NULL)
0f113f3e
MC
403 return NULL;
404 *pp = q;
12a765a5 405 if (a != NULL) {
0f113f3e
MC
406 DSA_free(*a);
407 *a = key;
408 }
409 return key;
410}
52664f50 411
9fdcc21f 412int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
0f113f3e
MC
413{
414 EVP_PKEY *pktmp;
415 int ret;
416 if (!a)
417 return 0;
418 pktmp = EVP_PKEY_new();
90945fa3 419 if (pktmp == NULL) {
0f113f3e 420 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 421 return -1;
0f113f3e 422 }
9fdcc21f 423 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
0f113f3e 424 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 425 pktmp->pkey.ptr = NULL;
0f113f3e
MC
426 EVP_PKEY_free(pktmp);
427 return ret;
428}
4d94ae00
BM
429#endif
430
14a7cfb3 431#ifndef OPENSSL_NO_EC
6343829a 432EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
0f113f3e
MC
433{
434 EVP_PKEY *pkey;
435 EC_KEY *key;
436 const unsigned char *q;
12a765a5 437
0f113f3e
MC
438 q = *pp;
439 pkey = d2i_PUBKEY(NULL, &q, length);
12a765a5 440 if (pkey == NULL)
26a7d938 441 return NULL;
0f113f3e
MC
442 key = EVP_PKEY_get1_EC_KEY(pkey);
443 EVP_PKEY_free(pkey);
12a765a5 444 if (key == NULL)
26a7d938 445 return NULL;
0f113f3e 446 *pp = q;
12a765a5 447 if (a != NULL) {
0f113f3e
MC
448 EC_KEY_free(*a);
449 *a = key;
450 }
26a7d938 451 return key;
0f113f3e 452}
4d94ae00 453
9fdcc21f 454int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
0f113f3e
MC
455{
456 EVP_PKEY *pktmp;
457 int ret;
12a765a5
RS
458
459 if (a == NULL)
26a7d938 460 return 0;
0f113f3e
MC
461 if ((pktmp = EVP_PKEY_new()) == NULL) {
462 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 463 return -1;
0f113f3e 464 }
9fdcc21f 465 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
0f113f3e 466 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 467 pktmp->pkey.ptr = NULL;
0f113f3e 468 EVP_PKEY_free(pktmp);
26a7d938 469 return ret;
0f113f3e 470}
12aefe78 471#endif
448be743
DSH
472
473int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
0f113f3e
MC
474 int ptype, void *pval,
475 unsigned char *penc, int penclen)
476{
477 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
478 return 0;
479 if (penc) {
b548a1f1 480 OPENSSL_free(pub->public_key->data);
0f113f3e
MC
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;
486 }
487 return 1;
488}
448be743
DSH
489
490int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
0f113f3e 491 const unsigned char **pk, int *ppklen,
7674e923 492 X509_ALGOR **pa, const X509_PUBKEY *pub)
0f113f3e
MC
493{
494 if (ppkalg)
495 *ppkalg = pub->algor->algorithm;
496 if (pk) {
497 *pk = pub->public_key->data;
498 *ppklen = pub->public_key->length;
499 }
500 if (pa)
501 *pa = pub->algor;
502 return 1;
503}
29fa0a1a
DSH
504
505ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
506{
507 if (x == NULL)
508 return NULL;
509 return x->cert_info.key->public_key;
510}
93f99b68
DDO
511
512/* Returns 1 for equal, 0, for non-equal, < 0 on error */
513int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
514{
515 X509_ALGOR *algA, *algB;
516 EVP_PKEY *pA, *pB;
517
518 if (a == b)
519 return 1;
520 if (a == NULL || b == NULL)
521 return 0;
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)
524 return -2;
525 if (X509_ALGOR_cmp(algA, algB) != 0)
526 return 0;
527 if ((pA = X509_PUBKEY_get0(a)) == NULL
528 || (pB = X509_PUBKEY_get0(b)) == NULL)
529 return -2;
c74aaa39 530 return EVP_PKEY_eq(pA, pB);
93f99b68 531}
22b81444
RL
532
533int X509_PUBKEY_get0_libctx(OPENSSL_CTX **plibctx, const char **ppropq,
534 const X509_PUBKEY *key)
535{
536 if (plibctx)
537 *plibctx = key->libctx;
538 if (ppropq)
539 *ppropq = key->propq;
540 return 1;
541}