| 1 |
/* crypto/asn1/n_pkey.c */ |
| 2 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This package is an SSL implementation written |
| 6 |
* by Eric Young ([email protected]). |
| 7 |
* The implementation was written so as to conform with Netscapes SSL. |
| 8 |
* |
| 9 |
* This library is free for commercial and non-commercial use as long as |
| 10 |
* the following conditions are aheared to. The following conditions |
| 11 |
* apply to all code found in this distribution, be it the RC4, RSA, |
| 12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 |
* included with this distribution is covered by the same copyright terms |
| 14 |
* except that the holder is Tim Hudson ([email protected]). |
| 15 |
* |
| 16 |
* Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 |
* the code are not to be removed. |
| 18 |
* If this package is used in a product, Eric Young should be given attribution |
| 19 |
* as the author of the parts of the library used. |
| 20 |
* This can be in the form of a textual message at program startup or |
| 21 |
* in documentation (online or textual) provided with the package. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. All advertising materials mentioning features or use of this software |
| 32 |
* must display the following acknowledgement: |
| 33 |
* "This product includes cryptographic software written by |
| 34 |
* Eric Young ([email protected])" |
| 35 |
* The word 'cryptographic' can be left out if the rouines from the library |
| 36 |
* being used are not cryptographic related :-). |
| 37 |
* 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 |
* the apps directory (application code) you must include an acknowledgement: |
| 39 |
* "This product includes software written by Tim Hudson ([email protected])" |
| 40 |
* |
| 41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 |
* SUCH DAMAGE. |
| 52 |
* |
| 53 |
* The licence and distribution terms for any publically available version or |
| 54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 |
* copied and put under another distribution licence |
| 56 |
* [including the GNU Public Licence.] |
| 57 |
*/ |
| 58 |
|
| 59 |
#include <stdio.h> |
| 60 |
#include "cryptlib.h" |
| 61 |
#ifndef OPENSSL_NO_RSA |
| 62 |
# include <openssl/rsa.h> |
| 63 |
# include <openssl/objects.h> |
| 64 |
# include <openssl/asn1t.h> |
| 65 |
# include <openssl/asn1_mac.h> |
| 66 |
# include <openssl/evp.h> |
| 67 |
# include <openssl/x509.h> |
| 68 |
|
| 69 |
# ifndef OPENSSL_NO_RC4 |
| 70 |
|
| 71 |
typedef struct netscape_pkey_st { |
| 72 |
long version; |
| 73 |
X509_ALGOR *algor; |
| 74 |
ASN1_OCTET_STRING *private_key; |
| 75 |
} NETSCAPE_PKEY; |
| 76 |
|
| 77 |
typedef struct netscape_encrypted_pkey_st { |
| 78 |
ASN1_OCTET_STRING *os; |
| 79 |
/* |
| 80 |
* This is the same structure as DigestInfo so use it: although this |
| 81 |
* isn't really anything to do with digests. |
| 82 |
*/ |
| 83 |
X509_SIG *enckey; |
| 84 |
} NETSCAPE_ENCRYPTED_PKEY; |
| 85 |
|
| 86 |
|
| 87 |
ASN1_BROKEN_SEQUENCE(NETSCAPE_ENCRYPTED_PKEY) = { |
| 88 |
ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, os, ASN1_OCTET_STRING), |
| 89 |
ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, enckey, X509_SIG) |
| 90 |
} ASN1_BROKEN_SEQUENCE_END(NETSCAPE_ENCRYPTED_PKEY) |
| 91 |
|
| 92 |
DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY) |
| 93 |
DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY,NETSCAPE_ENCRYPTED_PKEY) |
| 94 |
IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY) |
| 95 |
|
| 96 |
ASN1_SEQUENCE(NETSCAPE_PKEY) = { |
| 97 |
ASN1_SIMPLE(NETSCAPE_PKEY, version, LONG), |
| 98 |
ASN1_SIMPLE(NETSCAPE_PKEY, algor, X509_ALGOR), |
| 99 |
ASN1_SIMPLE(NETSCAPE_PKEY, private_key, ASN1_OCTET_STRING) |
| 100 |
} ASN1_SEQUENCE_END(NETSCAPE_PKEY) |
| 101 |
|
| 102 |
DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_PKEY) |
| 103 |
DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_PKEY,NETSCAPE_PKEY) |
| 104 |
IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_PKEY) |
| 105 |
|
| 106 |
static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os, |
| 107 |
int (*cb) (char *buf, int len, const char *prompt, |
| 108 |
int verify), int sgckey); |
| 109 |
|
| 110 |
int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, |
| 111 |
int (*cb) (char *buf, int len, const char *prompt, |
| 112 |
int verify)) |
| 113 |
{ |
| 114 |
return i2d_RSA_NET(a, pp, cb, 0); |
| 115 |
} |
| 116 |
|
| 117 |
int i2d_RSA_NET(const RSA *a, unsigned char **pp, |
| 118 |
int (*cb) (char *buf, int len, const char *prompt, |
| 119 |
int verify), int sgckey) |
| 120 |
{ |
| 121 |
int i, j, ret = 0; |
| 122 |
int rsalen, pkeylen, olen; |
| 123 |
NETSCAPE_PKEY *pkey = NULL; |
| 124 |
NETSCAPE_ENCRYPTED_PKEY *enckey = NULL; |
| 125 |
unsigned char buf[256], *zz; |
| 126 |
unsigned char key[EVP_MAX_KEY_LENGTH]; |
| 127 |
EVP_CIPHER_CTX ctx; |
| 128 |
EVP_CIPHER_CTX_init(&ctx); |
| 129 |
|
| 130 |
if (a == NULL) |
| 131 |
return (0); |
| 132 |
|
| 133 |
if ((pkey = NETSCAPE_PKEY_new()) == NULL) |
| 134 |
goto err; |
| 135 |
if ((enckey = NETSCAPE_ENCRYPTED_PKEY_new()) == NULL) |
| 136 |
goto err; |
| 137 |
pkey->version = 0; |
| 138 |
|
| 139 |
pkey->algor->algorithm = OBJ_nid2obj(NID_rsaEncryption); |
| 140 |
if ((pkey->algor->parameter = ASN1_TYPE_new()) == NULL) |
| 141 |
goto err; |
| 142 |
pkey->algor->parameter->type = V_ASN1_NULL; |
| 143 |
|
| 144 |
rsalen = i2d_RSAPrivateKey(a, NULL); |
| 145 |
|
| 146 |
/* |
| 147 |
* Fake some octet strings just for the initial length calculation. |
| 148 |
*/ |
| 149 |
|
| 150 |
pkey->private_key->length = rsalen; |
| 151 |
|
| 152 |
pkeylen = i2d_NETSCAPE_PKEY(pkey, NULL); |
| 153 |
|
| 154 |
enckey->enckey->digest->length = pkeylen; |
| 155 |
|
| 156 |
enckey->os->length = 11; /* "private-key" */ |
| 157 |
|
| 158 |
enckey->enckey->algor->algorithm = OBJ_nid2obj(NID_rc4); |
| 159 |
if ((enckey->enckey->algor->parameter = ASN1_TYPE_new()) == NULL) |
| 160 |
goto err; |
| 161 |
enckey->enckey->algor->parameter->type = V_ASN1_NULL; |
| 162 |
|
| 163 |
if (pp == NULL) { |
| 164 |
olen = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, NULL); |
| 165 |
NETSCAPE_PKEY_free(pkey); |
| 166 |
NETSCAPE_ENCRYPTED_PKEY_free(enckey); |
| 167 |
return olen; |
| 168 |
} |
| 169 |
|
| 170 |
/* Since its RC4 encrypted length is actual length */ |
| 171 |
if ((zz = (unsigned char *)OPENSSL_malloc(rsalen)) == NULL) { |
| 172 |
ASN1err(ASN1_F_I2D_RSA_NET, ERR_R_MALLOC_FAILURE); |
| 173 |
goto err; |
| 174 |
} |
| 175 |
|
| 176 |
pkey->private_key->data = zz; |
| 177 |
/* Write out private key encoding */ |
| 178 |
i2d_RSAPrivateKey(a, &zz); |
| 179 |
|
| 180 |
if ((zz = OPENSSL_malloc(pkeylen)) == NULL) { |
| 181 |
ASN1err(ASN1_F_I2D_RSA_NET, ERR_R_MALLOC_FAILURE); |
| 182 |
goto err; |
| 183 |
} |
| 184 |
|
| 185 |
if (!ASN1_STRING_set(enckey->os, "private-key", -1)) { |
| 186 |
ASN1err(ASN1_F_I2D_RSA_NET, ERR_R_MALLOC_FAILURE); |
| 187 |
goto err; |
| 188 |
} |
| 189 |
enckey->enckey->digest->data = zz; |
| 190 |
i2d_NETSCAPE_PKEY(pkey, &zz); |
| 191 |
|
| 192 |
/* Wipe the private key encoding */ |
| 193 |
OPENSSL_cleanse(pkey->private_key->data, rsalen); |
| 194 |
|
| 195 |
if (cb == NULL) |
| 196 |
#ifndef OPENSSL_NO_UI |
| 197 |
cb = EVP_read_pw_string; |
| 198 |
#else |
| 199 |
i = 1; |
| 200 |
else |
| 201 |
#endif |
| 202 |
i = cb((char *)buf, 256, "Enter Private Key password:", 1); |
| 203 |
if (i != 0) { |
| 204 |
ASN1err(ASN1_F_I2D_RSA_NET, ASN1_R_BAD_PASSWORD_READ); |
| 205 |
goto err; |
| 206 |
} |
| 207 |
i = strlen((char *)buf); |
| 208 |
/* If the key is used for SGC the algorithm is modified a little. */ |
| 209 |
if (sgckey) { |
| 210 |
if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL)) |
| 211 |
goto err; |
| 212 |
memcpy(buf + 16, "SGCKEYSALT", 10); |
| 213 |
i = 26; |
| 214 |
} |
| 215 |
|
| 216 |
if (!EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, buf, i, 1, key, NULL)) |
| 217 |
goto err; |
| 218 |
OPENSSL_cleanse(buf, 256); |
| 219 |
|
| 220 |
/* Encrypt private key in place */ |
| 221 |
zz = enckey->enckey->digest->data; |
| 222 |
if (!EVP_EncryptInit_ex(&ctx, EVP_rc4(), NULL, key, NULL)) |
| 223 |
goto err; |
| 224 |
if (!EVP_EncryptUpdate(&ctx, zz, &i, zz, pkeylen)) |
| 225 |
goto err; |
| 226 |
if (!EVP_EncryptFinal_ex(&ctx, zz + i, &j)) |
| 227 |
goto err; |
| 228 |
|
| 229 |
ret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp); |
| 230 |
err: |
| 231 |
EVP_CIPHER_CTX_cleanup(&ctx); |
| 232 |
NETSCAPE_ENCRYPTED_PKEY_free(enckey); |
| 233 |
NETSCAPE_PKEY_free(pkey); |
| 234 |
return (ret); |
| 235 |
} |
| 236 |
|
| 237 |
RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, |
| 238 |
int (*cb) (char *buf, int len, const char *prompt, |
| 239 |
int verify)) |
| 240 |
{ |
| 241 |
return d2i_RSA_NET(a, pp, length, cb, 0); |
| 242 |
} |
| 243 |
|
| 244 |
RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, |
| 245 |
int (*cb) (char *buf, int len, const char *prompt, |
| 246 |
int verify), int sgckey) |
| 247 |
{ |
| 248 |
RSA *ret = NULL; |
| 249 |
const unsigned char *p; |
| 250 |
NETSCAPE_ENCRYPTED_PKEY *enckey = NULL; |
| 251 |
|
| 252 |
p = *pp; |
| 253 |
|
| 254 |
enckey = d2i_NETSCAPE_ENCRYPTED_PKEY(NULL, &p, length); |
| 255 |
if (!enckey) { |
| 256 |
ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_DECODING_ERROR); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
if ((enckey->os->length != 11) || (strncmp("private-key", |
| 261 |
(char *)enckey->os->data, |
| 262 |
11) != 0)) { |
| 263 |
ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_PRIVATE_KEY_HEADER_MISSING); |
| 264 |
NETSCAPE_ENCRYPTED_PKEY_free(enckey); |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4) { |
| 268 |
ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM); |
| 269 |
goto err; |
| 270 |
} |
| 271 |
if (cb == NULL) |
| 272 |
#ifndef OPENSSL_NO_UI |
| 273 |
cb = EVP_read_pw_string; |
| 274 |
#else |
| 275 |
goto err; |
| 276 |
#endif |
| 277 |
if ((ret = d2i_RSA_NET_2(a, enckey->enckey->digest, cb, sgckey)) == NULL) |
| 278 |
goto err; |
| 279 |
|
| 280 |
*pp = p; |
| 281 |
|
| 282 |
err: |
| 283 |
NETSCAPE_ENCRYPTED_PKEY_free(enckey); |
| 284 |
return ret; |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os, |
| 289 |
int (*cb) (char *buf, int len, const char *prompt, |
| 290 |
int verify), int sgckey) |
| 291 |
{ |
| 292 |
NETSCAPE_PKEY *pkey = NULL; |
| 293 |
RSA *ret = NULL; |
| 294 |
int i, j; |
| 295 |
unsigned char buf[256]; |
| 296 |
const unsigned char *zz; |
| 297 |
unsigned char key[EVP_MAX_KEY_LENGTH]; |
| 298 |
EVP_CIPHER_CTX ctx; |
| 299 |
EVP_CIPHER_CTX_init(&ctx); |
| 300 |
|
| 301 |
i = cb((char *)buf, 256, "Enter Private Key password:", 0); |
| 302 |
if (i != 0) { |
| 303 |
ASN1err(ASN1_F_D2I_RSA_NET_2, ASN1_R_BAD_PASSWORD_READ); |
| 304 |
goto err; |
| 305 |
} |
| 306 |
|
| 307 |
i = strlen((char *)buf); |
| 308 |
if (sgckey) { |
| 309 |
if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL)) |
| 310 |
goto err; |
| 311 |
memcpy(buf + 16, "SGCKEYSALT", 10); |
| 312 |
i = 26; |
| 313 |
} |
| 314 |
|
| 315 |
if (!EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, buf, i, 1, key, NULL)) |
| 316 |
goto err; |
| 317 |
OPENSSL_cleanse(buf, 256); |
| 318 |
|
| 319 |
if (!EVP_DecryptInit_ex(&ctx, EVP_rc4(), NULL, key, NULL)) |
| 320 |
goto err; |
| 321 |
if (!EVP_DecryptUpdate(&ctx, os->data, &i, os->data, os->length)) |
| 322 |
goto err; |
| 323 |
if (!EVP_DecryptFinal_ex(&ctx, &(os->data[i]), &j)) |
| 324 |
goto err; |
| 325 |
os->length = i + j; |
| 326 |
|
| 327 |
zz = os->data; |
| 328 |
|
| 329 |
if ((pkey = d2i_NETSCAPE_PKEY(NULL, &zz, os->length)) == NULL) { |
| 330 |
ASN1err(ASN1_F_D2I_RSA_NET_2, |
| 331 |
ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY); |
| 332 |
goto err; |
| 333 |
} |
| 334 |
|
| 335 |
zz = pkey->private_key->data; |
| 336 |
if ((ret = d2i_RSAPrivateKey(a, &zz, pkey->private_key->length)) == NULL) { |
| 337 |
ASN1err(ASN1_F_D2I_RSA_NET_2, ASN1_R_UNABLE_TO_DECODE_RSA_KEY); |
| 338 |
goto err; |
| 339 |
} |
| 340 |
err: |
| 341 |
EVP_CIPHER_CTX_cleanup(&ctx); |
| 342 |
NETSCAPE_PKEY_free(pkey); |
| 343 |
return (ret); |
| 344 |
} |
| 345 |
|
| 346 |
# endif /* OPENSSL_NO_RC4 */ |
| 347 |
|
| 348 |
#else /* !OPENSSL_NO_RSA */ |
| 349 |
|
| 350 |
# if PEDANTIC |
| 351 |
static void *dummy = &dummy; |
| 352 |
# endif |
| 353 |
|
| 354 |
#endif |