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