cms_enc.c 8.0 KB

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