2
0

cms_enc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_BIO_LIB);
  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. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  78. goto err;
  79. }
  80. if (ivlen > 0) {
  81. if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
  82. goto err;
  83. piv = iv;
  84. }
  85. } else {
  86. if (evp_cipher_asn1_to_param_ex(ctx, calg->parameter, &aparams) <= 0) {
  87. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  88. goto err;
  89. }
  90. if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  91. piv = aparams.iv;
  92. if (ec->taglen > 0
  93. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  94. ec->taglen, ec->tag) <= 0) {
  95. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR);
  96. goto err;
  97. }
  98. }
  99. }
  100. len = EVP_CIPHER_CTX_get_key_length(ctx);
  101. if (len <= 0)
  102. goto err;
  103. tkeylen = (size_t)len;
  104. /* Generate random session key */
  105. if (!enc || !ec->key) {
  106. tkey = OPENSSL_malloc(tkeylen);
  107. if (tkey == NULL)
  108. goto err;
  109. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  110. goto err;
  111. }
  112. if (!ec->key) {
  113. ec->key = tkey;
  114. ec->keylen = tkeylen;
  115. tkey = NULL;
  116. if (enc)
  117. keep_key = 1;
  118. else
  119. ERR_clear_error();
  120. }
  121. if (ec->keylen != tkeylen) {
  122. /* If necessary set key length */
  123. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  124. /*
  125. * Only reveal failure if debugging so we don't leak information
  126. * which may be useful in MMA.
  127. */
  128. if (enc || ec->debug) {
  129. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  130. goto err;
  131. } else {
  132. /* Use random key */
  133. OPENSSL_clear_free(ec->key, ec->keylen);
  134. ec->key = tkey;
  135. ec->keylen = tkeylen;
  136. tkey = NULL;
  137. ERR_clear_error();
  138. }
  139. }
  140. }
  141. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  142. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
  143. goto err;
  144. }
  145. if (enc) {
  146. calg->parameter = ASN1_TYPE_new();
  147. if (calg->parameter == NULL) {
  148. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  149. goto err;
  150. }
  151. if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  152. memcpy(aparams.iv, piv, ivlen);
  153. aparams.iv_len = ivlen;
  154. aparams.tag_len = EVP_CIPHER_CTX_get_tag_length(ctx);
  155. if (aparams.tag_len <= 0)
  156. goto err;
  157. }
  158. if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) {
  159. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  160. goto err;
  161. }
  162. /* If parameter type not set omit parameter */
  163. if (calg->parameter->type == V_ASN1_UNDEF) {
  164. ASN1_TYPE_free(calg->parameter);
  165. calg->parameter = NULL;
  166. }
  167. }
  168. ok = 1;
  169. err:
  170. EVP_CIPHER_free(fetched_ciph);
  171. if (!keep_key || !ok) {
  172. OPENSSL_clear_free(ec->key, ec->keylen);
  173. ec->key = NULL;
  174. }
  175. OPENSSL_clear_free(tkey, tkeylen);
  176. if (ok)
  177. return b;
  178. BIO_free(b);
  179. return NULL;
  180. }
  181. int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  182. const EVP_CIPHER *cipher,
  183. const unsigned char *key, size_t keylen,
  184. const CMS_CTX *cms_ctx)
  185. {
  186. ec->cipher = cipher;
  187. if (key) {
  188. if ((ec->key = OPENSSL_malloc(keylen)) == NULL)
  189. return 0;
  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_ASN1_LIB);
  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. }