Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

304 рядки
8.5 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 1999-2004.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <string.h>
  56. #include <openssl/asn1t.h>
  57. #include <openssl/cipher.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/pkcs8.h>
  61. #include <openssl/rand.h>
  62. #include <openssl/x509.h>
  63. #include "internal.h"
  64. /* PKCS#5 v2.0 password based encryption structures */
  65. ASN1_SEQUENCE(PBE2PARAM) = {
  66. ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
  67. ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
  68. } ASN1_SEQUENCE_END(PBE2PARAM)
  69. IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
  70. ASN1_SEQUENCE(PBKDF2PARAM) = {
  71. ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
  72. ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
  73. ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
  74. ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
  75. } ASN1_SEQUENCE_END(PBKDF2PARAM)
  76. IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM);
  77. static int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
  78. {
  79. ASN1_STRING *os;
  80. if ((os=M_ASN1_OCTET_STRING_new()) == NULL) return(0);
  81. if (!M_ASN1_OCTET_STRING_set(os,data,len))
  82. {
  83. M_ASN1_OCTET_STRING_free(os);
  84. return 0;
  85. }
  86. ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os);
  87. return(1);
  88. }
  89. static int param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  90. {
  91. unsigned iv_len;
  92. iv_len = EVP_CIPHER_CTX_iv_length(c);
  93. return ASN1_TYPE_set_octetstring(type, c->oiv, iv_len);
  94. }
  95. /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm:
  96. * yes I know this is horrible!
  97. *
  98. * Extended version to allow application supplied PRF NID and IV. */
  99. X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
  100. unsigned char *salt, int saltlen,
  101. unsigned char *aiv, int prf_nid)
  102. {
  103. X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
  104. int alg_nid, keylen;
  105. EVP_CIPHER_CTX ctx;
  106. unsigned char iv[EVP_MAX_IV_LENGTH];
  107. PBE2PARAM *pbe2 = NULL;
  108. const ASN1_OBJECT *obj;
  109. alg_nid = EVP_CIPHER_nid(cipher);
  110. if(alg_nid == NID_undef) {
  111. OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  112. goto err;
  113. }
  114. obj = OBJ_nid2obj(alg_nid);
  115. if(!(pbe2 = PBE2PARAM_new())) goto merr;
  116. /* Setup the AlgorithmIdentifier for the encryption scheme */
  117. scheme = pbe2->encryption;
  118. scheme->algorithm = (ASN1_OBJECT*) obj;
  119. if(!(scheme->parameter = ASN1_TYPE_new())) goto merr;
  120. /* Create random IV */
  121. if (EVP_CIPHER_iv_length(cipher))
  122. {
  123. if (aiv)
  124. memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
  125. else if (!RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)))
  126. goto err;
  127. }
  128. EVP_CIPHER_CTX_init(&ctx);
  129. /* Dummy cipherinit to just setup the IV, and PRF */
  130. if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0))
  131. goto err;
  132. if(param_to_asn1(&ctx, scheme->parameter) < 0) {
  133. OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
  134. EVP_CIPHER_CTX_cleanup(&ctx);
  135. goto err;
  136. }
  137. /* If prf NID unspecified see if cipher has a preference.
  138. * An error is OK here: just means use default PRF.
  139. */
  140. if ((prf_nid == -1) &&
  141. EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0)
  142. {
  143. ERR_clear_error();
  144. prf_nid = NID_hmacWithSHA1;
  145. }
  146. EVP_CIPHER_CTX_cleanup(&ctx);
  147. /* If its RC2 then we'd better setup the key length */
  148. if(alg_nid == NID_rc2_cbc)
  149. keylen = EVP_CIPHER_key_length(cipher);
  150. else
  151. keylen = -1;
  152. /* Setup keyfunc */
  153. X509_ALGOR_free(pbe2->keyfunc);
  154. pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen);
  155. if (!pbe2->keyfunc)
  156. goto merr;
  157. /* Now set up top level AlgorithmIdentifier */
  158. if(!(ret = X509_ALGOR_new())) goto merr;
  159. if(!(ret->parameter = ASN1_TYPE_new())) goto merr;
  160. ret->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_pbes2);
  161. /* Encode PBE2PARAM into parameter */
  162. if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM),
  163. &ret->parameter->value.sequence)) goto merr;
  164. ret->parameter->type = V_ASN1_SEQUENCE;
  165. PBE2PARAM_free(pbe2);
  166. pbe2 = NULL;
  167. return ret;
  168. merr:
  169. OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, ERR_R_MALLOC_FAILURE);
  170. err:
  171. PBE2PARAM_free(pbe2);
  172. /* Note 'scheme' is freed as part of pbe2 */
  173. X509_ALGOR_free(kalg);
  174. X509_ALGOR_free(ret);
  175. return NULL;
  176. }
  177. X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
  178. unsigned char *salt, int saltlen)
  179. {
  180. return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
  181. }
  182. X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
  183. int prf_nid, int keylen)
  184. {
  185. X509_ALGOR *keyfunc = NULL;
  186. PBKDF2PARAM *kdf = NULL;
  187. ASN1_OCTET_STRING *osalt = NULL;
  188. if(!(kdf = PBKDF2PARAM_new()))
  189. goto merr;
  190. if(!(osalt = M_ASN1_OCTET_STRING_new()))
  191. goto merr;
  192. kdf->salt->value.octet_string = osalt;
  193. kdf->salt->type = V_ASN1_OCTET_STRING;
  194. if (!saltlen)
  195. saltlen = PKCS5_SALT_LEN;
  196. if (!(osalt->data = OPENSSL_malloc (saltlen)))
  197. goto merr;
  198. osalt->length = saltlen;
  199. if (salt)
  200. memcpy (osalt->data, salt, saltlen);
  201. else if (!RAND_bytes(osalt->data, saltlen))
  202. goto merr;
  203. if(iter <= 0)
  204. iter = PKCS5_DEFAULT_ITERATIONS;
  205. if(!ASN1_INTEGER_set(kdf->iter, iter))
  206. goto merr;
  207. /* If have a key len set it up */
  208. if(keylen > 0)
  209. {
  210. if(!(kdf->keylength = M_ASN1_INTEGER_new()))
  211. goto merr;
  212. if(!ASN1_INTEGER_set (kdf->keylength, keylen))
  213. goto merr;
  214. }
  215. /* prf can stay NULL if we are using hmacWithSHA1 */
  216. if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1)
  217. {
  218. kdf->prf = X509_ALGOR_new();
  219. if (!kdf->prf)
  220. goto merr;
  221. X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid),
  222. V_ASN1_NULL, NULL);
  223. }
  224. /* Finally setup the keyfunc structure */
  225. keyfunc = X509_ALGOR_new();
  226. if (!keyfunc)
  227. goto merr;
  228. keyfunc->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_id_pbkdf2);
  229. /* Encode PBKDF2PARAM into parameter of pbe2 */
  230. if(!(keyfunc->parameter = ASN1_TYPE_new()))
  231. goto merr;
  232. if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
  233. &keyfunc->parameter->value.sequence))
  234. goto merr;
  235. keyfunc->parameter->type = V_ASN1_SEQUENCE;
  236. PBKDF2PARAM_free(kdf);
  237. return keyfunc;
  238. merr:
  239. OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbkdf2_set, ERR_R_MALLOC_FAILURE);
  240. PBKDF2PARAM_free(kdf);
  241. X509_ALGOR_free(keyfunc);
  242. return NULL;
  243. }