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