n_pkey.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* crypto/asn1/n_pkey.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  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 (tjh@cryptsoft.com).
  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 (eay@cryptsoft.com)"
  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 (tjh@cryptsoft.com)"
  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. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #ifndef OPENSSL_NO_RSA
  61. #include <openssl/rsa.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/asn1t.h>
  64. #include <openssl/asn1_mac.h>
  65. #include <openssl/evp.h>
  66. #include <openssl/x509.h>
  67. #ifndef OPENSSL_NO_RC4
  68. typedef struct netscape_pkey_st
  69. {
  70. long version;
  71. X509_ALGOR *algor;
  72. ASN1_OCTET_STRING *private_key;
  73. } NETSCAPE_PKEY;
  74. typedef struct netscape_encrypted_pkey_st
  75. {
  76. ASN1_OCTET_STRING *os;
  77. /* This is the same structure as DigestInfo so use it:
  78. * although this isn't really anything to do with
  79. * digests.
  80. */
  81. X509_SIG *enckey;
  82. } NETSCAPE_ENCRYPTED_PKEY;
  83. ASN1_BROKEN_SEQUENCE(NETSCAPE_ENCRYPTED_PKEY) = {
  84. ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, os, ASN1_OCTET_STRING),
  85. ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, enckey, X509_SIG)
  86. } ASN1_BROKEN_SEQUENCE_END(NETSCAPE_ENCRYPTED_PKEY)
  87. DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY)
  88. DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY,NETSCAPE_ENCRYPTED_PKEY)
  89. IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY)
  90. ASN1_SEQUENCE(NETSCAPE_PKEY) = {
  91. ASN1_SIMPLE(NETSCAPE_PKEY, version, LONG),
  92. ASN1_SIMPLE(NETSCAPE_PKEY, algor, X509_ALGOR),
  93. ASN1_SIMPLE(NETSCAPE_PKEY, private_key, ASN1_OCTET_STRING)
  94. } ASN1_SEQUENCE_END(NETSCAPE_PKEY)
  95. DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_PKEY)
  96. DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_PKEY,NETSCAPE_PKEY)
  97. IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_PKEY)
  98. static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
  99. int (*cb)(char *buf, int len, const char *prompt,
  100. int verify),
  101. int sgckey);
  102. int i2d_Netscape_RSA(const RSA *a, unsigned char **pp,
  103. int (*cb)(char *buf, int len, const char *prompt,
  104. int verify))
  105. {
  106. return i2d_RSA_NET(a, pp, cb, 0);
  107. }
  108. int i2d_RSA_NET(const RSA *a, unsigned char **pp,
  109. int (*cb)(char *buf, int len, const char *prompt, int verify),
  110. int sgckey)
  111. {
  112. int i, j, ret = 0;
  113. int rsalen, pkeylen, olen;
  114. NETSCAPE_PKEY *pkey = NULL;
  115. NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
  116. unsigned char buf[256],*zz;
  117. unsigned char key[EVP_MAX_KEY_LENGTH];
  118. EVP_CIPHER_CTX ctx;
  119. EVP_CIPHER_CTX_init(&ctx);
  120. if (a == NULL) return(0);
  121. if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
  122. if ((enckey=NETSCAPE_ENCRYPTED_PKEY_new()) == NULL) goto err;
  123. pkey->version = 0;
  124. pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
  125. if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
  126. pkey->algor->parameter->type=V_ASN1_NULL;
  127. rsalen = i2d_RSAPrivateKey(a, NULL);
  128. /* Fake some octet strings just for the initial length
  129. * calculation.
  130. */
  131. pkey->private_key->length=rsalen;
  132. pkeylen=i2d_NETSCAPE_PKEY(pkey,NULL);
  133. enckey->enckey->digest->length = pkeylen;
  134. enckey->os->length = 11; /* "private-key" */
  135. enckey->enckey->algor->algorithm=OBJ_nid2obj(NID_rc4);
  136. if ((enckey->enckey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
  137. enckey->enckey->algor->parameter->type=V_ASN1_NULL;
  138. if (pp == NULL)
  139. {
  140. olen = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, NULL);
  141. NETSCAPE_PKEY_free(pkey);
  142. NETSCAPE_ENCRYPTED_PKEY_free(enckey);
  143. return olen;
  144. }
  145. /* Since its RC4 encrypted length is actual length */
  146. if ((zz=(unsigned char *)OPENSSL_malloc(rsalen)) == NULL)
  147. {
  148. ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);
  149. goto err;
  150. }
  151. pkey->private_key->data = zz;
  152. /* Write out private key encoding */
  153. i2d_RSAPrivateKey(a,&zz);
  154. if ((zz=OPENSSL_malloc(pkeylen)) == NULL)
  155. {
  156. ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);
  157. goto err;
  158. }
  159. if (!ASN1_STRING_set(enckey->os, "private-key", -1))
  160. {
  161. ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);
  162. goto err;
  163. }
  164. enckey->enckey->digest->data = zz;
  165. i2d_NETSCAPE_PKEY(pkey,&zz);
  166. /* Wipe the private key encoding */
  167. OPENSSL_cleanse(pkey->private_key->data, rsalen);
  168. if (cb == NULL)
  169. cb=EVP_read_pw_string;
  170. i=cb((char *)buf,256,"Enter Private Key password:",1);
  171. if (i != 0)
  172. {
  173. ASN1err(ASN1_F_I2D_RSA_NET,ASN1_R_BAD_PASSWORD_READ);
  174. goto err;
  175. }
  176. i = strlen((char *)buf);
  177. /* If the key is used for SGC the algorithm is modified a little. */
  178. if(sgckey) {
  179. if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL))
  180. goto err;
  181. memcpy(buf + 16, "SGCKEYSALT", 10);
  182. i = 26;
  183. }
  184. if (!EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL))
  185. goto err;
  186. OPENSSL_cleanse(buf,256);
  187. /* Encrypt private key in place */
  188. zz = enckey->enckey->digest->data;
  189. if (!EVP_EncryptInit_ex(&ctx,EVP_rc4(),NULL,key,NULL))
  190. goto err;
  191. if (!EVP_EncryptUpdate(&ctx,zz,&i,zz,pkeylen))
  192. goto err;
  193. if (!EVP_EncryptFinal_ex(&ctx,zz + i,&j))
  194. goto err;
  195. ret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp);
  196. err:
  197. EVP_CIPHER_CTX_cleanup(&ctx);
  198. NETSCAPE_ENCRYPTED_PKEY_free(enckey);
  199. NETSCAPE_PKEY_free(pkey);
  200. return(ret);
  201. }
  202. RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length,
  203. int (*cb)(char *buf, int len, const char *prompt,
  204. int verify))
  205. {
  206. return d2i_RSA_NET(a, pp, length, cb, 0);
  207. }
  208. RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
  209. int (*cb)(char *buf, int len, const char *prompt, int verify),
  210. int sgckey)
  211. {
  212. RSA *ret=NULL;
  213. const unsigned char *p;
  214. NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
  215. p = *pp;
  216. enckey = d2i_NETSCAPE_ENCRYPTED_PKEY(NULL, &p, length);
  217. if(!enckey) {
  218. ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_DECODING_ERROR);
  219. return NULL;
  220. }
  221. if ((enckey->os->length != 11) || (strncmp("private-key",
  222. (char *)enckey->os->data,11) != 0))
  223. {
  224. ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
  225. NETSCAPE_ENCRYPTED_PKEY_free(enckey);
  226. return NULL;
  227. }
  228. if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4)
  229. {
  230. ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
  231. goto err;
  232. }
  233. if (cb == NULL)
  234. cb=EVP_read_pw_string;
  235. if ((ret=d2i_RSA_NET_2(a, enckey->enckey->digest,cb, sgckey)) == NULL) goto err;
  236. *pp = p;
  237. err:
  238. NETSCAPE_ENCRYPTED_PKEY_free(enckey);
  239. return ret;
  240. }
  241. static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
  242. int (*cb)(char *buf, int len, const char *prompt,
  243. int verify), int sgckey)
  244. {
  245. NETSCAPE_PKEY *pkey=NULL;
  246. RSA *ret=NULL;
  247. int i,j;
  248. unsigned char buf[256];
  249. const unsigned char *zz;
  250. unsigned char key[EVP_MAX_KEY_LENGTH];
  251. EVP_CIPHER_CTX ctx;
  252. EVP_CIPHER_CTX_init(&ctx);
  253. i=cb((char *)buf,256,"Enter Private Key password:",0);
  254. if (i != 0)
  255. {
  256. ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_BAD_PASSWORD_READ);
  257. goto err;
  258. }
  259. i = strlen((char *)buf);
  260. if(sgckey){
  261. if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL))
  262. goto err;
  263. memcpy(buf + 16, "SGCKEYSALT", 10);
  264. i = 26;
  265. }
  266. if (!EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL))
  267. goto err;
  268. OPENSSL_cleanse(buf,256);
  269. if (!EVP_DecryptInit_ex(&ctx,EVP_rc4(),NULL, key,NULL))
  270. goto err;
  271. if (!EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length))
  272. goto err;
  273. if (!EVP_DecryptFinal_ex(&ctx,&(os->data[i]),&j))
  274. goto err;
  275. os->length=i+j;
  276. zz=os->data;
  277. if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
  278. {
  279. ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
  280. goto err;
  281. }
  282. zz=pkey->private_key->data;
  283. if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
  284. {
  285. ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
  286. goto err;
  287. }
  288. err:
  289. EVP_CIPHER_CTX_cleanup(&ctx);
  290. NETSCAPE_PKEY_free(pkey);
  291. return(ret);
  292. }
  293. #endif /* OPENSSL_NO_RC4 */
  294. #else /* !OPENSSL_NO_RSA */
  295. # if PEDANTIC
  296. static void *dummy=&dummy;
  297. # endif
  298. #endif