cms_enc.c 7.8 KB

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