cms_pwri.c 11 KB

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