1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
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.
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 (tjh@cryptsoft.com).
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 (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)"
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 #include <openssl/thread.h>
69
70 #include "../internal.h"
71 #include "internal.h"
72
73 // Minor tweak to operation: free up EVP_PKEY
pubkey_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)74 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
75 void *exarg) {
76 if (operation == ASN1_OP_FREE_POST) {
77 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
78 EVP_PKEY_free(pubkey->pkey);
79 }
80 return 1;
81 }
82
83 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
84 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
85 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING),
86 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
87
88 IMPLEMENT_ASN1_FUNCTIONS_const(X509_PUBKEY)
89
90 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) {
91 X509_PUBKEY *pk = NULL;
92 uint8_t *spki = NULL;
93 size_t spki_len;
94
95 if (x == NULL) {
96 return 0;
97 }
98
99 CBB cbb;
100 if (!CBB_init(&cbb, 0) || //
101 !EVP_marshal_public_key(&cbb, pkey) ||
102 !CBB_finish(&cbb, &spki, &spki_len) || //
103 spki_len > LONG_MAX) {
104 CBB_cleanup(&cbb);
105 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
106 goto error;
107 }
108
109 const uint8_t *p = spki;
110 pk = d2i_X509_PUBKEY(NULL, &p, (long)spki_len);
111 if (pk == NULL || p != spki + spki_len) {
112 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
113 goto error;
114 }
115
116 OPENSSL_free(spki);
117 X509_PUBKEY_free(*x);
118 *x = pk;
119
120 return 1;
121 error:
122 X509_PUBKEY_free(pk);
123 OPENSSL_free(spki);
124 return 0;
125 }
126
127 // g_pubkey_lock is used to protect the initialisation of the |pkey| member of
128 // |X509_PUBKEY| objects. Really |X509_PUBKEY| should have a |CRYPTO_once_t|
129 // inside it for this, but |CRYPTO_once_t| is private and |X509_PUBKEY| is
130 // not.
131 static struct CRYPTO_STATIC_MUTEX g_pubkey_lock = CRYPTO_STATIC_MUTEX_INIT;
132
X509_PUBKEY_get(X509_PUBKEY * key)133 EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) {
134 EVP_PKEY *ret = NULL;
135 uint8_t *spki = NULL;
136
137 if (key == NULL) {
138 goto error;
139 }
140
141 CRYPTO_STATIC_MUTEX_lock_read(&g_pubkey_lock);
142 if (key->pkey != NULL) {
143 CRYPTO_STATIC_MUTEX_unlock_read(&g_pubkey_lock);
144 EVP_PKEY_up_ref(key->pkey);
145 return key->pkey;
146 }
147 CRYPTO_STATIC_MUTEX_unlock_read(&g_pubkey_lock);
148
149 // Re-encode the |X509_PUBKEY| to DER and parse it.
150 int spki_len = i2d_X509_PUBKEY(key, &spki);
151 if (spki_len < 0) {
152 goto error;
153 }
154 CBS cbs;
155 CBS_init(&cbs, spki, (size_t)spki_len);
156 ret = EVP_parse_public_key(&cbs);
157 if (ret == NULL || CBS_len(&cbs) != 0) {
158 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
159 goto error;
160 }
161
162 // Check to see if another thread set key->pkey first
163 CRYPTO_STATIC_MUTEX_lock_write(&g_pubkey_lock);
164 if (key->pkey) {
165 CRYPTO_STATIC_MUTEX_unlock_write(&g_pubkey_lock);
166 EVP_PKEY_free(ret);
167 ret = key->pkey;
168 } else {
169 key->pkey = ret;
170 CRYPTO_STATIC_MUTEX_unlock_write(&g_pubkey_lock);
171 }
172
173 OPENSSL_free(spki);
174 EVP_PKEY_up_ref(ret);
175 return ret;
176
177 error:
178 OPENSSL_free(spki);
179 EVP_PKEY_free(ret);
180 return NULL;
181 }
182
X509_PUBKEY_set0_param(X509_PUBKEY * pub,ASN1_OBJECT * obj,int param_type,void * param_value,uint8_t * key,int key_len)183 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type,
184 void *param_value, uint8_t *key, int key_len) {
185 if (!X509_ALGOR_set0(pub->algor, obj, param_type, param_value)) {
186 return 0;
187 }
188
189 ASN1_STRING_set0(pub->public_key, key, key_len);
190 // Set the number of unused bits to zero.
191 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
192 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
193 return 1;
194 }
195
X509_PUBKEY_get0_param(ASN1_OBJECT ** out_obj,const uint8_t ** out_key,int * out_key_len,X509_ALGOR ** out_alg,X509_PUBKEY * pub)196 int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj, const uint8_t **out_key,
197 int *out_key_len, X509_ALGOR **out_alg,
198 X509_PUBKEY *pub) {
199 if (out_obj != NULL) {
200 *out_obj = pub->algor->algorithm;
201 }
202 if (out_key != NULL) {
203 *out_key = pub->public_key->data;
204 *out_key_len = pub->public_key->length;
205 }
206 if (out_alg != NULL) {
207 *out_alg = pub->algor;
208 }
209 return 1;
210 }
211
X509_PUBKEY_get0_public_key(const X509_PUBKEY * pub)212 const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) {
213 return pub->public_key;
214 }
215