1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
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 ([email protected]).
14 *
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.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
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 ([email protected])"
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 ([email protected])"
39 *
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
50 * SUCH DAMAGE.
51 *
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.] */
56
57 #include <openssl/x509.h>
58
59 #include <limits.h>
60
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/bytestring.h>
64 #include <openssl/err.h>
65 #include <openssl/evp.h>
66 #include <openssl/mem.h>
67 #include <openssl/obj.h>
68
69 #include "../internal.h"
70 #include "internal.h"
71
72
x509_pubkey_changed(X509_PUBKEY * pub)73 static void x509_pubkey_changed(X509_PUBKEY *pub) {
74 EVP_PKEY_free(pub->pkey);
75 pub->pkey = NULL;
76
77 // Re-encode the |X509_PUBKEY| to DER and parse it with EVP's APIs.
78 uint8_t *spki = NULL;
79 int spki_len = i2d_X509_PUBKEY(pub, &spki);
80 if (spki_len < 0) {
81 goto err;
82 }
83
84 CBS cbs;
85 CBS_init(&cbs, spki, (size_t)spki_len);
86 EVP_PKEY *pkey = EVP_parse_public_key(&cbs);
87 if (pkey == NULL || CBS_len(&cbs) != 0) {
88 EVP_PKEY_free(pkey);
89 goto err;
90 }
91
92 pub->pkey = pkey;
93
94 err:
95 OPENSSL_free(spki);
96 // If the operation failed, clear errors. An |X509_PUBKEY| whose key we cannot
97 // parse is still a valid SPKI. It just cannot be converted to an |EVP_PKEY|.
98 ERR_clear_error();
99 }
100
pubkey_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)101 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
102 void *exarg) {
103 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
104 if (operation == ASN1_OP_FREE_POST) {
105 EVP_PKEY_free(pubkey->pkey);
106 } else if (operation == ASN1_OP_D2I_POST) {
107 x509_pubkey_changed(pubkey);
108 }
109 return 1;
110 }
111
112 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
113 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
114 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING),
115 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
116
117 IMPLEMENT_ASN1_FUNCTIONS_const(X509_PUBKEY)
118
119 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) {
120 X509_PUBKEY *pk = NULL;
121 uint8_t *spki = NULL;
122 size_t spki_len;
123
124 if (x == NULL) {
125 return 0;
126 }
127
128 CBB cbb;
129 if (!CBB_init(&cbb, 0) || //
130 !EVP_marshal_public_key(&cbb, pkey) ||
131 !CBB_finish(&cbb, &spki, &spki_len) || //
132 spki_len > LONG_MAX) {
133 CBB_cleanup(&cbb);
134 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
135 goto error;
136 }
137
138 const uint8_t *p = spki;
139 pk = d2i_X509_PUBKEY(NULL, &p, (long)spki_len);
140 if (pk == NULL || p != spki + spki_len) {
141 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
142 goto error;
143 }
144
145 OPENSSL_free(spki);
146 X509_PUBKEY_free(*x);
147 *x = pk;
148
149 return 1;
150 error:
151 X509_PUBKEY_free(pk);
152 OPENSSL_free(spki);
153 return 0;
154 }
155
X509_PUBKEY_get0(const X509_PUBKEY * key)156 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key) {
157 if (key == NULL) {
158 return NULL;
159 }
160
161 if (key->pkey == NULL) {
162 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
163 return NULL;
164 }
165
166 return key->pkey;
167 }
168
X509_PUBKEY_get(const X509_PUBKEY * key)169 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) {
170 EVP_PKEY *pkey = X509_PUBKEY_get0(key);
171 if (pkey != NULL) {
172 EVP_PKEY_up_ref(pkey);
173 }
174 return pkey;
175 }
176
X509_PUBKEY_set0_param(X509_PUBKEY * pub,ASN1_OBJECT * obj,int param_type,void * param_value,uint8_t * key,int key_len)177 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type,
178 void *param_value, uint8_t *key, int key_len) {
179 if (!X509_ALGOR_set0(pub->algor, obj, param_type, param_value)) {
180 return 0;
181 }
182
183 ASN1_STRING_set0(pub->public_key, key, key_len);
184 // Set the number of unused bits to zero.
185 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
186 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
187
188 x509_pubkey_changed(pub);
189 return 1;
190 }
191
X509_PUBKEY_get0_param(ASN1_OBJECT ** out_obj,const uint8_t ** out_key,int * out_key_len,X509_ALGOR ** out_alg,X509_PUBKEY * pub)192 int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj, const uint8_t **out_key,
193 int *out_key_len, X509_ALGOR **out_alg,
194 X509_PUBKEY *pub) {
195 if (out_obj != NULL) {
196 *out_obj = pub->algor->algorithm;
197 }
198 if (out_key != NULL) {
199 *out_key = pub->public_key->data;
200 *out_key_len = pub->public_key->length;
201 }
202 if (out_alg != NULL) {
203 *out_alg = pub->algor;
204 }
205 return 1;
206 }
207
X509_PUBKEY_get0_public_key(const X509_PUBKEY * pub)208 const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) {
209 return pub->public_key;
210 }
211