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