cms_enc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "cms_local.h"
  17. /* CMS EncryptedData Utilities */
  18. /* Return BIO based on EncryptedContentInfo and key */
  19. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
  20. {
  21. BIO *b;
  22. EVP_CIPHER_CTX *ctx;
  23. const EVP_CIPHER *ciph;
  24. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  25. unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
  26. unsigned char *tkey = NULL;
  27. size_t tkeylen = 0;
  28. int ok = 0;
  29. int enc, keep_key = 0;
  30. enc = ec->cipher ? 1 : 0;
  31. b = BIO_new(BIO_f_cipher());
  32. if (b == NULL) {
  33. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  34. return NULL;
  35. }
  36. BIO_get_cipher_ctx(b, &ctx);
  37. if (enc) {
  38. ciph = ec->cipher;
  39. /*
  40. * If not keeping key set cipher to NULL so subsequent calls decrypt.
  41. */
  42. if (ec->key)
  43. ec->cipher = NULL;
  44. } else {
  45. ciph = EVP_get_cipherbyobj(calg->algorithm);
  46. if (!ciph) {
  47. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
  48. goto err;
  49. }
  50. }
  51. if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {
  52. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  53. CMS_R_CIPHER_INITIALISATION_ERROR);
  54. goto err;
  55. }
  56. if (enc) {
  57. int ivlen;
  58. calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
  59. if (calg->algorithm == NULL) {
  60. CMSerr(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
  61. goto err;
  62. }
  63. /* Generate a random IV if we need one */
  64. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  65. if (ivlen > 0) {
  66. if (RAND_bytes(iv, ivlen) <= 0)
  67. goto err;
  68. piv = iv;
  69. }
  70. } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
  71. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  72. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  73. goto err;
  74. }
  75. tkeylen = EVP_CIPHER_CTX_key_length(ctx);
  76. /* Generate random session key */
  77. if (!enc || !ec->key) {
  78. tkey = OPENSSL_malloc(tkeylen);
  79. if (tkey == NULL) {
  80. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  81. goto err;
  82. }
  83. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  84. goto err;
  85. }
  86. if (!ec->key) {
  87. ec->key = tkey;
  88. ec->keylen = tkeylen;
  89. tkey = NULL;
  90. if (enc)
  91. keep_key = 1;
  92. else
  93. ERR_clear_error();
  94. }
  95. if (ec->keylen != tkeylen) {
  96. /* If necessary set key length */
  97. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  98. /*
  99. * Only reveal failure if debugging so we don't leak information
  100. * which may be useful in MMA.
  101. */
  102. if (enc || ec->debug) {
  103. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  104. CMS_R_INVALID_KEY_LENGTH);
  105. goto err;
  106. } else {
  107. /* Use random key */
  108. OPENSSL_clear_free(ec->key, ec->keylen);
  109. ec->key = tkey;
  110. ec->keylen = tkeylen;
  111. tkey = NULL;
  112. ERR_clear_error();
  113. }
  114. }
  115. }
  116. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  117. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  118. CMS_R_CIPHER_INITIALISATION_ERROR);
  119. goto err;
  120. }
  121. if (enc) {
  122. calg->parameter = ASN1_TYPE_new();
  123. if (calg->parameter == NULL) {
  124. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  125. goto err;
  126. }
  127. if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {
  128. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  129. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  130. goto err;
  131. }
  132. /* If parameter type not set omit parameter */
  133. if (calg->parameter->type == V_ASN1_UNDEF) {
  134. ASN1_TYPE_free(calg->parameter);
  135. calg->parameter = NULL;
  136. }
  137. }
  138. ok = 1;
  139. err:
  140. if (!keep_key || !ok) {
  141. OPENSSL_clear_free(ec->key, ec->keylen);
  142. ec->key = NULL;
  143. }
  144. OPENSSL_clear_free(tkey, tkeylen);
  145. if (ok)
  146. return b;
  147. BIO_free(b);
  148. return NULL;
  149. }
  150. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  151. const EVP_CIPHER *cipher,
  152. const unsigned char *key, size_t keylen)
  153. {
  154. ec->cipher = cipher;
  155. if (key) {
  156. if ((ec->key = OPENSSL_malloc(keylen)) == NULL) {
  157. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT, ERR_R_MALLOC_FAILURE);
  158. return 0;
  159. }
  160. memcpy(ec->key, key, keylen);
  161. }
  162. ec->keylen = keylen;
  163. if (cipher)
  164. ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
  165. return 1;
  166. }
  167. int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
  168. const unsigned char *key, size_t keylen)
  169. {
  170. CMS_EncryptedContentInfo *ec;
  171. if (!key || !keylen) {
  172. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
  173. return 0;
  174. }
  175. if (ciph) {
  176. cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
  177. if (!cms->d.encryptedData) {
  178. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE);
  179. return 0;
  180. }
  181. cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
  182. cms->d.encryptedData->version = 0;
  183. } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
  184. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA);
  185. return 0;
  186. }
  187. ec = cms->d.encryptedData->encryptedContentInfo;
  188. return cms_EncryptedContent_init(ec, ciph, key, keylen);
  189. }
  190. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
  191. {
  192. CMS_EncryptedData *enc = cms->d.encryptedData;
  193. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  194. enc->version = 2;
  195. return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
  196. }