]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
4333b89f | 2 | * Copyright 1995-2021 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> | |
10315851 | 25 | #include <openssl/decoder.h> |
ece9304c | 26 | #include <openssl/encoder.h> |
97bb8dff | 27 | #include "internal/provider.h" |
10315851 | 28 | #include "internal/sizes.h" |
d02b48c6 | 29 | |
29fa0a1a DSH |
30 | struct X509_pubkey_st { |
31 | X509_ALGOR *algor; | |
32 | ASN1_BIT_STRING *public_key; | |
10315851 | 33 | |
29fa0a1a | 34 | EVP_PKEY *pkey; |
22b81444 RL |
35 | |
36 | /* extra data for the callback, used by d2i_PUBKEY_ex */ | |
b4250010 | 37 | OSSL_LIB_CTX *libctx; |
637dce3c | 38 | char *propq; |
10315851 RL |
39 | |
40 | /* Flag to force legacy keys */ | |
41 | unsigned int flag_force_legacy : 1; | |
29fa0a1a DSH |
42 | }; |
43 | ||
7674e923 | 44 | static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key); |
fa0a9d71 | 45 | |
637dce3c SL |
46 | static int x509_pubkey_set0_libctx(X509_PUBKEY *x, OSSL_LIB_CTX *libctx, |
47 | const char *propq) | |
48 | { | |
49 | if (x != NULL) { | |
50 | x->libctx = libctx; | |
51 | OPENSSL_free(x->propq); | |
52 | x->propq = NULL; | |
53 | if (propq != NULL) { | |
54 | x->propq = OPENSSL_strdup(propq); | |
55 | if (x->propq == NULL) | |
56 | return 0; | |
57 | } | |
58 | } | |
59 | return 1; | |
60 | } | |
61 | ||
10315851 RL |
62 | ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = { |
63 | ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR), | |
64 | ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING) | |
65 | } static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL) | |
66 | ||
67 | static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e | 68 | { |
66066e1b DDO |
69 | X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; |
70 | ||
10315851 RL |
71 | X509_ALGOR_free(pubkey->algor); |
72 | ASN1_BIT_STRING_free(pubkey->public_key); | |
73 | EVP_PKEY_free(pubkey->pkey); | |
74 | OPENSSL_free(pubkey); | |
75 | *pval = NULL; | |
76 | } | |
77 | ||
78 | static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
79 | { | |
80 | X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; | |
81 | ||
82 | return (pubkey->algor != NULL | |
83 | || (pubkey->algor = X509_ALGOR_new()) != NULL) | |
84 | && (pubkey->public_key != NULL | |
85 | || (pubkey->public_key = ASN1_BIT_STRING_new()) != NULL); | |
86 | } | |
87 | ||
88 | static int x509_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
89 | { | |
90 | X509_PUBKEY *ret; | |
91 | ||
92 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL | |
93 | || !x509_pubkey_ex_populate((ASN1_VALUE **)&ret, NULL)) { | |
94 | x509_pubkey_ex_free((ASN1_VALUE **)&ret, NULL); | |
95 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
96 | } else { | |
97 | *pval = (ASN1_VALUE *)ret; | |
98 | } | |
99 | ||
100 | return ret != NULL; | |
101 | } | |
102 | ||
103 | static int x509_pubkey_ex_d2i(ASN1_VALUE **pval, | |
104 | const unsigned char **in, long len, | |
105 | const ASN1_ITEM *it, int tag, int aclass, | |
106 | char opt, ASN1_TLC *ctx) | |
107 | { | |
108 | const unsigned char *in_saved = *in; | |
109 | X509_PUBKEY *pubkey; | |
110 | int ret; | |
111 | OSSL_DECODER_CTX *dctx = NULL; | |
112 | ||
113 | if (*pval == NULL && !x509_pubkey_ex_new(pval, it)) | |
114 | return 0; | |
115 | if (!x509_pubkey_ex_populate(pval, NULL)) { | |
116 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
117 | return 0; | |
118 | } | |
119 | ||
120 | /* This ensures that |*in| advances properly no matter what */ | |
121 | if ((ret = ASN1_item_ex_d2i(pval, in, len, | |
122 | ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), | |
123 | tag, aclass, opt, ctx)) <= 0) | |
124 | return ret; | |
125 | ||
126 | pubkey = (X509_PUBKEY *)*pval; | |
127 | EVP_PKEY_free(pubkey->pkey); | |
128 | pubkey->pkey = NULL; | |
129 | ||
130 | /* | |
131 | * Opportunistically decode the key but remove any non fatal errors | |
132 | * from the queue. Subsequent explicit attempts to decode/use the key | |
133 | * will return an appropriate error. | |
134 | */ | |
135 | ERR_set_mark(); | |
136 | ||
137 | /* | |
138 | * Try to decode with legacy method first. This ensures that engines | |
139 | * aren't overriden by providers. | |
140 | */ | |
141 | if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) { | |
142 | /* -1 indicates a fatal error, like malloc failure */ | |
143 | ERR_clear_last_mark(); | |
144 | goto end; | |
145 | } | |
146 | ||
147 | /* Try to decode it into an EVP_PKEY with OSSL_DECODER */ | |
148 | if (ret <= 0 && !pubkey->flag_force_legacy) { | |
149 | const unsigned char *p = in_saved; | |
150 | char txtoidname[OSSL_MAX_NAME_SIZE]; | |
151 | ||
152 | if (OBJ_obj2txt(txtoidname, sizeof(txtoidname), | |
153 | pubkey->algor->algorithm, 0) <= 0) { | |
66066e1b | 154 | ERR_clear_last_mark(); |
10315851 | 155 | goto end; |
66066e1b | 156 | } |
10315851 RL |
157 | if ((dctx = |
158 | OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey, | |
159 | "DER", "SubjectPublicKeyInfo", | |
160 | txtoidname, EVP_PKEY_PUBLIC_KEY, | |
161 | pubkey->libctx, | |
162 | pubkey->propq)) != NULL) | |
163 | /* | |
164 | * As said higher up, we're being opportunistic. In other words, | |
165 | * we don't care about what the return value signals. | |
166 | */ | |
167 | OSSL_DECODER_from_data(dctx, &p, NULL); | |
0f113f3e | 168 | } |
10315851 RL |
169 | |
170 | ERR_pop_to_mark(); | |
171 | ret = 1; | |
172 | end: | |
173 | OSSL_DECODER_CTX_free(dctx); | |
174 | return ret; | |
0f113f3e | 175 | } |
d02b48c6 | 176 | |
10315851 RL |
177 | static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out, |
178 | const ASN1_ITEM *it, int tag, int aclass) | |
179 | { | |
180 | return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), | |
181 | tag, aclass); | |
182 | } | |
183 | ||
184 | static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent, | |
185 | const char *fname, const ASN1_PCTX *pctx) | |
186 | { | |
187 | return ASN1_item_print(out, *pval, indent, | |
188 | ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx); | |
189 | } | |
190 | ||
191 | static const ASN1_EXTERN_FUNCS x509_pubkey_ff = { | |
192 | NULL, | |
193 | x509_pubkey_ex_new, | |
194 | x509_pubkey_ex_free, | |
195 | 0, /* Default clear behaviour is OK */ | |
196 | x509_pubkey_ex_d2i, | |
197 | x509_pubkey_ex_i2d, | |
198 | x509_pubkey_ex_print | |
199 | }; | |
d02b48c6 | 200 | |
10315851 | 201 | IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff) |
9d6b1ce6 | 202 | IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY) |
10315851 RL |
203 | |
204 | /* | |
205 | * X509_PUBKEY_dup() must be implemented manually, because there is no | |
206 | * support for it in ASN1_EXTERN_FUNCS. | |
207 | */ | |
208 | X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a) | |
209 | { | |
e7aa284e P |
210 | X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey)); |
211 | ||
212 | if (pubkey == NULL | |
213 | || !x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq) | |
214 | || (pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL | |
215 | || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL | |
216 | || !ASN1_BIT_STRING_set(pubkey->public_key, | |
217 | a->public_key->data, a->public_key->length) | |
218 | || (a->pkey != NULL && !EVP_PKEY_up_ref(a->pkey))) { | |
10315851 RL |
219 | x509_pubkey_ex_free((ASN1_VALUE **)&pubkey, |
220 | ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL)); | |
221 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | |
222 | return NULL; | |
223 | } | |
10315851 RL |
224 | pubkey->pkey = a->pkey; |
225 | return pubkey; | |
226 | } | |
d02b48c6 | 227 | |
6b691a5c | 228 | int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) |
0f113f3e MC |
229 | { |
230 | X509_PUBKEY *pk = NULL; | |
231 | ||
7836f949 DDO |
232 | if (x == NULL || pkey == NULL) { |
233 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); | |
26a7d938 | 234 | return 0; |
7836f949 | 235 | } |
0f113f3e | 236 | |
e56ba0e1 RL |
237 | if (pkey->ameth != NULL) { |
238 | if ((pk = X509_PUBKEY_new()) == NULL) { | |
9311d0c4 | 239 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
e56ba0e1 RL |
240 | goto error; |
241 | } | |
242 | if (pkey->ameth->pub_encode != NULL) { | |
0f113f3e | 243 | if (!pkey->ameth->pub_encode(pk, pkey)) { |
9311d0c4 | 244 | ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR); |
0f113f3e MC |
245 | goto error; |
246 | } | |
247 | } else { | |
9311d0c4 | 248 | ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED); |
0f113f3e MC |
249 | goto error; |
250 | } | |
113adc1f | 251 | } else if (evp_pkey_is_provided(pkey)) { |
113adc1f RL |
252 | unsigned char *der = NULL; |
253 | size_t derlen = 0; | |
ece9304c | 254 | OSSL_ENCODER_CTX *ectx = |
fe75766c TM |
255 | OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY, |
256 | "DER", "SubjectPublicKeyInfo", | |
257 | NULL); | |
e56ba0e1 | 258 | |
113adc1f RL |
259 | if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) { |
260 | const unsigned char *pder = der; | |
e56ba0e1 | 261 | |
113adc1f | 262 | pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen); |
e56ba0e1 RL |
263 | } |
264 | ||
ece9304c | 265 | OSSL_ENCODER_CTX_free(ectx); |
113adc1f | 266 | OPENSSL_free(der); |
0f113f3e MC |
267 | } |
268 | ||
7836f949 DDO |
269 | if (pk == NULL) { |
270 | ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM); | |
271 | goto error; | |
272 | } | |
e56ba0e1 | 273 | |
222561fe | 274 | X509_PUBKEY_free(*x); |
e56ba0e1 | 275 | if (!EVP_PKEY_up_ref(pkey)) { |
9311d0c4 | 276 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
e56ba0e1 RL |
277 | goto error; |
278 | } | |
0f113f3e | 279 | *x = pk; |
a076951b RL |
280 | |
281 | /* | |
282 | * pk->pkey is NULL when using the legacy routine, but is non-NULL when | |
ece9304c | 283 | * going through the encoder, and for all intents and purposes, it's |
0d5bbaaa MC |
284 | * a perfect copy of the public key portions of |pkey|, just not the same |
285 | * instance. If that's all there was to pkey then we could simply return | |
286 | * early, right here. However, some application might very well depend on | |
287 | * the passed |pkey| being used and none other, so we spend a few more | |
288 | * cycles throwing away the newly created |pk->pkey| and replace it with | |
289 | * |pkey|. | |
a076951b RL |
290 | */ |
291 | if (pk->pkey != NULL) | |
292 | EVP_PKEY_free(pk->pkey); | |
293 | ||
fa0a9d71 | 294 | pk->pkey = pkey; |
e56ba0e1 RL |
295 | return 1; |
296 | ||
0f113f3e | 297 | error: |
222561fe | 298 | X509_PUBKEY_free(pk); |
0f113f3e MC |
299 | return 0; |
300 | } | |
d02b48c6 | 301 | |
fa0a9d71 DSH |
302 | /* |
303 | * Attempt to decode a public key. | |
304 | * Returns 1 on success, 0 for a decode failure and -1 for a fatal | |
305 | * error e.g. malloc failure. | |
10315851 RL |
306 | * |
307 | * This function is #legacy. | |
fa0a9d71 | 308 | */ |
7674e923 | 309 | static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key) |
7fcdbd83 | 310 | { |
fa0a9d71 | 311 | EVP_PKEY *pkey = EVP_PKEY_new(); |
0f113f3e | 312 | |
fa0a9d71 | 313 | if (pkey == NULL) { |
9311d0c4 | 314 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
fa0a9d71 | 315 | return -1; |
0f113f3e MC |
316 | } |
317 | ||
fa0a9d71 | 318 | if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) { |
9311d0c4 | 319 | ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM); |
0f113f3e MC |
320 | goto error; |
321 | } | |
322 | ||
fa0a9d71 DSH |
323 | if (pkey->ameth->pub_decode) { |
324 | /* | |
325 | * Treat any failure of pub_decode as a decode error. In | |
326 | * future we could have different return codes for decode | |
327 | * errors and fatal errors such as malloc failure. | |
328 | */ | |
66066e1b | 329 | if (!pkey->ameth->pub_decode(pkey, key)) |
0f113f3e | 330 | goto error; |
0f113f3e | 331 | } else { |
9311d0c4 | 332 | ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED); |
0f113f3e MC |
333 | goto error; |
334 | } | |
335 | ||
fa0a9d71 DSH |
336 | *ppkey = pkey; |
337 | return 1; | |
0f113f3e MC |
338 | |
339 | error: | |
fa0a9d71 DSH |
340 | EVP_PKEY_free(pkey); |
341 | return 0; | |
342 | } | |
343 | ||
7674e923 | 344 | EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key) |
fa0a9d71 DSH |
345 | { |
346 | EVP_PKEY *ret = NULL; | |
347 | ||
348 | if (key == NULL || key->public_key == NULL) | |
349 | return NULL; | |
350 | ||
351 | if (key->pkey != NULL) | |
352 | return key->pkey; | |
353 | ||
354 | /* | |
355 | * When the key ASN.1 is initially parsed an attempt is made to | |
356 | * decode the public key and cache the EVP_PKEY structure. If this | |
357 | * operation fails the cached value will be NULL. Parsing continues | |
358 | * to allow parsing of unknown key types or unsupported forms. | |
359 | * We repeat the decode operation so the appropriate errors are left | |
360 | * in the queue. | |
361 | */ | |
362 | x509_pubkey_decode(&ret, key); | |
363 | /* If decode doesn't fail something bad happened */ | |
364 | if (ret != NULL) { | |
9311d0c4 | 365 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
fa0a9d71 DSH |
366 | EVP_PKEY_free(ret); |
367 | } | |
368 | ||
369 | return NULL; | |
0f113f3e MC |
370 | } |
371 | ||
7674e923 | 372 | EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) |
c01ff880 DSH |
373 | { |
374 | EVP_PKEY *ret = X509_PUBKEY_get0(key); | |
e9e7b5df BE |
375 | |
376 | if (ret != NULL && !EVP_PKEY_up_ref(ret)) { | |
9311d0c4 | 377 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
e9e7b5df BE |
378 | ret = NULL; |
379 | } | |
c01ff880 DSH |
380 | return ret; |
381 | } | |
382 | ||
0f113f3e | 383 | /* |
22b81444 RL |
384 | * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode |
385 | * or decode as X509_PUBKEY | |
52664f50 | 386 | */ |
10315851 RL |
387 | static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a, |
388 | const unsigned char **pp, long length, | |
389 | OSSL_LIB_CTX *libctx, const char *propq, | |
390 | unsigned int force_legacy, | |
391 | X509_PUBKEY * | |
392 | (*d2i_x509_pubkey)(X509_PUBKEY **a, | |
393 | const unsigned char **in, | |
394 | long len)) | |
0f113f3e | 395 | { |
22b81444 RL |
396 | X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL; |
397 | EVP_PKEY *pktmp = NULL; | |
a46c9789 | 398 | const unsigned char *q; |
12a765a5 | 399 | |
a46c9789 | 400 | q = *pp; |
22b81444 RL |
401 | |
402 | /* | |
403 | * If libctx or propq are non-NULL, we take advantage of the reuse | |
404 | * feature. It's not generally recommended, but is safe enough for | |
405 | * newly created structures. | |
406 | */ | |
10315851 | 407 | if (libctx != NULL || propq != NULL || force_legacy) { |
22b81444 RL |
408 | xpk2 = OPENSSL_zalloc(sizeof(*xpk2)); |
409 | if (xpk2 == NULL) { | |
410 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | |
411 | return NULL; | |
412 | } | |
637dce3c SL |
413 | if (!x509_pubkey_set0_libctx(xpk2, libctx, propq)) |
414 | goto end; | |
10315851 | 415 | xpk2->flag_force_legacy = !!force_legacy; |
22b81444 RL |
416 | pxpk = &xpk2; |
417 | } | |
10315851 | 418 | xpk = d2i_x509_pubkey(pxpk, &q, length); |
12a765a5 | 419 | if (xpk == NULL) |
22b81444 | 420 | goto end; |
0f113f3e MC |
421 | pktmp = X509_PUBKEY_get(xpk); |
422 | X509_PUBKEY_free(xpk); | |
22b81444 | 423 | xpk2 = NULL; /* We know that xpk == xpk2 */ |
12a765a5 | 424 | if (pktmp == NULL) |
22b81444 | 425 | goto end; |
a46c9789 | 426 | *pp = q; |
12a765a5 | 427 | if (a != NULL) { |
0f113f3e MC |
428 | EVP_PKEY_free(*a); |
429 | *a = pktmp; | |
430 | } | |
22b81444 RL |
431 | end: |
432 | X509_PUBKEY_free(xpk2); | |
0f113f3e MC |
433 | return pktmp; |
434 | } | |
52664f50 | 435 | |
10315851 | 436 | /* For the algorithm specific d2i functions further down */ |
aff442dc RL |
437 | static EVP_PKEY *d2i_PUBKEY_legacy(EVP_PKEY **a, |
438 | const unsigned char **pp, long length) | |
10315851 RL |
439 | { |
440 | return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY); | |
441 | } | |
442 | ||
443 | EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length, | |
444 | OSSL_LIB_CTX *libctx, const char *propq) | |
445 | { | |
446 | return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY); | |
447 | } | |
448 | ||
22b81444 RL |
449 | EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length) |
450 | { | |
451 | return d2i_PUBKEY_ex(a, pp, length, NULL, NULL); | |
452 | } | |
453 | ||
9fdcc21f | 454 | int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp) |
0f113f3e | 455 | { |
9fdcc21f DO |
456 | int ret = -1; |
457 | ||
458 | if (a == NULL) | |
0f113f3e | 459 | return 0; |
cdb16632 RL |
460 | if (a->ameth != NULL) { |
461 | X509_PUBKEY *xpk = NULL; | |
462 | ||
463 | if ((xpk = X509_PUBKEY_new()) == NULL) | |
464 | return -1; | |
465 | ||
466 | /* pub_encode() only encode parameters, not the key itself */ | |
467 | if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) { | |
468 | xpk->pkey = (EVP_PKEY *)a; | |
469 | ret = i2d_X509_PUBKEY(xpk, pp); | |
470 | xpk->pkey = NULL; | |
471 | } | |
472 | X509_PUBKEY_free(xpk); | |
3c6ed955 | 473 | } else if (a->keymgmt != NULL) { |
ece9304c | 474 | OSSL_ENCODER_CTX *ctx = |
fe75766c TM |
475 | OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY, |
476 | "DER", "SubjectPublicKeyInfo", | |
477 | NULL); | |
cdb16632 RL |
478 | BIO *out = BIO_new(BIO_s_mem()); |
479 | BUF_MEM *buf = NULL; | |
480 | ||
97bb8dff | 481 | if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0 |
cdb16632 | 482 | && out != NULL |
ece9304c | 483 | && OSSL_ENCODER_to_bio(ctx, out) |
cdb16632 RL |
484 | && BIO_get_mem_ptr(out, &buf) > 0) { |
485 | ret = buf->length; | |
486 | ||
487 | if (pp != NULL) { | |
488 | if (*pp == NULL) { | |
489 | *pp = (unsigned char *)buf->data; | |
490 | buf->length = 0; | |
491 | buf->data = NULL; | |
492 | } else { | |
493 | memcpy(*pp, buf->data, ret); | |
494 | *pp += ret; | |
495 | } | |
496 | } | |
497 | } | |
498 | BIO_free(out); | |
ece9304c | 499 | OSSL_ENCODER_CTX_free(ctx); |
cdb16632 RL |
500 | } |
501 | ||
0f113f3e MC |
502 | return ret; |
503 | } | |
504 | ||
505 | /* | |
506 | * The following are equivalents but which return RSA and DSA keys | |
52664f50 | 507 | */ |
0f113f3e MC |
508 | RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length) |
509 | { | |
510 | EVP_PKEY *pkey; | |
06f67612 | 511 | RSA *key = NULL; |
0f113f3e | 512 | const unsigned char *q; |
12a765a5 | 513 | |
0f113f3e | 514 | q = *pp; |
aff442dc | 515 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
12a765a5 | 516 | if (pkey == NULL) |
0f113f3e MC |
517 | return NULL; |
518 | key = EVP_PKEY_get1_RSA(pkey); | |
519 | EVP_PKEY_free(pkey); | |
12a765a5 | 520 | if (key == NULL) |
0f113f3e MC |
521 | return NULL; |
522 | *pp = q; | |
12a765a5 | 523 | if (a != NULL) { |
0f113f3e MC |
524 | RSA_free(*a); |
525 | *a = key; | |
526 | } | |
527 | return key; | |
528 | } | |
52664f50 | 529 | |
9fdcc21f | 530 | int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp) |
0f113f3e MC |
531 | { |
532 | EVP_PKEY *pktmp; | |
533 | int ret; | |
534 | if (!a) | |
535 | return 0; | |
536 | pktmp = EVP_PKEY_new(); | |
90945fa3 | 537 | if (pktmp == NULL) { |
9311d0c4 | 538 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
157997f0 | 539 | return -1; |
0f113f3e | 540 | } |
9fdcc21f | 541 | (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a); |
0f113f3e | 542 | ret = i2d_PUBKEY(pktmp, pp); |
9fdcc21f | 543 | pktmp->pkey.ptr = NULL; |
0f113f3e MC |
544 | EVP_PKEY_free(pktmp); |
545 | return ret; | |
546 | } | |
52664f50 | 547 | |
06f67612 RL |
548 | #ifndef OPENSSL_NO_DH |
549 | DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length) | |
550 | { | |
551 | EVP_PKEY *pkey; | |
552 | DH *key = NULL; | |
553 | const unsigned char *q; | |
554 | ||
555 | q = *pp; | |
aff442dc | 556 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
06f67612 RL |
557 | if (pkey == NULL) |
558 | return NULL; | |
ed576acd | 559 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH) |
06f67612 RL |
560 | key = EVP_PKEY_get1_DH(pkey); |
561 | EVP_PKEY_free(pkey); | |
562 | if (key == NULL) | |
563 | return NULL; | |
564 | *pp = q; | |
565 | if (a != NULL) { | |
566 | DH_free(*a); | |
567 | *a = key; | |
568 | } | |
569 | return key; | |
570 | } | |
571 | ||
572 | int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp) | |
573 | { | |
574 | EVP_PKEY *pktmp; | |
575 | int ret; | |
576 | if (!a) | |
577 | return 0; | |
578 | pktmp = EVP_PKEY_new(); | |
579 | if (pktmp == NULL) { | |
580 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
581 | return -1; | |
582 | } | |
583 | (void)EVP_PKEY_assign_DH(pktmp, (DH *)a); | |
584 | ret = i2d_PUBKEY(pktmp, pp); | |
585 | pktmp->pkey.ptr = NULL; | |
586 | EVP_PKEY_free(pktmp); | |
587 | return ret; | |
588 | } | |
589 | ||
590 | DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length) | |
591 | { | |
592 | EVP_PKEY *pkey; | |
593 | DH *key = NULL; | |
594 | const unsigned char *q; | |
595 | ||
596 | q = *pp; | |
aff442dc | 597 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
06f67612 RL |
598 | if (pkey == NULL) |
599 | return NULL; | |
ed576acd | 600 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX) |
06f67612 RL |
601 | key = EVP_PKEY_get1_DH(pkey); |
602 | EVP_PKEY_free(pkey); | |
603 | if (key == NULL) | |
604 | return NULL; | |
605 | *pp = q; | |
606 | if (a != NULL) { | |
607 | DH_free(*a); | |
608 | *a = key; | |
609 | } | |
610 | return key; | |
611 | } | |
612 | ||
613 | int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp) | |
614 | { | |
615 | EVP_PKEY *pktmp; | |
616 | int ret; | |
617 | if (!a) | |
618 | return 0; | |
619 | pktmp = EVP_PKEY_new(); | |
620 | if (pktmp == NULL) { | |
621 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
622 | return -1; | |
623 | } | |
624 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a); | |
625 | ret = i2d_PUBKEY(pktmp, pp); | |
626 | pktmp->pkey.ptr = NULL; | |
627 | EVP_PKEY_free(pktmp); | |
628 | return ret; | |
629 | } | |
630 | #endif | |
631 | ||
cf1b7d96 | 632 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
633 | DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length) |
634 | { | |
635 | EVP_PKEY *pkey; | |
06f67612 | 636 | DSA *key = NULL; |
0f113f3e | 637 | const unsigned char *q; |
12a765a5 | 638 | |
0f113f3e | 639 | q = *pp; |
aff442dc | 640 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
12a765a5 | 641 | if (pkey == NULL) |
0f113f3e MC |
642 | return NULL; |
643 | key = EVP_PKEY_get1_DSA(pkey); | |
644 | EVP_PKEY_free(pkey); | |
12a765a5 | 645 | if (key == NULL) |
0f113f3e MC |
646 | return NULL; |
647 | *pp = q; | |
12a765a5 | 648 | if (a != NULL) { |
0f113f3e MC |
649 | DSA_free(*a); |
650 | *a = key; | |
651 | } | |
652 | return key; | |
653 | } | |
52664f50 | 654 | |
9fdcc21f | 655 | int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp) |
0f113f3e MC |
656 | { |
657 | EVP_PKEY *pktmp; | |
658 | int ret; | |
659 | if (!a) | |
660 | return 0; | |
661 | pktmp = EVP_PKEY_new(); | |
90945fa3 | 662 | if (pktmp == NULL) { |
9311d0c4 | 663 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
157997f0 | 664 | return -1; |
0f113f3e | 665 | } |
9fdcc21f | 666 | (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a); |
0f113f3e | 667 | ret = i2d_PUBKEY(pktmp, pp); |
9fdcc21f | 668 | pktmp->pkey.ptr = NULL; |
0f113f3e MC |
669 | EVP_PKEY_free(pktmp); |
670 | return ret; | |
671 | } | |
4d94ae00 BM |
672 | #endif |
673 | ||
14a7cfb3 | 674 | #ifndef OPENSSL_NO_EC |
6343829a | 675 | EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length) |
0f113f3e MC |
676 | { |
677 | EVP_PKEY *pkey; | |
06f67612 | 678 | EC_KEY *key = NULL; |
0f113f3e | 679 | const unsigned char *q; |
b4c4a2c6 | 680 | int type; |
12a765a5 | 681 | |
0f113f3e | 682 | q = *pp; |
aff442dc | 683 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
12a765a5 | 684 | if (pkey == NULL) |
26a7d938 | 685 | return NULL; |
ed576acd | 686 | type = EVP_PKEY_get_id(pkey); |
b4c4a2c6 | 687 | if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2) |
06f67612 | 688 | key = EVP_PKEY_get1_EC_KEY(pkey); |
0f113f3e | 689 | EVP_PKEY_free(pkey); |
12a765a5 | 690 | if (key == NULL) |
26a7d938 | 691 | return NULL; |
0f113f3e | 692 | *pp = q; |
12a765a5 | 693 | if (a != NULL) { |
0f113f3e MC |
694 | EC_KEY_free(*a); |
695 | *a = key; | |
696 | } | |
26a7d938 | 697 | return key; |
0f113f3e | 698 | } |
4d94ae00 | 699 | |
9fdcc21f | 700 | int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp) |
0f113f3e MC |
701 | { |
702 | EVP_PKEY *pktmp; | |
703 | int ret; | |
12a765a5 RS |
704 | |
705 | if (a == NULL) | |
26a7d938 | 706 | return 0; |
0f113f3e | 707 | if ((pktmp = EVP_PKEY_new()) == NULL) { |
9311d0c4 | 708 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
157997f0 | 709 | return -1; |
0f113f3e | 710 | } |
9fdcc21f | 711 | (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a); |
0f113f3e | 712 | ret = i2d_PUBKEY(pktmp, pp); |
9fdcc21f | 713 | pktmp->pkey.ptr = NULL; |
0f113f3e | 714 | EVP_PKEY_free(pktmp); |
26a7d938 | 715 | return ret; |
0f113f3e | 716 | } |
06f67612 RL |
717 | |
718 | ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a, | |
719 | const unsigned char **pp, long length) | |
720 | { | |
721 | EVP_PKEY *pkey; | |
722 | ECX_KEY *key = NULL; | |
723 | const unsigned char *q; | |
724 | ||
725 | q = *pp; | |
aff442dc | 726 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
06f67612 RL |
727 | if (pkey == NULL) |
728 | return NULL; | |
729 | key = ossl_evp_pkey_get1_ED25519(pkey); | |
730 | EVP_PKEY_free(pkey); | |
731 | if (key == NULL) | |
732 | return NULL; | |
733 | *pp = q; | |
734 | if (a != NULL) { | |
735 | ossl_ecx_key_free(*a); | |
736 | *a = key; | |
737 | } | |
738 | return key; | |
739 | } | |
740 | ||
741 | int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
742 | { | |
743 | EVP_PKEY *pktmp; | |
744 | int ret; | |
745 | ||
746 | if (a == NULL) | |
747 | return 0; | |
748 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
749 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
750 | return -1; | |
751 | } | |
752 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a); | |
753 | ret = i2d_PUBKEY(pktmp, pp); | |
754 | pktmp->pkey.ptr = NULL; | |
755 | EVP_PKEY_free(pktmp); | |
756 | return ret; | |
757 | } | |
758 | ||
759 | ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a, | |
760 | const unsigned char **pp, long length) | |
761 | { | |
762 | EVP_PKEY *pkey; | |
763 | ECX_KEY *key = NULL; | |
764 | const unsigned char *q; | |
765 | ||
766 | q = *pp; | |
aff442dc | 767 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
06f67612 RL |
768 | if (pkey == NULL) |
769 | return NULL; | |
ed576acd | 770 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448) |
06f67612 RL |
771 | key = ossl_evp_pkey_get1_ED448(pkey); |
772 | EVP_PKEY_free(pkey); | |
773 | if (key == NULL) | |
774 | return NULL; | |
775 | *pp = q; | |
776 | if (a != NULL) { | |
777 | ossl_ecx_key_free(*a); | |
778 | *a = key; | |
779 | } | |
780 | return key; | |
781 | } | |
782 | ||
783 | int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
784 | { | |
785 | EVP_PKEY *pktmp; | |
786 | int ret; | |
787 | ||
788 | if (a == NULL) | |
789 | return 0; | |
790 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
791 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
792 | return -1; | |
793 | } | |
794 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a); | |
795 | ret = i2d_PUBKEY(pktmp, pp); | |
796 | pktmp->pkey.ptr = NULL; | |
797 | EVP_PKEY_free(pktmp); | |
798 | return ret; | |
799 | } | |
800 | ||
801 | ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a, | |
802 | const unsigned char **pp, long length) | |
803 | { | |
804 | EVP_PKEY *pkey; | |
805 | ECX_KEY *key = NULL; | |
806 | const unsigned char *q; | |
807 | ||
808 | q = *pp; | |
aff442dc | 809 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
06f67612 RL |
810 | if (pkey == NULL) |
811 | return NULL; | |
ed576acd | 812 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519) |
06f67612 RL |
813 | key = ossl_evp_pkey_get1_X25519(pkey); |
814 | EVP_PKEY_free(pkey); | |
815 | if (key == NULL) | |
816 | return NULL; | |
817 | *pp = q; | |
818 | if (a != NULL) { | |
819 | ossl_ecx_key_free(*a); | |
820 | *a = key; | |
821 | } | |
822 | return key; | |
823 | } | |
824 | ||
825 | int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
826 | { | |
827 | EVP_PKEY *pktmp; | |
828 | int ret; | |
829 | ||
830 | if (a == NULL) | |
831 | return 0; | |
832 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
833 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
834 | return -1; | |
835 | } | |
836 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a); | |
837 | ret = i2d_PUBKEY(pktmp, pp); | |
838 | pktmp->pkey.ptr = NULL; | |
839 | EVP_PKEY_free(pktmp); | |
840 | return ret; | |
841 | } | |
842 | ||
843 | ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a, | |
844 | const unsigned char **pp, long length) | |
845 | { | |
846 | EVP_PKEY *pkey; | |
847 | ECX_KEY *key = NULL; | |
848 | const unsigned char *q; | |
849 | ||
850 | q = *pp; | |
aff442dc | 851 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); |
06f67612 RL |
852 | if (pkey == NULL) |
853 | return NULL; | |
ed576acd | 854 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448) |
06f67612 RL |
855 | key = ossl_evp_pkey_get1_X448(pkey); |
856 | EVP_PKEY_free(pkey); | |
857 | if (key == NULL) | |
858 | return NULL; | |
859 | *pp = q; | |
860 | if (a != NULL) { | |
861 | ossl_ecx_key_free(*a); | |
862 | *a = key; | |
863 | } | |
864 | return key; | |
865 | } | |
866 | ||
867 | int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
868 | { | |
869 | EVP_PKEY *pktmp; | |
870 | int ret; | |
871 | ||
872 | if (a == NULL) | |
873 | return 0; | |
874 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
875 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
876 | return -1; | |
877 | } | |
878 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a); | |
879 | ret = i2d_PUBKEY(pktmp, pp); | |
880 | pktmp->pkey.ptr = NULL; | |
881 | EVP_PKEY_free(pktmp); | |
882 | return ret; | |
883 | } | |
884 | ||
12aefe78 | 885 | #endif |
448be743 DSH |
886 | |
887 | int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, | |
0f113f3e MC |
888 | int ptype, void *pval, |
889 | unsigned char *penc, int penclen) | |
890 | { | |
891 | if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) | |
892 | return 0; | |
893 | if (penc) { | |
b548a1f1 | 894 | OPENSSL_free(pub->public_key->data); |
0f113f3e MC |
895 | pub->public_key->data = penc; |
896 | pub->public_key->length = penclen; | |
897 | /* Set number of unused bits to zero */ | |
898 | pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | |
899 | pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT; | |
900 | } | |
901 | return 1; | |
902 | } | |
448be743 DSH |
903 | |
904 | int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, | |
0f113f3e | 905 | const unsigned char **pk, int *ppklen, |
7674e923 | 906 | X509_ALGOR **pa, const X509_PUBKEY *pub) |
0f113f3e MC |
907 | { |
908 | if (ppkalg) | |
909 | *ppkalg = pub->algor->algorithm; | |
910 | if (pk) { | |
911 | *pk = pub->public_key->data; | |
912 | *ppklen = pub->public_key->length; | |
913 | } | |
914 | if (pa) | |
915 | *pa = pub->algor; | |
916 | return 1; | |
917 | } | |
29fa0a1a DSH |
918 | |
919 | ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) | |
920 | { | |
921 | if (x == NULL) | |
922 | return NULL; | |
923 | return x->cert_info.key->public_key; | |
924 | } | |
93f99b68 DDO |
925 | |
926 | /* Returns 1 for equal, 0, for non-equal, < 0 on error */ | |
927 | int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b) | |
928 | { | |
929 | X509_ALGOR *algA, *algB; | |
930 | EVP_PKEY *pA, *pB; | |
931 | ||
932 | if (a == b) | |
933 | return 1; | |
934 | if (a == NULL || b == NULL) | |
935 | return 0; | |
936 | if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL | |
937 | || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL) | |
938 | return -2; | |
939 | if (X509_ALGOR_cmp(algA, algB) != 0) | |
940 | return 0; | |
941 | if ((pA = X509_PUBKEY_get0(a)) == NULL | |
942 | || (pB = X509_PUBKEY_get0(b)) == NULL) | |
943 | return -2; | |
c74aaa39 | 944 | return EVP_PKEY_eq(pA, pB); |
93f99b68 | 945 | } |
22b81444 | 946 | |
4669015d SL |
947 | int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq, |
948 | const X509_PUBKEY *key) | |
22b81444 RL |
949 | { |
950 | if (plibctx) | |
951 | *plibctx = key->libctx; | |
952 | if (ppropq) | |
953 | *ppropq = key->propq; | |
954 | return 1; | |
955 | } |