cms_enc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "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_BIO_LIB);
  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. goto err;
  114. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  115. goto err;
  116. }
  117. if (!ec->key) {
  118. ec->key = tkey;
  119. ec->keylen = tkeylen;
  120. tkey = NULL;
  121. if (enc)
  122. keep_key = 1;
  123. else
  124. ERR_clear_error();
  125. }
  126. if (ec->keylen != tkeylen) {
  127. /* If necessary set key length */
  128. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  129. /*
  130. * Only reveal failure if debugging so we don't leak information
  131. * which may be useful in MMA.
  132. */
  133. if (enc || ec->debug) {
  134. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  135. goto err;
  136. } else {
  137. /* Use random key */
  138. OPENSSL_clear_free(ec->key, ec->keylen);
  139. ec->key = tkey;
  140. ec->keylen = tkeylen;
  141. tkey = NULL;
  142. ERR_clear_error();
  143. }
  144. }
  145. }
  146. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  147. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
  148. goto err;
  149. }
  150. if (enc) {
  151. calg->parameter = ASN1_TYPE_new();
  152. if (calg->parameter == NULL) {
  153. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  154. goto err;
  155. }
  156. if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  157. memcpy(aparams.iv, piv, ivlen);
  158. aparams.iv_len = ivlen;
  159. aparams.tag_len = EVP_CIPHER_CTX_get_tag_length(ctx);
  160. if (aparams.tag_len <= 0)
  161. goto err;
  162. }
  163. if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) {
  164. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  165. goto err;
  166. }
  167. /* If parameter type not set omit parameter */
  168. if (calg->parameter->type == V_ASN1_UNDEF) {
  169. ASN1_TYPE_free(calg->parameter);
  170. calg->parameter = NULL;
  171. }
  172. }
  173. ok = 1;
  174. err:
  175. EVP_CIPHER_free(fetched_ciph);
  176. if (!keep_key || !ok) {
  177. OPENSSL_clear_free(ec->key, ec->keylen);
  178. ec->key = NULL;
  179. }
  180. OPENSSL_clear_free(tkey, tkeylen);
  181. if (ok)
  182. return b;
  183. BIO_free(b);
  184. return NULL;
  185. }
  186. int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  187. const EVP_CIPHER *cipher,
  188. const unsigned char *key, size_t keylen,
  189. const CMS_CTX *cms_ctx)
  190. {
  191. ec->cipher = cipher;
  192. if (key) {
  193. if ((ec->key = OPENSSL_malloc(keylen)) == NULL)
  194. return 0;
  195. memcpy(ec->key, key, keylen);
  196. }
  197. ec->keylen = keylen;
  198. if (cipher != NULL)
  199. ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
  200. return 1;
  201. }
  202. int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
  203. const unsigned char *key, size_t keylen)
  204. {
  205. CMS_EncryptedContentInfo *ec;
  206. if (!key || !keylen) {
  207. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
  208. return 0;
  209. }
  210. if (ciph) {
  211. cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
  212. if (!cms->d.encryptedData) {
  213. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  214. return 0;
  215. }
  216. cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
  217. cms->d.encryptedData->version = 0;
  218. } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
  219. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_ENCRYPTED_DATA);
  220. return 0;
  221. }
  222. ec = cms->d.encryptedData->encryptedContentInfo;
  223. return ossl_cms_EncryptedContent_init(ec, ciph, key, keylen,
  224. ossl_cms_get0_cmsctx(cms));
  225. }
  226. BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms)
  227. {
  228. CMS_EncryptedData *enc = cms->d.encryptedData;
  229. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  230. enc->version = 2;
  231. return ossl_cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
  232. ossl_cms_get0_cmsctx(cms));
  233. }