p5_pbev2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 <assert.h>
  56. #include <limits.h>
  57. #include <string.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/cipher.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/pkcs8.h>
  63. #include <openssl/rand.h>
  64. #include <openssl/x509.h>
  65. #include "internal.h"
  66. /* PKCS#5 v2.0 password based encryption structures */
  67. ASN1_SEQUENCE(PBE2PARAM) = {
  68. ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
  69. ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
  70. } ASN1_SEQUENCE_END(PBE2PARAM)
  71. IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
  72. ASN1_SEQUENCE(PBKDF2PARAM) = {
  73. ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
  74. ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
  75. ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
  76. ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
  77. } ASN1_SEQUENCE_END(PBKDF2PARAM)
  78. IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM);
  79. static int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
  80. {
  81. ASN1_STRING *os;
  82. if ((os=M_ASN1_OCTET_STRING_new()) == NULL) return(0);
  83. if (!M_ASN1_OCTET_STRING_set(os,data,len))
  84. {
  85. M_ASN1_OCTET_STRING_free(os);
  86. return 0;
  87. }
  88. ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os);
  89. return(1);
  90. }
  91. static int param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  92. {
  93. unsigned iv_len;
  94. iv_len = EVP_CIPHER_CTX_iv_length(c);
  95. return ASN1_TYPE_set_octetstring(type, c->oiv, iv_len);
  96. }
  97. /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm:
  98. * yes I know this is horrible!
  99. *
  100. * Extended version to allow application supplied PRF NID and IV. */
  101. X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
  102. unsigned char *salt, int saltlen,
  103. unsigned char *aiv, int prf_nid)
  104. {
  105. X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
  106. int alg_nid, keylen;
  107. EVP_CIPHER_CTX ctx;
  108. unsigned char iv[EVP_MAX_IV_LENGTH];
  109. PBE2PARAM *pbe2 = NULL;
  110. const ASN1_OBJECT *obj;
  111. alg_nid = EVP_CIPHER_nid(cipher);
  112. if(alg_nid == NID_undef) {
  113. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  114. goto err;
  115. }
  116. obj = OBJ_nid2obj(alg_nid);
  117. if(!(pbe2 = PBE2PARAM_new())) goto merr;
  118. /* Setup the AlgorithmIdentifier for the encryption scheme */
  119. scheme = pbe2->encryption;
  120. scheme->algorithm = (ASN1_OBJECT*) obj;
  121. if(!(scheme->parameter = ASN1_TYPE_new())) goto merr;
  122. /* Create random IV */
  123. if (EVP_CIPHER_iv_length(cipher))
  124. {
  125. if (aiv)
  126. memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
  127. else if (!RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)))
  128. goto err;
  129. }
  130. EVP_CIPHER_CTX_init(&ctx);
  131. /* Dummy cipherinit to just setup the IV, and PRF */
  132. if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0))
  133. goto err;
  134. if(param_to_asn1(&ctx, scheme->parameter) < 0) {
  135. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
  136. EVP_CIPHER_CTX_cleanup(&ctx);
  137. goto err;
  138. }
  139. /* If prf NID unspecified see if cipher has a preference.
  140. * An error is OK here: just means use default PRF.
  141. */
  142. if ((prf_nid == -1) &&
  143. EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0)
  144. {
  145. ERR_clear_error();
  146. prf_nid = NID_hmacWithSHA1;
  147. }
  148. EVP_CIPHER_CTX_cleanup(&ctx);
  149. /* If its RC2 then we'd better setup the key length */
  150. if(alg_nid == NID_rc2_cbc)
  151. keylen = EVP_CIPHER_key_length(cipher);
  152. else
  153. keylen = -1;
  154. /* Setup keyfunc */
  155. X509_ALGOR_free(pbe2->keyfunc);
  156. pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen);
  157. if (!pbe2->keyfunc)
  158. goto merr;
  159. /* Now set up top level AlgorithmIdentifier */
  160. if(!(ret = X509_ALGOR_new())) goto merr;
  161. if(!(ret->parameter = ASN1_TYPE_new())) goto merr;
  162. ret->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_pbes2);
  163. /* Encode PBE2PARAM into parameter */
  164. if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM),
  165. &ret->parameter->value.sequence)) goto merr;
  166. ret->parameter->type = V_ASN1_SEQUENCE;
  167. PBE2PARAM_free(pbe2);
  168. pbe2 = NULL;
  169. return ret;
  170. merr:
  171. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  172. err:
  173. PBE2PARAM_free(pbe2);
  174. /* Note 'scheme' is freed as part of pbe2 */
  175. X509_ALGOR_free(kalg);
  176. X509_ALGOR_free(ret);
  177. return NULL;
  178. }
  179. X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
  180. unsigned char *salt, int saltlen)
  181. {
  182. return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
  183. }
  184. X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
  185. int prf_nid, int keylen)
  186. {
  187. X509_ALGOR *keyfunc = NULL;
  188. PBKDF2PARAM *kdf = NULL;
  189. ASN1_OCTET_STRING *osalt = NULL;
  190. if(!(kdf = PBKDF2PARAM_new()))
  191. goto merr;
  192. if(!(osalt = M_ASN1_OCTET_STRING_new()))
  193. goto merr;
  194. kdf->salt->value.octet_string = osalt;
  195. kdf->salt->type = V_ASN1_OCTET_STRING;
  196. if (!saltlen)
  197. saltlen = PKCS5_SALT_LEN;
  198. if (!(osalt->data = OPENSSL_malloc (saltlen)))
  199. goto merr;
  200. osalt->length = saltlen;
  201. if (salt)
  202. memcpy (osalt->data, salt, saltlen);
  203. else if (!RAND_bytes(osalt->data, saltlen))
  204. goto merr;
  205. if(iter <= 0)
  206. iter = PKCS5_DEFAULT_ITERATIONS;
  207. if(!ASN1_INTEGER_set(kdf->iter, iter))
  208. goto merr;
  209. /* If have a key len set it up */
  210. if(keylen > 0)
  211. {
  212. if(!(kdf->keylength = M_ASN1_INTEGER_new()))
  213. goto merr;
  214. if(!ASN1_INTEGER_set (kdf->keylength, keylen))
  215. goto merr;
  216. }
  217. /* prf can stay NULL if we are using hmacWithSHA1 */
  218. if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1)
  219. {
  220. kdf->prf = X509_ALGOR_new();
  221. if (!kdf->prf)
  222. goto merr;
  223. X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid),
  224. V_ASN1_NULL, NULL);
  225. }
  226. /* Finally setup the keyfunc structure */
  227. keyfunc = X509_ALGOR_new();
  228. if (!keyfunc)
  229. goto merr;
  230. keyfunc->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_id_pbkdf2);
  231. /* Encode PBKDF2PARAM into parameter of pbe2 */
  232. if(!(keyfunc->parameter = ASN1_TYPE_new()))
  233. goto merr;
  234. if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
  235. &keyfunc->parameter->value.sequence))
  236. goto merr;
  237. keyfunc->parameter->type = V_ASN1_SEQUENCE;
  238. PBKDF2PARAM_free(kdf);
  239. return keyfunc;
  240. merr:
  241. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  242. PBKDF2PARAM_free(kdf);
  243. X509_ALGOR_free(keyfunc);
  244. return NULL;
  245. }
  246. static int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx,
  247. const uint8_t *pass_raw,
  248. size_t pass_raw_len, const ASN1_TYPE *param,
  249. const ASN1_TYPE *iv, int enc) {
  250. int rv = 0;
  251. PBKDF2PARAM *pbkdf2param = NULL;
  252. if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
  253. OPENSSL_PUT_ERROR(PKCS8, CIPHER_R_NO_CIPHER_SET);
  254. goto err;
  255. }
  256. /* Decode parameters. */
  257. if (param == NULL || param->type != V_ASN1_SEQUENCE) {
  258. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  259. goto err;
  260. }
  261. const uint8_t *pbuf = param->value.sequence->data;
  262. int plen = param->value.sequence->length;
  263. pbkdf2param = d2i_PBKDF2PARAM(NULL, &pbuf, plen);
  264. if (pbkdf2param == NULL || pbuf != param->value.sequence->data + plen) {
  265. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  266. goto err;
  267. }
  268. /* Now check the parameters. */
  269. uint8_t key[EVP_MAX_KEY_LENGTH];
  270. const size_t key_len = EVP_CIPHER_CTX_key_length(ctx);
  271. assert(key_len <= sizeof(key));
  272. if (pbkdf2param->keylength != NULL &&
  273. ASN1_INTEGER_get(pbkdf2param->keylength) != (int) key_len) {
  274. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEYLENGTH);
  275. goto err;
  276. }
  277. if (pbkdf2param->prf != NULL &&
  278. OBJ_obj2nid(pbkdf2param->prf->algorithm) != NID_hmacWithSHA1) {
  279. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF);
  280. goto err;
  281. }
  282. if (pbkdf2param->salt->type != V_ASN1_OCTET_STRING) {
  283. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_SALT_TYPE);
  284. goto err;
  285. }
  286. if (pbkdf2param->iter->type != V_ASN1_INTEGER) {
  287. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
  288. goto err;
  289. }
  290. long iterations = ASN1_INTEGER_get(pbkdf2param->iter);
  291. if (iterations <= 0 ||
  292. (sizeof(long) > sizeof(unsigned) && iterations > (long)UINT_MAX)) {
  293. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
  294. goto err;
  295. }
  296. if (iv->type != V_ASN1_OCTET_STRING || iv->value.octet_string == NULL) {
  297. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
  298. goto err;
  299. }
  300. const size_t iv_len = EVP_CIPHER_CTX_iv_length(ctx);
  301. if ((size_t) iv->value.octet_string->length != iv_len) {
  302. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
  303. goto err;
  304. }
  305. if (!PKCS5_PBKDF2_HMAC_SHA1((const char *) pass_raw, pass_raw_len,
  306. pbkdf2param->salt->value.octet_string->data,
  307. pbkdf2param->salt->value.octet_string->length,
  308. iterations, key_len, key)) {
  309. goto err;
  310. }
  311. rv = EVP_CipherInit_ex(ctx, NULL /* cipher */, NULL /* engine */, key,
  312. iv->value.octet_string->data, enc);
  313. err:
  314. PBKDF2PARAM_free(pbkdf2param);
  315. return rv;
  316. }
  317. int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
  318. size_t pass_raw_len, ASN1_TYPE *param,
  319. const EVP_CIPHER *unused, const EVP_MD *unused2,
  320. int enc) {
  321. PBE2PARAM *pbe2param = NULL;
  322. int rv = 0;
  323. if (param == NULL ||
  324. param->type != V_ASN1_SEQUENCE ||
  325. param->value.sequence == NULL) {
  326. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  327. goto err;
  328. }
  329. const uint8_t *pbuf = param->value.sequence->data;
  330. int plen = param->value.sequence->length;
  331. pbe2param = d2i_PBE2PARAM(NULL, &pbuf, plen);
  332. if (pbe2param == NULL || pbuf != param->value.sequence->data + plen) {
  333. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  334. goto err;
  335. }
  336. /* Check that the key derivation function is PBKDF2. */
  337. if (OBJ_obj2nid(pbe2param->keyfunc->algorithm) != NID_id_pbkdf2) {
  338. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
  339. goto err;
  340. }
  341. /* See if we recognise the encryption algorithm. */
  342. const EVP_CIPHER *cipher =
  343. EVP_get_cipherbynid(OBJ_obj2nid(pbe2param->encryption->algorithm));
  344. if (cipher == NULL) {
  345. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_CIPHER);
  346. goto err;
  347. }
  348. /* Fixup cipher based on AlgorithmIdentifier. */
  349. if (!EVP_CipherInit_ex(ctx, cipher, NULL /* engine */, NULL /* key */,
  350. NULL /* iv */, enc)) {
  351. goto err;
  352. }
  353. rv = PKCS5_v2_PBKDF2_keyivgen(ctx, pass_raw, pass_raw_len,
  354. pbe2param->keyfunc->parameter,
  355. pbe2param->encryption->parameter, enc);
  356. err:
  357. PBE2PARAM_free(pbe2param);
  358. return rv;
  359. }