| 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 |
#include <openssl/thread.h> |
| 69 |
|
| 70 |
#include "../internal.h" |
| 71 |
|
| 72 |
/* Minor tweak to operation: free up EVP_PKEY */ |
| 73 |
static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 74 |
void *exarg) |
| 75 |
{ |
| 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(X509_PUBKEY) |
| 89 |
|
| 90 |
int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) |
| 91 |
{ |
| 92 |
X509_PUBKEY *pk = NULL; |
| 93 |
uint8_t *spki = NULL; |
| 94 |
size_t spki_len; |
| 95 |
|
| 96 |
if (x == NULL) |
| 97 |
return (0); |
| 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 |
|
| 133 |
EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) |
| 134 |
{ |
| 135 |
EVP_PKEY *ret = NULL; |
| 136 |
uint8_t *spki = NULL; |
| 137 |
|
| 138 |
if (key == NULL) |
| 139 |
goto error; |
| 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 |
return EVP_PKEY_up_ref(key->pkey); |
| 145 |
} |
| 146 |
CRYPTO_STATIC_MUTEX_unlock_read(&g_pubkey_lock); |
| 147 |
|
| 148 |
/* Re-encode the |X509_PUBKEY| to DER and parse it. */ |
| 149 |
int spki_len = i2d_X509_PUBKEY(key, &spki); |
| 150 |
if (spki_len < 0) { |
| 151 |
goto error; |
| 152 |
} |
| 153 |
CBS cbs; |
| 154 |
CBS_init(&cbs, spki, (size_t)spki_len); |
| 155 |
ret = EVP_parse_public_key(&cbs); |
| 156 |
if (ret == NULL || CBS_len(&cbs) != 0) { |
| 157 |
OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR); |
| 158 |
goto error; |
| 159 |
} |
| 160 |
|
| 161 |
/* Check to see if another thread set key->pkey first */ |
| 162 |
CRYPTO_STATIC_MUTEX_lock_write(&g_pubkey_lock); |
| 163 |
if (key->pkey) { |
| 164 |
CRYPTO_STATIC_MUTEX_unlock_write(&g_pubkey_lock); |
| 165 |
EVP_PKEY_free(ret); |
| 166 |
ret = key->pkey; |
| 167 |
} else { |
| 168 |
key->pkey = ret; |
| 169 |
CRYPTO_STATIC_MUTEX_unlock_write(&g_pubkey_lock); |
| 170 |
} |
| 171 |
|
| 172 |
OPENSSL_free(spki); |
| 173 |
return EVP_PKEY_up_ref(ret); |
| 174 |
|
| 175 |
error: |
| 176 |
OPENSSL_free(spki); |
| 177 |
EVP_PKEY_free(ret); |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or |
| 183 |
* decode as X509_PUBKEY |
| 184 |
*/ |
| 185 |
|
| 186 |
EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length) |
| 187 |
{ |
| 188 |
X509_PUBKEY *xpk; |
| 189 |
EVP_PKEY *pktmp; |
| 190 |
xpk = d2i_X509_PUBKEY(NULL, pp, length); |
| 191 |
if (!xpk) |
| 192 |
return NULL; |
| 193 |
pktmp = X509_PUBKEY_get(xpk); |
| 194 |
X509_PUBKEY_free(xpk); |
| 195 |
if (!pktmp) |
| 196 |
return NULL; |
| 197 |
if (a) { |
| 198 |
EVP_PKEY_free(*a); |
| 199 |
*a = pktmp; |
| 200 |
} |
| 201 |
return pktmp; |
| 202 |
} |
| 203 |
|
| 204 |
int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp) |
| 205 |
{ |
| 206 |
X509_PUBKEY *xpk = NULL; |
| 207 |
int ret; |
| 208 |
if (!a) |
| 209 |
return 0; |
| 210 |
if (!X509_PUBKEY_set(&xpk, (EVP_PKEY *)a)) |
| 211 |
return 0; |
| 212 |
ret = i2d_X509_PUBKEY(xpk, pp); |
| 213 |
X509_PUBKEY_free(xpk); |
| 214 |
return ret; |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* The following are equivalents but which return RSA and DSA keys |
| 219 |
*/ |
| 220 |
RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length) |
| 221 |
{ |
| 222 |
EVP_PKEY *pkey; |
| 223 |
RSA *key; |
| 224 |
const unsigned char *q; |
| 225 |
q = *pp; |
| 226 |
pkey = d2i_PUBKEY(NULL, &q, length); |
| 227 |
if (!pkey) |
| 228 |
return NULL; |
| 229 |
key = EVP_PKEY_get1_RSA(pkey); |
| 230 |
EVP_PKEY_free(pkey); |
| 231 |
if (!key) |
| 232 |
return NULL; |
| 233 |
*pp = q; |
| 234 |
if (a) { |
| 235 |
RSA_free(*a); |
| 236 |
*a = key; |
| 237 |
} |
| 238 |
return key; |
| 239 |
} |
| 240 |
|
| 241 |
int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp) |
| 242 |
{ |
| 243 |
EVP_PKEY *pktmp; |
| 244 |
int ret; |
| 245 |
if (!a) |
| 246 |
return 0; |
| 247 |
pktmp = EVP_PKEY_new(); |
| 248 |
if (!pktmp) { |
| 249 |
OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE); |
| 250 |
return 0; |
| 251 |
} |
| 252 |
EVP_PKEY_set1_RSA(pktmp, (RSA *)a); |
| 253 |
ret = i2d_PUBKEY(pktmp, pp); |
| 254 |
EVP_PKEY_free(pktmp); |
| 255 |
return ret; |
| 256 |
} |
| 257 |
|
| 258 |
#ifndef OPENSSL_NO_DSA |
| 259 |
DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length) |
| 260 |
{ |
| 261 |
EVP_PKEY *pkey; |
| 262 |
DSA *key; |
| 263 |
const unsigned char *q; |
| 264 |
q = *pp; |
| 265 |
pkey = d2i_PUBKEY(NULL, &q, length); |
| 266 |
if (!pkey) |
| 267 |
return NULL; |
| 268 |
key = EVP_PKEY_get1_DSA(pkey); |
| 269 |
EVP_PKEY_free(pkey); |
| 270 |
if (!key) |
| 271 |
return NULL; |
| 272 |
*pp = q; |
| 273 |
if (a) { |
| 274 |
DSA_free(*a); |
| 275 |
*a = key; |
| 276 |
} |
| 277 |
return key; |
| 278 |
} |
| 279 |
|
| 280 |
int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp) |
| 281 |
{ |
| 282 |
EVP_PKEY *pktmp; |
| 283 |
int ret; |
| 284 |
if (!a) |
| 285 |
return 0; |
| 286 |
pktmp = EVP_PKEY_new(); |
| 287 |
if (!pktmp) { |
| 288 |
OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
EVP_PKEY_set1_DSA(pktmp, (DSA *)a); |
| 292 |
ret = i2d_PUBKEY(pktmp, pp); |
| 293 |
EVP_PKEY_free(pktmp); |
| 294 |
return ret; |
| 295 |
} |
| 296 |
#endif |
| 297 |
|
| 298 |
EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length) |
| 299 |
{ |
| 300 |
EVP_PKEY *pkey; |
| 301 |
EC_KEY *key; |
| 302 |
const unsigned char *q; |
| 303 |
q = *pp; |
| 304 |
pkey = d2i_PUBKEY(NULL, &q, length); |
| 305 |
if (!pkey) |
| 306 |
return (NULL); |
| 307 |
key = EVP_PKEY_get1_EC_KEY(pkey); |
| 308 |
EVP_PKEY_free(pkey); |
| 309 |
if (!key) |
| 310 |
return (NULL); |
| 311 |
*pp = q; |
| 312 |
if (a) { |
| 313 |
EC_KEY_free(*a); |
| 314 |
*a = key; |
| 315 |
} |
| 316 |
return (key); |
| 317 |
} |
| 318 |
|
| 319 |
int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp) |
| 320 |
{ |
| 321 |
EVP_PKEY *pktmp; |
| 322 |
int ret; |
| 323 |
if (!a) |
| 324 |
return (0); |
| 325 |
if ((pktmp = EVP_PKEY_new()) == NULL) { |
| 326 |
OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE); |
| 327 |
return (0); |
| 328 |
} |
| 329 |
EVP_PKEY_set1_EC_KEY(pktmp, (EC_KEY *)a); |
| 330 |
ret = i2d_PUBKEY(pktmp, pp); |
| 331 |
EVP_PKEY_free(pktmp); |
| 332 |
return (ret); |
| 333 |
} |
| 334 |
|
| 335 |
int X509_PUBKEY_set0_param(X509_PUBKEY *pub, const ASN1_OBJECT *aobj, |
| 336 |
int ptype, void *pval, |
| 337 |
unsigned char *penc, int penclen) |
| 338 |
{ |
| 339 |
if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) |
| 340 |
return 0; |
| 341 |
if (penc) { |
| 342 |
if (pub->public_key->data) |
| 343 |
OPENSSL_free(pub->public_key->data); |
| 344 |
pub->public_key->data = penc; |
| 345 |
pub->public_key->length = penclen; |
| 346 |
/* Set number of unused bits to zero */ |
| 347 |
pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
| 348 |
pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
| 349 |
} |
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, |
| 354 |
const unsigned char **pk, int *ppklen, |
| 355 |
X509_ALGOR **pa, X509_PUBKEY *pub) |
| 356 |
{ |
| 357 |
if (ppkalg) |
| 358 |
*ppkalg = pub->algor->algorithm; |
| 359 |
if (pk) { |
| 360 |
*pk = pub->public_key->data; |
| 361 |
*ppklen = pub->public_key->length; |
| 362 |
} |
| 363 |
if (pa) |
| 364 |
*pa = pub->algor; |
| 365 |
return 1; |
| 366 |
} |