1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
59 #include "internal/cryptlib.h"
60 #include <openssl/asn1t.h>
61 #include <openssl/x509.h>
62 #include "internal/asn1_int.h"
63 #include "internal/evp_int.h"
64 #include "internal/x509_int.h"
65 #include <openssl/rsa.h>
66 #include <openssl/dsa.h>
68 struct X509_pubkey_st {
70 ASN1_BIT_STRING *public_key;
74 static int x509_pubkey_decode(EVP_PKEY **pk, X509_PUBKEY *key);
76 /* Minor tweak to operation: free up EVP_PKEY */
77 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
80 if (operation == ASN1_OP_FREE_POST) {
81 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
82 EVP_PKEY_free(pubkey->pkey);
83 } else if (operation == ASN1_OP_D2I_POST) {
84 /* Attempt to decode public key and cache in pubkey structure. */
85 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
86 EVP_PKEY_free(pubkey->pkey);
88 * Opportunistically decode the key but remove any non fatal errors
89 * from the queue. Subsequent explicit attempts to decode/use the key
90 * will return an appropriate error.
93 if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1)
100 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
101 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
102 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
103 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
105 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
107 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
109 X509_PUBKEY *pk = NULL;
114 if ((pk = X509_PUBKEY_new()) == NULL)
118 if (pkey->ameth->pub_encode) {
119 if (!pkey->ameth->pub_encode(pk, pkey)) {
120 X509err(X509_F_X509_PUBKEY_SET,
121 X509_R_PUBLIC_KEY_ENCODE_ERROR);
125 X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
129 X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
133 X509_PUBKEY_free(*x);
136 EVP_PKEY_up_ref(pkey);
140 X509_PUBKEY_free(pk);
145 * Attempt to decode a public key.
146 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
147 * error e.g. malloc failure.
151 static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)
153 EVP_PKEY *pkey = EVP_PKEY_new();
156 X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
160 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
161 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
165 if (pkey->ameth->pub_decode) {
167 * Treat any failure of pub_decode as a decode error. In
168 * future we could have different return codes for decode
169 * errors and fatal errors such as malloc failure.
171 if (!pkey->ameth->pub_decode(pkey, key)) {
172 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);
176 X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
188 EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)
190 EVP_PKEY *ret = NULL;
192 if (key == NULL || key->public_key == NULL)
195 if (key->pkey != NULL)
199 * When the key ASN.1 is initially parsed an attempt is made to
200 * decode the public key and cache the EVP_PKEY structure. If this
201 * operation fails the cached value will be NULL. Parsing continues
202 * to allow parsing of unknown key types or unsupported forms.
203 * We repeat the decode operation so the appropriate errors are left
206 x509_pubkey_decode(&ret, key);
207 /* If decode doesn't fail something bad happened */
209 X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
216 EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
218 EVP_PKEY *ret = X509_PUBKEY_get0(key);
220 EVP_PKEY_up_ref(ret);
225 * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
226 * decode as X509_PUBKEY
229 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
233 const unsigned char *q;
235 xpk = d2i_X509_PUBKEY(NULL, &q, length);
238 pktmp = X509_PUBKEY_get(xpk);
239 X509_PUBKEY_free(xpk);
250 int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
252 X509_PUBKEY *xpk = NULL;
256 if (!X509_PUBKEY_set(&xpk, a))
258 ret = i2d_X509_PUBKEY(xpk, pp);
259 X509_PUBKEY_free(xpk);
264 * The following are equivalents but which return RSA and DSA keys
266 #ifndef OPENSSL_NO_RSA
267 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
271 const unsigned char *q;
273 pkey = d2i_PUBKEY(NULL, &q, length);
276 key = EVP_PKEY_get1_RSA(pkey);
288 int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
294 pktmp = EVP_PKEY_new();
296 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
299 EVP_PKEY_set1_RSA(pktmp, a);
300 ret = i2d_PUBKEY(pktmp, pp);
301 EVP_PKEY_free(pktmp);
306 #ifndef OPENSSL_NO_DSA
307 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
311 const unsigned char *q;
313 pkey = d2i_PUBKEY(NULL, &q, length);
316 key = EVP_PKEY_get1_DSA(pkey);
328 int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
334 pktmp = EVP_PKEY_new();
336 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
339 EVP_PKEY_set1_DSA(pktmp, a);
340 ret = i2d_PUBKEY(pktmp, pp);
341 EVP_PKEY_free(pktmp);
346 #ifndef OPENSSL_NO_EC
347 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
351 const unsigned char *q;
353 pkey = d2i_PUBKEY(NULL, &q, length);
356 key = EVP_PKEY_get1_EC_KEY(pkey);
368 int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
374 if ((pktmp = EVP_PKEY_new()) == NULL) {
375 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
378 EVP_PKEY_set1_EC_KEY(pktmp, a);
379 ret = i2d_PUBKEY(pktmp, pp);
380 EVP_PKEY_free(pktmp);
385 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
386 int ptype, void *pval,
387 unsigned char *penc, int penclen)
389 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
392 OPENSSL_free(pub->public_key->data);
393 pub->public_key->data = penc;
394 pub->public_key->length = penclen;
395 /* Set number of unused bits to zero */
396 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
397 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
402 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
403 const unsigned char **pk, int *ppklen,
404 X509_ALGOR **pa, X509_PUBKEY *pub)
407 *ppkalg = pub->algor->algorithm;
409 *pk = pub->public_key->data;
410 *ppklen = pub->public_key->length;
417 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
421 return x->cert_info.key->public_key;