cms_pwri.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright 2009-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 <openssl/aes.h>
  17. #include "internal/sizes.h"
  18. #include "crypto/asn1.h"
  19. #include "cms_local.h"
  20. int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
  21. unsigned char *pass, ossl_ssize_t passlen)
  22. {
  23. CMS_PasswordRecipientInfo *pwri;
  24. if (ri->type != CMS_RECIPINFO_PASS) {
  25. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_PWRI);
  26. return 0;
  27. }
  28. pwri = ri->d.pwri;
  29. pwri->pass = pass;
  30. if (pass && passlen < 0)
  31. passlen = strlen((char *)pass);
  32. pwri->passlen = passlen;
  33. return 1;
  34. }
  35. CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
  36. int iter, int wrap_nid,
  37. int pbe_nid,
  38. unsigned char *pass,
  39. ossl_ssize_t passlen,
  40. const EVP_CIPHER *kekciph)
  41. {
  42. STACK_OF(CMS_RecipientInfo) *ris;
  43. CMS_RecipientInfo *ri = NULL;
  44. CMS_EncryptedContentInfo *ec;
  45. CMS_PasswordRecipientInfo *pwri;
  46. EVP_CIPHER_CTX *ctx = NULL;
  47. X509_ALGOR *encalg = NULL;
  48. unsigned char iv[EVP_MAX_IV_LENGTH];
  49. int ivlen;
  50. const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
  51. ec = ossl_cms_get0_env_enc_content(cms);
  52. if (ec == NULL)
  53. return NULL;
  54. ris = CMS_get0_RecipientInfos(cms);
  55. if (ris == NULL)
  56. return NULL;
  57. if (wrap_nid <= 0)
  58. wrap_nid = NID_id_alg_PWRI_KEK;
  59. if (pbe_nid <= 0)
  60. pbe_nid = NID_id_pbkdf2;
  61. /* Get from enveloped data */
  62. if (kekciph == NULL)
  63. kekciph = ec->cipher;
  64. if (kekciph == NULL) {
  65. ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
  66. return NULL;
  67. }
  68. if (wrap_nid != NID_id_alg_PWRI_KEK) {
  69. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
  70. return NULL;
  71. }
  72. /* Setup algorithm identifier for cipher */
  73. encalg = X509_ALGOR_new();
  74. if (encalg == NULL) {
  75. goto merr;
  76. }
  77. ctx = EVP_CIPHER_CTX_new();
  78. if (EVP_EncryptInit_ex(ctx, kekciph, NULL, NULL, NULL) <= 0) {
  79. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  80. goto err;
  81. }
  82. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  83. if (ivlen > 0) {
  84. if (RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx), iv, ivlen, 0) <= 0)
  85. goto err;
  86. if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) {
  87. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  88. goto err;
  89. }
  90. encalg->parameter = ASN1_TYPE_new();
  91. if (!encalg->parameter) {
  92. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  93. goto err;
  94. }
  95. if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) {
  96. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  97. goto err;
  98. }
  99. }
  100. encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx));
  101. EVP_CIPHER_CTX_free(ctx);
  102. ctx = NULL;
  103. /* Initialize recipient info */
  104. ri = M_ASN1_new_of(CMS_RecipientInfo);
  105. if (ri == NULL)
  106. goto merr;
  107. ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
  108. if (ri->d.pwri == NULL)
  109. goto merr;
  110. ri->type = CMS_RECIPINFO_PASS;
  111. pwri = ri->d.pwri;
  112. pwri->cms_ctx = cms_ctx;
  113. /* Since this is overwritten, free up empty structure already there */
  114. X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
  115. pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
  116. if (pwri->keyEncryptionAlgorithm == NULL)
  117. goto merr;
  118. pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
  119. pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
  120. if (pwri->keyEncryptionAlgorithm->parameter == NULL)
  121. goto merr;
  122. if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
  123. &pwri->keyEncryptionAlgorithm->parameter->
  124. value.sequence))
  125. goto merr;
  126. pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
  127. X509_ALGOR_free(encalg);
  128. encalg = NULL;
  129. /* Setup PBE algorithm */
  130. pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
  131. if (pwri->keyDerivationAlgorithm == NULL)
  132. goto err;
  133. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  134. pwri->version = 0;
  135. if (!sk_CMS_RecipientInfo_push(ris, ri))
  136. goto merr;
  137. return ri;
  138. merr:
  139. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  140. err:
  141. EVP_CIPHER_CTX_free(ctx);
  142. if (ri)
  143. M_ASN1_free_of(ri, CMS_RecipientInfo);
  144. X509_ALGOR_free(encalg);
  145. return NULL;
  146. }
  147. /*
  148. * This is an implementation of the key wrapping mechanism in RFC3211, at
  149. * some point this should go into EVP.
  150. */
  151. static int kek_unwrap_key(unsigned char *out, size_t *outlen,
  152. const unsigned char *in, size_t inlen,
  153. EVP_CIPHER_CTX *ctx)
  154. {
  155. size_t blocklen = EVP_CIPHER_CTX_get_block_size(ctx);
  156. unsigned char *tmp;
  157. int outl, rv = 0;
  158. if (inlen < 2 * blocklen) {
  159. /* too small */
  160. return 0;
  161. }
  162. if (inlen % blocklen) {
  163. /* Invalid size */
  164. return 0;
  165. }
  166. if ((tmp = OPENSSL_malloc(inlen)) == NULL) {
  167. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  168. return 0;
  169. }
  170. /* setup IV by decrypting last two blocks */
  171. if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
  172. in + inlen - 2 * blocklen, blocklen * 2)
  173. /*
  174. * Do a decrypt of last decrypted block to set IV to correct value
  175. * output it to start of buffer so we don't corrupt decrypted block
  176. * this works because buffer is at least two block lengths long.
  177. */
  178. || !EVP_DecryptUpdate(ctx, tmp, &outl,
  179. tmp + inlen - blocklen, blocklen)
  180. /* Can now decrypt first n - 1 blocks */
  181. || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)
  182. /* Reset IV to original value */
  183. || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
  184. /* Decrypt again */
  185. || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
  186. goto err;
  187. /* Check check bytes */
  188. if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
  189. /* Check byte failure */
  190. goto err;
  191. }
  192. if (inlen < (size_t)(tmp[0] - 4)) {
  193. /* Invalid length value */
  194. goto err;
  195. }
  196. *outlen = (size_t)tmp[0];
  197. memcpy(out, tmp + 4, *outlen);
  198. rv = 1;
  199. err:
  200. OPENSSL_clear_free(tmp, inlen);
  201. return rv;
  202. }
  203. static int kek_wrap_key(unsigned char *out, size_t *outlen,
  204. const unsigned char *in, size_t inlen,
  205. EVP_CIPHER_CTX *ctx, const CMS_CTX *cms_ctx)
  206. {
  207. size_t blocklen = EVP_CIPHER_CTX_get_block_size(ctx);
  208. size_t olen;
  209. int dummy;
  210. /*
  211. * First decide length of output buffer: need header and round up to
  212. * multiple of block length.
  213. */
  214. olen = (inlen + 4 + blocklen - 1) / blocklen;
  215. olen *= blocklen;
  216. if (olen < 2 * blocklen) {
  217. /* Key too small */
  218. return 0;
  219. }
  220. if (inlen > 0xFF) {
  221. /* Key too large */
  222. return 0;
  223. }
  224. if (out) {
  225. /* Set header */
  226. out[0] = (unsigned char)inlen;
  227. out[1] = in[0] ^ 0xFF;
  228. out[2] = in[1] ^ 0xFF;
  229. out[3] = in[2] ^ 0xFF;
  230. memcpy(out + 4, in, inlen);
  231. /* Add random padding to end */
  232. if (olen > inlen + 4
  233. && RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx), out + 4 + inlen,
  234. olen - 4 - inlen, 0) <= 0)
  235. return 0;
  236. /* Encrypt twice */
  237. if (!EVP_EncryptUpdate(ctx, out, &dummy, out, olen)
  238. || !EVP_EncryptUpdate(ctx, out, &dummy, out, olen))
  239. return 0;
  240. }
  241. *outlen = olen;
  242. return 1;
  243. }
  244. /* Encrypt/Decrypt content key in PWRI recipient info */
  245. int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
  246. CMS_RecipientInfo *ri, int en_de)
  247. {
  248. CMS_EncryptedContentInfo *ec;
  249. CMS_PasswordRecipientInfo *pwri;
  250. int r = 0;
  251. X509_ALGOR *algtmp, *kekalg = NULL;
  252. EVP_CIPHER_CTX *kekctx = NULL;
  253. char name[OSSL_MAX_NAME_SIZE];
  254. EVP_CIPHER *kekcipher;
  255. unsigned char *key = NULL;
  256. size_t keylen;
  257. const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
  258. ec = ossl_cms_get0_env_enc_content(cms);
  259. pwri = ri->d.pwri;
  260. if (pwri->pass == NULL) {
  261. ERR_raise(ERR_LIB_CMS, CMS_R_NO_PASSWORD);
  262. return 0;
  263. }
  264. algtmp = pwri->keyEncryptionAlgorithm;
  265. if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
  266. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
  267. return 0;
  268. }
  269. kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
  270. algtmp->parameter);
  271. if (kekalg == NULL) {
  272. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
  273. return 0;
  274. }
  275. OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0);
  276. kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx), name,
  277. ossl_cms_ctx_get0_propq(cms_ctx));
  278. if (kekcipher == NULL) {
  279. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
  280. goto err;
  281. }
  282. kekctx = EVP_CIPHER_CTX_new();
  283. if (kekctx == NULL) {
  284. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  285. goto err;
  286. }
  287. /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
  288. if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))
  289. goto err;
  290. EVP_CIPHER_CTX_set_padding(kekctx, 0);
  291. if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) {
  292. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  293. goto err;
  294. }
  295. algtmp = pwri->keyDerivationAlgorithm;
  296. /* Finish password based key derivation to setup key in "ctx" */
  297. if (EVP_PBE_CipherInit(algtmp->algorithm,
  298. (char *)pwri->pass, pwri->passlen,
  299. algtmp->parameter, kekctx, en_de) < 0) {
  300. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  301. goto err;
  302. }
  303. /* Finally wrap/unwrap the key */
  304. if (en_de) {
  305. if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx, cms_ctx))
  306. goto err;
  307. key = OPENSSL_malloc(keylen);
  308. if (key == NULL)
  309. goto err;
  310. if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx, cms_ctx))
  311. goto err;
  312. pwri->encryptedKey->data = key;
  313. pwri->encryptedKey->length = keylen;
  314. } else {
  315. key = OPENSSL_malloc(pwri->encryptedKey->length);
  316. if (key == NULL) {
  317. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  318. goto err;
  319. }
  320. if (!kek_unwrap_key(key, &keylen,
  321. pwri->encryptedKey->data,
  322. pwri->encryptedKey->length, kekctx)) {
  323. ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_FAILURE);
  324. goto err;
  325. }
  326. OPENSSL_clear_free(ec->key, ec->keylen);
  327. ec->key = key;
  328. ec->keylen = keylen;
  329. }
  330. r = 1;
  331. err:
  332. EVP_CIPHER_free(kekcipher);
  333. EVP_CIPHER_CTX_free(kekctx);
  334. if (!r)
  335. OPENSSL_free(key);
  336. X509_ALGOR_free(kekalg);
  337. return r;
  338. }