cms_enc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include "internal/cryptlib.h"
  54. #include <openssl/asn1t.h>
  55. #include <openssl/pem.h>
  56. #include <openssl/x509v3.h>
  57. #include <openssl/err.h>
  58. #include <openssl/cms.h>
  59. #include <openssl/rand.h>
  60. #include "cms_lcl.h"
  61. /* CMS EncryptedData Utilities */
  62. /* Return BIO based on EncryptedContentInfo and key */
  63. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
  64. {
  65. BIO *b;
  66. EVP_CIPHER_CTX *ctx;
  67. const EVP_CIPHER *ciph;
  68. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  69. unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
  70. unsigned char *tkey = NULL;
  71. size_t tkeylen = 0;
  72. int ok = 0;
  73. int enc, keep_key = 0;
  74. enc = ec->cipher ? 1 : 0;
  75. b = BIO_new(BIO_f_cipher());
  76. if (b == NULL) {
  77. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  78. return NULL;
  79. }
  80. BIO_get_cipher_ctx(b, &ctx);
  81. if (enc) {
  82. ciph = ec->cipher;
  83. /*
  84. * If not keeping key set cipher to NULL so subsequent calls decrypt.
  85. */
  86. if (ec->key)
  87. ec->cipher = NULL;
  88. } else {
  89. ciph = EVP_get_cipherbyobj(calg->algorithm);
  90. if (!ciph) {
  91. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
  92. goto err;
  93. }
  94. }
  95. if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {
  96. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  97. CMS_R_CIPHER_INITIALISATION_ERROR);
  98. goto err;
  99. }
  100. if (enc) {
  101. int ivlen;
  102. calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
  103. /* Generate a random IV if we need one */
  104. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  105. if (ivlen > 0) {
  106. if (RAND_bytes(iv, ivlen) <= 0)
  107. goto err;
  108. piv = iv;
  109. }
  110. } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
  111. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  112. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  113. goto err;
  114. }
  115. tkeylen = EVP_CIPHER_CTX_key_length(ctx);
  116. /* Generate random session key */
  117. if (!enc || !ec->key) {
  118. tkey = OPENSSL_malloc(tkeylen);
  119. if (tkey == NULL) {
  120. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  121. goto err;
  122. }
  123. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  124. goto err;
  125. }
  126. if (!ec->key) {
  127. ec->key = tkey;
  128. ec->keylen = tkeylen;
  129. tkey = NULL;
  130. if (enc)
  131. keep_key = 1;
  132. else
  133. ERR_clear_error();
  134. }
  135. if (ec->keylen != tkeylen) {
  136. /* If necessary set key length */
  137. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  138. /*
  139. * Only reveal failure if debugging so we don't leak information
  140. * which may be useful in MMA.
  141. */
  142. if (enc || ec->debug) {
  143. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  144. CMS_R_INVALID_KEY_LENGTH);
  145. goto err;
  146. } else {
  147. /* Use random key */
  148. OPENSSL_clear_free(ec->key, ec->keylen);
  149. ec->key = tkey;
  150. ec->keylen = tkeylen;
  151. tkey = NULL;
  152. ERR_clear_error();
  153. }
  154. }
  155. }
  156. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  157. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  158. CMS_R_CIPHER_INITIALISATION_ERROR);
  159. goto err;
  160. }
  161. if (piv) {
  162. calg->parameter = ASN1_TYPE_new();
  163. if (calg->parameter == NULL) {
  164. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  165. goto err;
  166. }
  167. if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {
  168. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  169. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  170. goto err;
  171. }
  172. }
  173. ok = 1;
  174. err:
  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 cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  186. const EVP_CIPHER *cipher,
  187. const unsigned char *key, size_t keylen)
  188. {
  189. ec->cipher = cipher;
  190. if (key) {
  191. ec->key = OPENSSL_malloc(keylen);
  192. if (ec->key == NULL)
  193. return 0;
  194. memcpy(ec->key, key, keylen);
  195. }
  196. ec->keylen = keylen;
  197. if (cipher)
  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. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, 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. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE);
  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. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA);
  219. return 0;
  220. }
  221. ec = cms->d.encryptedData->encryptedContentInfo;
  222. return cms_EncryptedContent_init(ec, ciph, key, keylen);
  223. }
  224. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
  225. {
  226. CMS_EncryptedData *enc = cms->d.encryptedData;
  227. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  228. enc->version = 2;
  229. return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
  230. }