cms_pwri.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* crypto/cms/cms_pwri.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2009 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. #include "cryptlib.h"
  55. #include <openssl/asn1t.h>
  56. #include <openssl/pem.h>
  57. #include <openssl/x509v3.h>
  58. #include <openssl/err.h>
  59. #include <openssl/cms.h>
  60. #include <openssl/rand.h>
  61. #include <openssl/aes.h>
  62. #include "cms_lcl.h"
  63. #include "asn1_locl.h"
  64. int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
  65. unsigned char *pass, ossl_ssize_t passlen)
  66. {
  67. CMS_PasswordRecipientInfo *pwri;
  68. if (ri->type != CMS_RECIPINFO_PASS) {
  69. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, CMS_R_NOT_PWRI);
  70. return 0;
  71. }
  72. pwri = ri->d.pwri;
  73. pwri->pass = pass;
  74. if (pass && passlen < 0)
  75. passlen = strlen((char *)pass);
  76. pwri->passlen = passlen;
  77. return 1;
  78. }
  79. CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
  80. int iter, int wrap_nid,
  81. int pbe_nid,
  82. unsigned char *pass,
  83. ossl_ssize_t passlen,
  84. const EVP_CIPHER *kekciph)
  85. {
  86. CMS_RecipientInfo *ri = NULL;
  87. CMS_EnvelopedData *env;
  88. CMS_PasswordRecipientInfo *pwri;
  89. EVP_CIPHER_CTX ctx;
  90. X509_ALGOR *encalg = NULL;
  91. unsigned char iv[EVP_MAX_IV_LENGTH];
  92. int ivlen;
  93. env = cms_get0_enveloped(cms);
  94. if (!env)
  95. return NULL;
  96. if (wrap_nid <= 0)
  97. wrap_nid = NID_id_alg_PWRI_KEK;
  98. if (pbe_nid <= 0)
  99. pbe_nid = NID_id_pbkdf2;
  100. /* Get from enveloped data */
  101. if (kekciph == NULL)
  102. kekciph = env->encryptedContentInfo->cipher;
  103. if (kekciph == NULL) {
  104. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER);
  105. return NULL;
  106. }
  107. if (wrap_nid != NID_id_alg_PWRI_KEK) {
  108. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
  109. CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
  110. return NULL;
  111. }
  112. /* Setup algorithm identifier for cipher */
  113. encalg = X509_ALGOR_new();
  114. EVP_CIPHER_CTX_init(&ctx);
  115. if (EVP_EncryptInit_ex(&ctx, kekciph, NULL, NULL, NULL) <= 0) {
  116. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
  117. goto err;
  118. }
  119. ivlen = EVP_CIPHER_CTX_iv_length(&ctx);
  120. if (ivlen > 0) {
  121. if (RAND_pseudo_bytes(iv, ivlen) <= 0)
  122. goto err;
  123. if (EVP_EncryptInit_ex(&ctx, NULL, NULL, NULL, iv) <= 0) {
  124. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
  125. goto err;
  126. }
  127. encalg->parameter = ASN1_TYPE_new();
  128. if (!encalg->parameter) {
  129. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
  130. goto err;
  131. }
  132. if (EVP_CIPHER_param_to_asn1(&ctx, encalg->parameter) <= 0) {
  133. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
  134. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  135. goto err;
  136. }
  137. }
  138. encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(&ctx));
  139. EVP_CIPHER_CTX_cleanup(&ctx);
  140. /* Initialize recipient info */
  141. ri = M_ASN1_new_of(CMS_RecipientInfo);
  142. if (!ri)
  143. goto merr;
  144. ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
  145. if (!ri->d.pwri)
  146. goto merr;
  147. ri->type = CMS_RECIPINFO_PASS;
  148. pwri = ri->d.pwri;
  149. /* Since this is overwritten, free up empty structure already there */
  150. X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
  151. pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
  152. if (!pwri->keyEncryptionAlgorithm)
  153. goto merr;
  154. pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
  155. pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
  156. if (!pwri->keyEncryptionAlgorithm->parameter)
  157. goto merr;
  158. if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
  159. &pwri->keyEncryptionAlgorithm->parameter->
  160. value.sequence))
  161. goto merr;
  162. pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
  163. X509_ALGOR_free(encalg);
  164. encalg = NULL;
  165. /* Setup PBE algorithm */
  166. pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
  167. if (!pwri->keyDerivationAlgorithm)
  168. goto err;
  169. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  170. pwri->version = 0;
  171. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  172. goto merr;
  173. return ri;
  174. merr:
  175. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
  176. err:
  177. EVP_CIPHER_CTX_cleanup(&ctx);
  178. if (ri)
  179. M_ASN1_free_of(ri, CMS_RecipientInfo);
  180. if (encalg)
  181. X509_ALGOR_free(encalg);
  182. return NULL;
  183. }
  184. /*
  185. * This is an implementation of the key wrapping mechanism in RFC3211, at
  186. * some point this should go into EVP.
  187. */
  188. static int kek_unwrap_key(unsigned char *out, size_t *outlen,
  189. const unsigned char *in, size_t inlen,
  190. EVP_CIPHER_CTX *ctx)
  191. {
  192. size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
  193. unsigned char *tmp;
  194. int outl, rv = 0;
  195. if (inlen < 2 * blocklen) {
  196. /* too small */
  197. return 0;
  198. }
  199. if (inlen % blocklen) {
  200. /* Invalid size */
  201. return 0;
  202. }
  203. tmp = OPENSSL_malloc(inlen);
  204. if (!tmp)
  205. return 0;
  206. /* setup IV by decrypting last two blocks */
  207. EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
  208. in + inlen - 2 * blocklen, blocklen * 2);
  209. /*
  210. * Do a decrypt of last decrypted block to set IV to correct value output
  211. * it to start of buffer so we don't corrupt decrypted block this works
  212. * because buffer is at least two block lengths long.
  213. */
  214. EVP_DecryptUpdate(ctx, tmp, &outl, tmp + inlen - blocklen, blocklen);
  215. /* Can now decrypt first n - 1 blocks */
  216. EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen);
  217. /* Reset IV to original value */
  218. EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
  219. /* Decrypt again */
  220. EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen);
  221. /* Check check bytes */
  222. if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
  223. /* Check byte failure */
  224. goto err;
  225. }
  226. if (inlen < (size_t)(tmp[0] - 4)) {
  227. /* Invalid length value */
  228. goto err;
  229. }
  230. *outlen = (size_t)tmp[0];
  231. memcpy(out, tmp + 4, *outlen);
  232. rv = 1;
  233. err:
  234. OPENSSL_cleanse(tmp, inlen);
  235. OPENSSL_free(tmp);
  236. return rv;
  237. }
  238. static int kek_wrap_key(unsigned char *out, size_t *outlen,
  239. const unsigned char *in, size_t inlen,
  240. EVP_CIPHER_CTX *ctx)
  241. {
  242. size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
  243. size_t olen;
  244. int dummy;
  245. /*
  246. * First decide length of output buffer: need header and round up to
  247. * multiple of block length.
  248. */
  249. olen = (inlen + 4 + blocklen - 1) / blocklen;
  250. olen *= blocklen;
  251. if (olen < 2 * blocklen) {
  252. /* Key too small */
  253. return 0;
  254. }
  255. if (inlen > 0xFF) {
  256. /* Key too large */
  257. return 0;
  258. }
  259. if (out) {
  260. /* Set header */
  261. out[0] = (unsigned char)inlen;
  262. out[1] = in[0] ^ 0xFF;
  263. out[2] = in[1] ^ 0xFF;
  264. out[3] = in[2] ^ 0xFF;
  265. memcpy(out + 4, in, inlen);
  266. /* Add random padding to end */
  267. if (olen > inlen + 4
  268. && RAND_pseudo_bytes(out + 4 + inlen, olen - 4 - inlen) < 0)
  269. return 0;
  270. /* Encrypt twice */
  271. EVP_EncryptUpdate(ctx, out, &dummy, out, olen);
  272. EVP_EncryptUpdate(ctx, out, &dummy, out, olen);
  273. }
  274. *outlen = olen;
  275. return 1;
  276. }
  277. /* Encrypt/Decrypt content key in PWRI recipient info */
  278. int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  279. int en_de)
  280. {
  281. CMS_EncryptedContentInfo *ec;
  282. CMS_PasswordRecipientInfo *pwri;
  283. const unsigned char *p = NULL;
  284. int plen;
  285. int r = 0;
  286. X509_ALGOR *algtmp, *kekalg = NULL;
  287. EVP_CIPHER_CTX kekctx;
  288. const EVP_CIPHER *kekcipher;
  289. unsigned char *key = NULL;
  290. size_t keylen;
  291. ec = cms->d.envelopedData->encryptedContentInfo;
  292. pwri = ri->d.pwri;
  293. EVP_CIPHER_CTX_init(&kekctx);
  294. if (!pwri->pass) {
  295. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);
  296. return 0;
  297. }
  298. algtmp = pwri->keyEncryptionAlgorithm;
  299. if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
  300. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  301. CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
  302. return 0;
  303. }
  304. if (algtmp->parameter->type == V_ASN1_SEQUENCE) {
  305. p = algtmp->parameter->value.sequence->data;
  306. plen = algtmp->parameter->value.sequence->length;
  307. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  308. }
  309. if (kekalg == NULL) {
  310. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  311. CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
  312. return 0;
  313. }
  314. kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
  315. if (!kekcipher) {
  316. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);
  317. goto err;
  318. }
  319. /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
  320. if (!EVP_CipherInit_ex(&kekctx, kekcipher, NULL, NULL, NULL, en_de))
  321. goto err;
  322. EVP_CIPHER_CTX_set_padding(&kekctx, 0);
  323. if (EVP_CIPHER_asn1_to_param(&kekctx, kekalg->parameter) < 0) {
  324. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  325. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  326. goto err;
  327. }
  328. algtmp = pwri->keyDerivationAlgorithm;
  329. /* Finish password based key derivation to setup key in "ctx" */
  330. if (EVP_PBE_CipherInit(algtmp->algorithm,
  331. (char *)pwri->pass, pwri->passlen,
  332. algtmp->parameter, &kekctx, en_de) < 0) {
  333. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);
  334. goto err;
  335. }
  336. /* Finally wrap/unwrap the key */
  337. if (en_de) {
  338. if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, &kekctx))
  339. goto err;
  340. key = OPENSSL_malloc(keylen);
  341. if (!key)
  342. goto err;
  343. if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, &kekctx))
  344. goto err;
  345. pwri->encryptedKey->data = key;
  346. pwri->encryptedKey->length = keylen;
  347. } else {
  348. key = OPENSSL_malloc(pwri->encryptedKey->length);
  349. if (!key) {
  350. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
  351. goto err;
  352. }
  353. if (!kek_unwrap_key(key, &keylen,
  354. pwri->encryptedKey->data,
  355. pwri->encryptedKey->length, &kekctx)) {
  356. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);
  357. goto err;
  358. }
  359. ec->key = key;
  360. ec->keylen = keylen;
  361. }
  362. r = 1;
  363. err:
  364. EVP_CIPHER_CTX_cleanup(&kekctx);
  365. if (!r && key)
  366. OPENSSL_free(key);
  367. X509_ALGOR_free(kekalg);
  368. return r;
  369. }