p5_pbev2.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <openssl/pkcs8.h>
  56. #include <limits.h>
  57. #include <string.h>
  58. #include <openssl/bytestring.h>
  59. #include <openssl/cipher.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/obj.h>
  63. #include <openssl/rand.h>
  64. #include "internal.h"
  65. #include "../internal.h"
  66. static int pkcs5_pbe2_cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  67. unsigned iterations, const uint8_t *pass_raw,
  68. size_t pass_raw_len, const uint8_t *salt,
  69. size_t salt_len, const uint8_t *iv,
  70. size_t iv_len, int enc) {
  71. if (iv_len != EVP_CIPHER_iv_length(cipher)) {
  72. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
  73. return 0;
  74. }
  75. uint8_t key[EVP_MAX_KEY_LENGTH];
  76. int ret = PKCS5_PBKDF2_HMAC_SHA1((const char *)pass_raw, pass_raw_len, salt,
  77. salt_len, iterations,
  78. EVP_CIPHER_key_length(cipher), key) &&
  79. EVP_CipherInit_ex(ctx, cipher, NULL /* engine */, key, iv, enc);
  80. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  81. return ret;
  82. }
  83. int PKCS5_pbe2_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx,
  84. const EVP_CIPHER *cipher, unsigned iterations,
  85. const uint8_t *pass_raw, size_t pass_raw_len,
  86. const uint8_t *salt, size_t salt_len) {
  87. int cipher_nid = EVP_CIPHER_nid(cipher);
  88. if (cipher_nid == NID_undef) {
  89. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  90. return 0;
  91. }
  92. /* Generate a random IV. */
  93. uint8_t iv[EVP_MAX_IV_LENGTH];
  94. if (!RAND_bytes(iv, EVP_CIPHER_iv_length(cipher))) {
  95. return 0;
  96. }
  97. /* See RFC 2898, appendix A. */
  98. CBB algorithm, param, kdf, kdf_param, salt_cbb, cipher_cbb, iv_cbb;
  99. if (!CBB_add_asn1(out, &algorithm, CBS_ASN1_SEQUENCE) ||
  100. !OBJ_nid2cbb(&algorithm, NID_pbes2) ||
  101. !CBB_add_asn1(&algorithm, &param, CBS_ASN1_SEQUENCE) ||
  102. !CBB_add_asn1(&param, &kdf, CBS_ASN1_SEQUENCE) ||
  103. !OBJ_nid2cbb(&kdf, NID_id_pbkdf2) ||
  104. !CBB_add_asn1(&kdf, &kdf_param, CBS_ASN1_SEQUENCE) ||
  105. !CBB_add_asn1(&kdf_param, &salt_cbb, CBS_ASN1_OCTETSTRING) ||
  106. !CBB_add_bytes(&salt_cbb, salt, salt_len) ||
  107. !CBB_add_asn1_uint64(&kdf_param, iterations) ||
  108. /* Specify a key length for RC2. */
  109. (cipher_nid == NID_rc2_cbc &&
  110. !CBB_add_asn1_uint64(&kdf_param, EVP_CIPHER_key_length(cipher))) ||
  111. /* Omit the PRF. We use the default hmacWithSHA1. */
  112. !CBB_add_asn1(&param, &cipher_cbb, CBS_ASN1_SEQUENCE) ||
  113. !OBJ_nid2cbb(&cipher_cbb, cipher_nid) ||
  114. /* RFC 2898 says RC2-CBC and RC5-CBC-Pad use a SEQUENCE with version and
  115. * IV, but OpenSSL always uses an OCTET STRING IV, so we do the same. */
  116. !CBB_add_asn1(&cipher_cbb, &iv_cbb, CBS_ASN1_OCTETSTRING) ||
  117. !CBB_add_bytes(&iv_cbb, iv, EVP_CIPHER_iv_length(cipher)) ||
  118. !CBB_flush(out)) {
  119. return 0;
  120. }
  121. return pkcs5_pbe2_cipher_init(ctx, cipher, iterations, pass_raw, pass_raw_len,
  122. salt, salt_len, iv,
  123. EVP_CIPHER_iv_length(cipher), 1 /* encrypt */);
  124. }
  125. int PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx,
  126. const uint8_t *pass_raw, size_t pass_raw_len,
  127. CBS *param) {
  128. CBS pbe_param, kdf, kdf_obj, enc_scheme, enc_obj;
  129. if (!CBS_get_asn1(param, &pbe_param, CBS_ASN1_SEQUENCE) ||
  130. CBS_len(param) != 0 ||
  131. !CBS_get_asn1(&pbe_param, &kdf, CBS_ASN1_SEQUENCE) ||
  132. !CBS_get_asn1(&pbe_param, &enc_scheme, CBS_ASN1_SEQUENCE) ||
  133. CBS_len(&pbe_param) != 0 ||
  134. !CBS_get_asn1(&kdf, &kdf_obj, CBS_ASN1_OBJECT) ||
  135. !CBS_get_asn1(&enc_scheme, &enc_obj, CBS_ASN1_OBJECT)) {
  136. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  137. return 0;
  138. }
  139. /* Check that the key derivation function is PBKDF2. */
  140. if (OBJ_cbs2nid(&kdf_obj) != NID_id_pbkdf2) {
  141. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
  142. return 0;
  143. }
  144. /* See if we recognise the encryption algorithm. */
  145. const EVP_CIPHER *cipher = EVP_get_cipherbynid(OBJ_cbs2nid(&enc_obj));
  146. if (cipher == NULL) {
  147. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_CIPHER);
  148. return 0;
  149. }
  150. /* Parse the KDF parameters. */
  151. CBS pbkdf2_params, salt;
  152. uint64_t iterations;
  153. if (!CBS_get_asn1(&kdf, &pbkdf2_params, CBS_ASN1_SEQUENCE) ||
  154. CBS_len(&kdf) != 0 ||
  155. !CBS_get_asn1(&pbkdf2_params, &salt, CBS_ASN1_OCTETSTRING) ||
  156. !CBS_get_asn1_uint64(&pbkdf2_params, &iterations)) {
  157. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  158. return 0;
  159. }
  160. if (iterations == 0 || iterations > UINT_MAX) {
  161. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
  162. return 0;
  163. }
  164. /* The optional keyLength parameter, if present, must match the key length of
  165. * the cipher. */
  166. if (CBS_peek_asn1_tag(&pbkdf2_params, CBS_ASN1_INTEGER)) {
  167. uint64_t key_len;
  168. if (!CBS_get_asn1_uint64(&pbkdf2_params, &key_len)) {
  169. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  170. return 0;
  171. }
  172. if (key_len != EVP_CIPHER_key_length(cipher)) {
  173. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEYLENGTH);
  174. return 0;
  175. }
  176. }
  177. if (CBS_len(&pbkdf2_params) != 0) {
  178. CBS prf;
  179. if (!CBS_get_asn1(&pbkdf2_params, &prf, CBS_ASN1_OBJECT) ||
  180. CBS_len(&pbkdf2_params) != 0) {
  181. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  182. return 0;
  183. }
  184. /* We only support hmacWithSHA1. It is the DEFAULT, so DER requires it be
  185. * omitted, but we match OpenSSL in tolerating it being present. */
  186. if (OBJ_cbs2nid(&prf) != NID_hmacWithSHA1) {
  187. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF);
  188. return 0;
  189. }
  190. }
  191. /* Parse the encryption scheme parameters. Note OpenSSL does not match the
  192. * specification. Per RFC 2898, this should depend on the encryption scheme.
  193. * In particular, RC2-CBC and RC5-CBC-Pad use a SEQUENCE with version and IV.
  194. * We align with OpenSSL. */
  195. CBS iv;
  196. if (!CBS_get_asn1(&enc_scheme, &iv, CBS_ASN1_OCTETSTRING) ||
  197. CBS_len(&enc_scheme) != 0) {
  198. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF);
  199. return 0;
  200. }
  201. return pkcs5_pbe2_cipher_init(ctx, cipher, (unsigned)iterations, pass_raw,
  202. pass_raw_len, CBS_data(&salt), CBS_len(&salt),
  203. CBS_data(&iv), CBS_len(&iv), 0 /* decrypt */);
  204. }