cms_enc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 *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 = cms_ctx_get0_libctx(cms_ctx);
  37. const char *propq = 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_name(cipher), propq);
  58. if (fetched_ciph != NULL)
  59. cipher = fetched_ciph;
  60. }
  61. if (cipher == NULL) {
  62. (void)ERR_clear_last_mark();
  63. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
  64. goto err;
  65. }
  66. (void)ERR_pop_to_mark();
  67. if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc) <= 0) {
  68. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
  69. goto err;
  70. }
  71. if (enc) {
  72. calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
  73. /* Generate a random IV if we need one */
  74. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  75. if (ivlen > 0) {
  76. if (RAND_bytes_ex(libctx, iv, ivlen) <= 0)
  77. goto err;
  78. piv = iv;
  79. }
  80. } else {
  81. if (evp_cipher_asn1_to_param_ex(ctx, calg->parameter, &aparams) <= 0) {
  82. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  83. goto err;
  84. }
  85. if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  86. piv = aparams.iv;
  87. if (ec->taglen > 0
  88. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  89. ec->taglen, ec->tag) <= 0) {
  90. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR);
  91. goto err;
  92. }
  93. }
  94. }
  95. len = EVP_CIPHER_CTX_key_length(ctx);
  96. if (len <= 0)
  97. goto err;
  98. tkeylen = (size_t)len;
  99. /* Generate random session key */
  100. if (!enc || !ec->key) {
  101. tkey = OPENSSL_malloc(tkeylen);
  102. if (tkey == NULL) {
  103. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  104. goto err;
  105. }
  106. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  107. goto err;
  108. }
  109. if (!ec->key) {
  110. ec->key = tkey;
  111. ec->keylen = tkeylen;
  112. tkey = NULL;
  113. if (enc)
  114. keep_key = 1;
  115. else
  116. ERR_clear_error();
  117. }
  118. if (ec->keylen != tkeylen) {
  119. /* If necessary set key length */
  120. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  121. /*
  122. * Only reveal failure if debugging so we don't leak information
  123. * which may be useful in MMA.
  124. */
  125. if (enc || ec->debug) {
  126. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  127. goto err;
  128. } else {
  129. /* Use random key */
  130. OPENSSL_clear_free(ec->key, ec->keylen);
  131. ec->key = tkey;
  132. ec->keylen = tkeylen;
  133. tkey = NULL;
  134. ERR_clear_error();
  135. }
  136. }
  137. }
  138. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  139. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
  140. goto err;
  141. }
  142. if (enc) {
  143. calg->parameter = ASN1_TYPE_new();
  144. if (calg->parameter == NULL) {
  145. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  146. goto err;
  147. }
  148. if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  149. memcpy(aparams.iv, piv, ivlen);
  150. aparams.iv_len = ivlen;
  151. aparams.tag_len = EVP_CIPHER_CTX_tag_length(ctx);
  152. if (aparams.tag_len <= 0)
  153. goto err;
  154. }
  155. if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) {
  156. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  157. goto err;
  158. }
  159. /* If parameter type not set omit parameter */
  160. if (calg->parameter->type == V_ASN1_UNDEF) {
  161. ASN1_TYPE_free(calg->parameter);
  162. calg->parameter = NULL;
  163. }
  164. }
  165. ok = 1;
  166. err:
  167. EVP_CIPHER_free(fetched_ciph);
  168. if (!keep_key || !ok) {
  169. OPENSSL_clear_free(ec->key, ec->keylen);
  170. ec->key = NULL;
  171. }
  172. OPENSSL_clear_free(tkey, tkeylen);
  173. if (ok)
  174. return b;
  175. BIO_free(b);
  176. return NULL;
  177. }
  178. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  179. const EVP_CIPHER *cipher,
  180. const unsigned char *key, size_t keylen,
  181. const CMS_CTX *cms_ctx)
  182. {
  183. ec->cipher = cipher;
  184. if (key) {
  185. if ((ec->key = OPENSSL_malloc(keylen)) == NULL) {
  186. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  187. return 0;
  188. }
  189. memcpy(ec->key, key, keylen);
  190. }
  191. ec->keylen = keylen;
  192. if (cipher != NULL)
  193. ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
  194. return 1;
  195. }
  196. int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
  197. const unsigned char *key, size_t keylen)
  198. {
  199. CMS_EncryptedContentInfo *ec;
  200. if (!key || !keylen) {
  201. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
  202. return 0;
  203. }
  204. if (ciph) {
  205. cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
  206. if (!cms->d.encryptedData) {
  207. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  208. return 0;
  209. }
  210. cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
  211. cms->d.encryptedData->version = 0;
  212. } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
  213. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_ENCRYPTED_DATA);
  214. return 0;
  215. }
  216. ec = cms->d.encryptedData->encryptedContentInfo;
  217. return cms_EncryptedContent_init(ec, ciph, key, keylen, cms_get0_cmsctx(cms));
  218. }
  219. BIO *cms_EncryptedData_init_bio(const CMS_ContentInfo *cms)
  220. {
  221. CMS_EncryptedData *enc = cms->d.encryptedData;
  222. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  223. enc->version = 2;
  224. return cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
  225. cms_get0_cmsctx(cms));
  226. }