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