cms_enc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include <openssl/asn1t.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/err.h>
  14. #include <openssl/cms.h>
  15. #include <openssl/rand.h>
  16. #include "crypto/evp.h"
  17. #include "cms_local.h"
  18. /* CMS EncryptedData Utilities */
  19. /* Return BIO based on EncryptedContentInfo and key */
  20. BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
  21. const CMS_CTX *cms_ctx)
  22. {
  23. BIO *b;
  24. EVP_CIPHER_CTX *ctx;
  25. EVP_CIPHER *fetched_ciph = NULL;
  26. const EVP_CIPHER *cipher = NULL;
  27. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  28. evp_cipher_aead_asn1_params aparams;
  29. unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
  30. unsigned char *tkey = NULL;
  31. int len;
  32. int ivlen = 0;
  33. size_t tkeylen = 0;
  34. int ok = 0;
  35. int enc, keep_key = 0;
  36. OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(cms_ctx);
  37. const char *propq = ossl_cms_ctx_get0_propq(cms_ctx);
  38. enc = ec->cipher ? 1 : 0;
  39. b = BIO_new(BIO_f_cipher());
  40. if (b == NULL) {
  41. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  42. return NULL;
  43. }
  44. BIO_get_cipher_ctx(b, &ctx);
  45. (void)ERR_set_mark();
  46. if (enc) {
  47. cipher = ec->cipher;
  48. /*
  49. * If not keeping key set cipher to NULL so subsequent calls decrypt.
  50. */
  51. if (ec->key != NULL)
  52. ec->cipher = NULL;
  53. } else {
  54. cipher = EVP_get_cipherbyobj(calg->algorithm);
  55. }
  56. if (cipher != NULL) {
  57. fetched_ciph = EVP_CIPHER_fetch(libctx, EVP_CIPHER_get0_name(cipher),
  58. propq);
  59. if (fetched_ciph != NULL)
  60. cipher = fetched_ciph;
  61. }
  62. if (cipher == NULL) {
  63. (void)ERR_clear_last_mark();
  64. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
  65. goto err;
  66. }
  67. (void)ERR_pop_to_mark();
  68. if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc) <= 0) {
  69. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
  70. goto err;
  71. }
  72. if (enc) {
  73. calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx));
  74. /* Generate a random IV if we need one */
  75. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  76. if (ivlen > 0) {
  77. if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
  78. goto err;
  79. piv = iv;
  80. }
  81. } else {
  82. if (evp_cipher_asn1_to_param_ex(ctx, calg->parameter, &aparams) <= 0) {
  83. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  84. goto err;
  85. }
  86. if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  87. piv = aparams.iv;
  88. if (ec->taglen > 0
  89. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  90. ec->taglen, ec->tag) <= 0) {
  91. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR);
  92. goto err;
  93. }
  94. }
  95. }
  96. len = EVP_CIPHER_CTX_get_key_length(ctx);
  97. if (len <= 0)
  98. goto err;
  99. tkeylen = (size_t)len;
  100. /* Generate random session key */
  101. if (!enc || !ec->key) {
  102. tkey = OPENSSL_malloc(tkeylen);
  103. if (tkey == NULL) {
  104. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  105. goto err;
  106. }
  107. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  108. goto err;
  109. }
  110. if (!ec->key) {
  111. ec->key = tkey;
  112. ec->keylen = tkeylen;
  113. tkey = NULL;
  114. if (enc)
  115. keep_key = 1;
  116. else
  117. ERR_clear_error();
  118. }
  119. if (ec->keylen != tkeylen) {
  120. /* If necessary set key length */
  121. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  122. /*
  123. * Only reveal failure if debugging so we don't leak information
  124. * which may be useful in MMA.
  125. */
  126. if (enc || ec->debug) {
  127. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  128. goto err;
  129. } else {
  130. /* Use random key */
  131. OPENSSL_clear_free(ec->key, ec->keylen);
  132. ec->key = tkey;
  133. ec->keylen = tkeylen;
  134. tkey = NULL;
  135. ERR_clear_error();
  136. }
  137. }
  138. }
  139. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  140. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
  141. goto err;
  142. }
  143. if (enc) {
  144. calg->parameter = ASN1_TYPE_new();
  145. if (calg->parameter == NULL) {
  146. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  147. goto err;
  148. }
  149. if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  150. memcpy(aparams.iv, piv, ivlen);
  151. aparams.iv_len = ivlen;
  152. aparams.tag_len = EVP_CIPHER_CTX_get_tag_length(ctx);
  153. if (aparams.tag_len <= 0)
  154. goto err;
  155. }
  156. if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) {
  157. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  158. goto err;
  159. }
  160. /* If parameter type not set omit parameter */
  161. if (calg->parameter->type == V_ASN1_UNDEF) {
  162. ASN1_TYPE_free(calg->parameter);
  163. calg->parameter = NULL;
  164. }
  165. }
  166. ok = 1;
  167. err:
  168. EVP_CIPHER_free(fetched_ciph);
  169. if (!keep_key || !ok) {
  170. OPENSSL_clear_free(ec->key, ec->keylen);
  171. ec->key = NULL;
  172. }
  173. OPENSSL_clear_free(tkey, tkeylen);
  174. if (ok)
  175. return b;
  176. BIO_free(b);
  177. return NULL;
  178. }
  179. int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  180. const EVP_CIPHER *cipher,
  181. const unsigned char *key, size_t keylen,
  182. const CMS_CTX *cms_ctx)
  183. {
  184. ec->cipher = cipher;
  185. if (key) {
  186. if ((ec->key = OPENSSL_malloc(keylen)) == NULL) {
  187. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  188. return 0;
  189. }
  190. memcpy(ec->key, key, keylen);
  191. }
  192. ec->keylen = keylen;
  193. if (cipher != NULL)
  194. ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
  195. return 1;
  196. }
  197. int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
  198. const unsigned char *key, size_t keylen)
  199. {
  200. CMS_EncryptedContentInfo *ec;
  201. if (!key || !keylen) {
  202. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
  203. return 0;
  204. }
  205. if (ciph) {
  206. cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
  207. if (!cms->d.encryptedData) {
  208. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  209. return 0;
  210. }
  211. cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
  212. cms->d.encryptedData->version = 0;
  213. } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
  214. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_ENCRYPTED_DATA);
  215. return 0;
  216. }
  217. ec = cms->d.encryptedData->encryptedContentInfo;
  218. return ossl_cms_EncryptedContent_init(ec, ciph, key, keylen,
  219. ossl_cms_get0_cmsctx(cms));
  220. }
  221. BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms)
  222. {
  223. CMS_EncryptedData *enc = cms->d.encryptedData;
  224. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  225. enc->version = 2;
  226. return ossl_cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
  227. ossl_cms_get0_cmsctx(cms));
  228. }