/src/boringssl/crypto/x509/x_pubkey.c
Line | Count | Source (jump to first uncovered line) |
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 |
74 | | static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
75 | 815k | void *exarg) { |
76 | 815k | if (operation == ASN1_OP_FREE_POST) { |
77 | 143k | X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; |
78 | 143k | EVP_PKEY_free(pubkey->pkey); |
79 | 143k | } |
80 | 815k | return 1; |
81 | 815k | } |
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 | 0 | int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) { |
91 | 0 | X509_PUBKEY *pk = NULL; |
92 | 0 | uint8_t *spki = NULL; |
93 | 0 | size_t spki_len; |
94 | |
|
95 | 0 | if (x == NULL) { |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 0 | CBB cbb; |
100 | 0 | if (!CBB_init(&cbb, 0) || // |
101 | 0 | !EVP_marshal_public_key(&cbb, pkey) || |
102 | 0 | !CBB_finish(&cbb, &spki, &spki_len) || // |
103 | 0 | spki_len > LONG_MAX) { |
104 | 0 | CBB_cleanup(&cbb); |
105 | 0 | OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_ENCODE_ERROR); |
106 | 0 | goto error; |
107 | 0 | } |
108 | | |
109 | 0 | const uint8_t *p = spki; |
110 | 0 | pk = d2i_X509_PUBKEY(NULL, &p, (long)spki_len); |
111 | 0 | if (pk == NULL || p != spki + spki_len) { |
112 | 0 | OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR); |
113 | 0 | goto error; |
114 | 0 | } |
115 | | |
116 | 0 | OPENSSL_free(spki); |
117 | 0 | X509_PUBKEY_free(*x); |
118 | 0 | *x = pk; |
119 | |
|
120 | 0 | return 1; |
121 | 0 | error: |
122 | 0 | X509_PUBKEY_free(pk); |
123 | 0 | OPENSSL_free(spki); |
124 | 0 | return 0; |
125 | 0 | } |
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 | | |
133 | 7.02k | EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) { |
134 | 7.02k | EVP_PKEY *ret = NULL; |
135 | 7.02k | uint8_t *spki = NULL; |
136 | | |
137 | 7.02k | if (key == NULL) { |
138 | 0 | goto error; |
139 | 0 | } |
140 | | |
141 | 7.02k | CRYPTO_STATIC_MUTEX_lock_read(&g_pubkey_lock); |
142 | 7.02k | if (key->pkey != NULL) { |
143 | 332 | CRYPTO_STATIC_MUTEX_unlock_read(&g_pubkey_lock); |
144 | 332 | EVP_PKEY_up_ref(key->pkey); |
145 | 332 | return key->pkey; |
146 | 332 | } |
147 | 6.69k | CRYPTO_STATIC_MUTEX_unlock_read(&g_pubkey_lock); |
148 | | |
149 | | // Re-encode the |X509_PUBKEY| to DER and parse it. |
150 | 6.69k | int spki_len = i2d_X509_PUBKEY(key, &spki); |
151 | 6.69k | if (spki_len < 0) { |
152 | 0 | goto error; |
153 | 0 | } |
154 | 6.69k | CBS cbs; |
155 | 6.69k | CBS_init(&cbs, spki, (size_t)spki_len); |
156 | 6.69k | ret = EVP_parse_public_key(&cbs); |
157 | 6.69k | if (ret == NULL || CBS_len(&cbs) != 0) { |
158 | 5.85k | OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR); |
159 | 5.85k | goto error; |
160 | 5.85k | } |
161 | | |
162 | | // Check to see if another thread set key->pkey first |
163 | 844 | CRYPTO_STATIC_MUTEX_lock_write(&g_pubkey_lock); |
164 | 844 | if (key->pkey) { |
165 | 0 | CRYPTO_STATIC_MUTEX_unlock_write(&g_pubkey_lock); |
166 | 0 | EVP_PKEY_free(ret); |
167 | 0 | ret = key->pkey; |
168 | 844 | } else { |
169 | 844 | key->pkey = ret; |
170 | 844 | CRYPTO_STATIC_MUTEX_unlock_write(&g_pubkey_lock); |
171 | 844 | } |
172 | | |
173 | 844 | OPENSSL_free(spki); |
174 | 844 | EVP_PKEY_up_ref(ret); |
175 | 844 | return ret; |
176 | | |
177 | 5.85k | error: |
178 | 5.85k | OPENSSL_free(spki); |
179 | 5.85k | EVP_PKEY_free(ret); |
180 | 5.85k | return NULL; |
181 | 6.69k | } |
182 | | |
183 | | int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type, |
184 | 0 | void *param_value, uint8_t *key, int key_len) { |
185 | 0 | if (!X509_ALGOR_set0(pub->algor, obj, param_type, param_value)) { |
186 | 0 | return 0; |
187 | 0 | } |
188 | | |
189 | 0 | ASN1_STRING_set0(pub->public_key, key, key_len); |
190 | | // Set the number of unused bits to zero. |
191 | 0 | pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
192 | 0 | pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
193 | 0 | return 1; |
194 | 0 | } |
195 | | |
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 | 0 | X509_PUBKEY *pub) { |
199 | 0 | if (out_obj != NULL) { |
200 | 0 | *out_obj = pub->algor->algorithm; |
201 | 0 | } |
202 | 0 | if (out_key != NULL) { |
203 | 0 | *out_key = pub->public_key->data; |
204 | 0 | *out_key_len = pub->public_key->length; |
205 | 0 | } |
206 | 0 | if (out_alg != NULL) { |
207 | 0 | *out_alg = pub->algor; |
208 | 0 | } |
209 | 0 | return 1; |
210 | 0 | } |
211 | | |
212 | 0 | const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) { |
213 | 0 | return pub->public_key; |
214 | 0 | } |