]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x_pubkey.c
x509: add missing X509 dup functions
[thirdparty/openssl.git] / crypto / x509 / x_pubkey.c
CommitLineData
b1322259 1/*
6ec5fce2 2 * Copyright 1995-2018 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
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
9d6b1ce6 12#include <openssl/asn1t.h>
f0e8ae72 13#include <openssl/x509.h>
5fe736e5 14#include "internal/asn1_int.h"
3aeb9348 15#include "internal/evp_int.h"
29fa0a1a 16#include "internal/x509_int.h"
3c27208f
RS
17#include <openssl/rsa.h>
18#include <openssl/dsa.h>
d02b48c6 19
29fa0a1a
DSH
20struct X509_pubkey_st {
21 X509_ALGOR *algor;
22 ASN1_BIT_STRING *public_key;
23 EVP_PKEY *pkey;
29fa0a1a
DSH
24};
25
fa0a9d71
DSH
26static int x509_pubkey_decode(EVP_PKEY **pk, X509_PUBKEY *key);
27
9d6b1ce6 28/* Minor tweak to operation: free up EVP_PKEY */
24484759 29static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e
MC
30 void *exarg)
31{
32 if (operation == ASN1_OP_FREE_POST) {
33 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
34 EVP_PKEY_free(pubkey->pkey);
d2ec189f
DSH
35 } else if (operation == ASN1_OP_D2I_POST) {
36 /* Attempt to decode public key and cache in pubkey structure. */
37 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
38 EVP_PKEY_free(pubkey->pkey);
5dc40a83 39 pubkey->pkey = NULL;
d2ec189f 40 /*
fa0a9d71
DSH
41 * Opportunistically decode the key but remove any non fatal errors
42 * from the queue. Subsequent explicit attempts to decode/use the key
43 * will return an appropriate error.
d2ec189f
DSH
44 */
45 ERR_set_mark();
fa0a9d71
DSH
46 if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1)
47 return 0;
d2ec189f 48 ERR_pop_to_mark();
0f113f3e
MC
49 }
50 return 1;
51}
d02b48c6 52
9d6b1ce6 53ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
0f113f3e
MC
54 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
55 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
d339187b 56} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
d02b48c6 57
9d6b1ce6 58IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
d02b48c6 59
9fdcc21f 60/* TODO should better be called X509_PUBKEY_set1 */
6b691a5c 61int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
0f113f3e
MC
62{
63 X509_PUBKEY *pk = NULL;
64
65 if (x == NULL)
26a7d938 66 return 0;
0f113f3e
MC
67
68 if ((pk = X509_PUBKEY_new()) == NULL)
69 goto error;
70
9fdcc21f 71 if (pkey != NULL && pkey->ameth) {
0f113f3e
MC
72 if (pkey->ameth->pub_encode) {
73 if (!pkey->ameth->pub_encode(pk, pkey)) {
74 X509err(X509_F_X509_PUBKEY_SET,
75 X509_R_PUBLIC_KEY_ENCODE_ERROR);
76 goto error;
77 }
78 } else {
79 X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
80 goto error;
81 }
82 } else {
83 X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
84 goto error;
85 }
86
222561fe 87 X509_PUBKEY_free(*x);
0f113f3e 88 *x = pk;
fa0a9d71 89 pk->pkey = pkey;
9fdcc21f 90 return EVP_PKEY_up_ref(pkey);
222561fe 91
0f113f3e 92 error:
222561fe 93 X509_PUBKEY_free(pk);
0f113f3e
MC
94 return 0;
95}
d02b48c6 96
fa0a9d71
DSH
97/*
98 * Attempt to decode a public key.
99 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
100 * error e.g. malloc failure.
101 */
0f113f3e 102
0f113f3e 103
fa0a9d71 104static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)
7fcdbd83 105{
fa0a9d71 106 EVP_PKEY *pkey = EVP_PKEY_new();
0f113f3e 107
fa0a9d71
DSH
108 if (pkey == NULL) {
109 X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
110 return -1;
0f113f3e
MC
111 }
112
fa0a9d71
DSH
113 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
114 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
0f113f3e
MC
115 goto error;
116 }
117
fa0a9d71
DSH
118 if (pkey->ameth->pub_decode) {
119 /*
120 * Treat any failure of pub_decode as a decode error. In
121 * future we could have different return codes for decode
122 * errors and fatal errors such as malloc failure.
123 */
124 if (!pkey->ameth->pub_decode(pkey, key)) {
125 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);
0f113f3e
MC
126 goto error;
127 }
128 } else {
fa0a9d71 129 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
130 goto error;
131 }
132
fa0a9d71
DSH
133 *ppkey = pkey;
134 return 1;
0f113f3e
MC
135
136 error:
fa0a9d71
DSH
137 EVP_PKEY_free(pkey);
138 return 0;
139}
140
141EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)
142{
143 EVP_PKEY *ret = NULL;
144
145 if (key == NULL || key->public_key == NULL)
146 return NULL;
147
148 if (key->pkey != NULL)
149 return key->pkey;
150
151 /*
152 * When the key ASN.1 is initially parsed an attempt is made to
153 * decode the public key and cache the EVP_PKEY structure. If this
154 * operation fails the cached value will be NULL. Parsing continues
155 * to allow parsing of unknown key types or unsupported forms.
156 * We repeat the decode operation so the appropriate errors are left
157 * in the queue.
158 */
159 x509_pubkey_decode(&ret, key);
160 /* If decode doesn't fail something bad happened */
161 if (ret != NULL) {
162 X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
163 EVP_PKEY_free(ret);
164 }
165
166 return NULL;
0f113f3e
MC
167}
168
c01ff880
DSH
169EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
170{
171 EVP_PKEY *ret = X509_PUBKEY_get0(key);
172 if (ret != NULL)
173 EVP_PKEY_up_ref(ret);
174 return ret;
175}
176
0f113f3e
MC
177/*
178 * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
179 * decode as X509_PUBKEY
52664f50
DSH
180 */
181
0f113f3e
MC
182EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
183{
184 X509_PUBKEY *xpk;
185 EVP_PKEY *pktmp;
a46c9789
KR
186 const unsigned char *q;
187 q = *pp;
188 xpk = d2i_X509_PUBKEY(NULL, &q, length);
0f113f3e
MC
189 if (!xpk)
190 return NULL;
191 pktmp = X509_PUBKEY_get(xpk);
192 X509_PUBKEY_free(xpk);
193 if (!pktmp)
194 return NULL;
a46c9789 195 *pp = q;
0f113f3e
MC
196 if (a) {
197 EVP_PKEY_free(*a);
198 *a = pktmp;
199 }
200 return pktmp;
201}
52664f50 202
9fdcc21f 203int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
0f113f3e
MC
204{
205 X509_PUBKEY *xpk = NULL;
9fdcc21f
DO
206 int ret = -1;
207
208 if (a == NULL)
0f113f3e 209 return 0;
9fdcc21f 210 if ((xpk = X509_PUBKEY_new()) == NULL)
157997f0 211 return -1;
9fdcc21f
DO
212 if (a->ameth != NULL && a->ameth->pub_encode != NULL
213 && !a->ameth->pub_encode(xpk, a))
214 goto error;
215 xpk->pkey = (EVP_PKEY *)a;
0f113f3e 216 ret = i2d_X509_PUBKEY(xpk, pp);
9fdcc21f
DO
217 xpk->pkey = NULL;
218 error:
0f113f3e
MC
219 X509_PUBKEY_free(xpk);
220 return ret;
221}
222
223/*
224 * The following are equivalents but which return RSA and DSA keys
52664f50 225 */
cf1b7d96 226#ifndef OPENSSL_NO_RSA
0f113f3e
MC
227RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
228{
229 EVP_PKEY *pkey;
230 RSA *key;
231 const unsigned char *q;
232 q = *pp;
233 pkey = d2i_PUBKEY(NULL, &q, length);
234 if (!pkey)
235 return NULL;
236 key = EVP_PKEY_get1_RSA(pkey);
237 EVP_PKEY_free(pkey);
238 if (!key)
239 return NULL;
240 *pp = q;
241 if (a) {
242 RSA_free(*a);
243 *a = key;
244 }
245 return key;
246}
52664f50 247
9fdcc21f 248int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
0f113f3e
MC
249{
250 EVP_PKEY *pktmp;
251 int ret;
252 if (!a)
253 return 0;
254 pktmp = EVP_PKEY_new();
90945fa3 255 if (pktmp == NULL) {
0f113f3e 256 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 257 return -1;
0f113f3e 258 }
9fdcc21f 259 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
0f113f3e 260 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 261 pktmp->pkey.ptr = NULL;
0f113f3e
MC
262 EVP_PKEY_free(pktmp);
263 return ret;
264}
12aefe78 265#endif
52664f50 266
cf1b7d96 267#ifndef OPENSSL_NO_DSA
0f113f3e
MC
268DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
269{
270 EVP_PKEY *pkey;
271 DSA *key;
272 const unsigned char *q;
273 q = *pp;
274 pkey = d2i_PUBKEY(NULL, &q, length);
275 if (!pkey)
276 return NULL;
277 key = EVP_PKEY_get1_DSA(pkey);
278 EVP_PKEY_free(pkey);
279 if (!key)
280 return NULL;
281 *pp = q;
282 if (a) {
283 DSA_free(*a);
284 *a = key;
285 }
286 return key;
287}
52664f50 288
9fdcc21f 289int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
0f113f3e
MC
290{
291 EVP_PKEY *pktmp;
292 int ret;
293 if (!a)
294 return 0;
295 pktmp = EVP_PKEY_new();
90945fa3 296 if (pktmp == NULL) {
0f113f3e 297 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 298 return -1;
0f113f3e 299 }
9fdcc21f 300 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
0f113f3e 301 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 302 pktmp->pkey.ptr = NULL;
0f113f3e
MC
303 EVP_PKEY_free(pktmp);
304 return ret;
305}
4d94ae00
BM
306#endif
307
14a7cfb3 308#ifndef OPENSSL_NO_EC
6343829a 309EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
0f113f3e
MC
310{
311 EVP_PKEY *pkey;
312 EC_KEY *key;
313 const unsigned char *q;
314 q = *pp;
315 pkey = d2i_PUBKEY(NULL, &q, length);
316 if (!pkey)
26a7d938 317 return NULL;
0f113f3e
MC
318 key = EVP_PKEY_get1_EC_KEY(pkey);
319 EVP_PKEY_free(pkey);
320 if (!key)
26a7d938 321 return NULL;
0f113f3e
MC
322 *pp = q;
323 if (a) {
324 EC_KEY_free(*a);
325 *a = key;
326 }
26a7d938 327 return key;
0f113f3e 328}
4d94ae00 329
9fdcc21f 330int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
0f113f3e
MC
331{
332 EVP_PKEY *pktmp;
333 int ret;
334 if (!a)
26a7d938 335 return 0;
0f113f3e
MC
336 if ((pktmp = EVP_PKEY_new()) == NULL) {
337 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
157997f0 338 return -1;
0f113f3e 339 }
9fdcc21f 340 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
0f113f3e 341 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 342 pktmp->pkey.ptr = NULL;
0f113f3e 343 EVP_PKEY_free(pktmp);
26a7d938 344 return ret;
0f113f3e 345}
12aefe78 346#endif
448be743
DSH
347
348int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
0f113f3e
MC
349 int ptype, void *pval,
350 unsigned char *penc, int penclen)
351{
352 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
353 return 0;
354 if (penc) {
b548a1f1 355 OPENSSL_free(pub->public_key->data);
0f113f3e
MC
356 pub->public_key->data = penc;
357 pub->public_key->length = penclen;
358 /* Set number of unused bits to zero */
359 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
360 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
361 }
362 return 1;
363}
448be743
DSH
364
365int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
0f113f3e
MC
366 const unsigned char **pk, int *ppklen,
367 X509_ALGOR **pa, X509_PUBKEY *pub)
368{
369 if (ppkalg)
370 *ppkalg = pub->algor->algorithm;
371 if (pk) {
372 *pk = pub->public_key->data;
373 *ppklen = pub->public_key->length;
374 }
375 if (pa)
376 *pa = pub->algor;
377 return 1;
378}
29fa0a1a
DSH
379
380ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
381{
382 if (x == NULL)
383 return NULL;
384 return x->cert_info.key->public_key;
385}