pk7_doit.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /* crypto/pkcs7/pk7_doit.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/rand.h>
  61. #include <openssl/objects.h>
  62. #include <openssl/x509.h>
  63. #include <openssl/x509v3.h>
  64. #include <openssl/err.h>
  65. static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
  66. void *value);
  67. static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid);
  68. static int PKCS7_type_is_other(PKCS7 *p7)
  69. {
  70. int isOther = 1;
  71. int nid = OBJ_obj2nid(p7->type);
  72. switch (nid) {
  73. case NID_pkcs7_data:
  74. case NID_pkcs7_signed:
  75. case NID_pkcs7_enveloped:
  76. case NID_pkcs7_signedAndEnveloped:
  77. case NID_pkcs7_digest:
  78. case NID_pkcs7_encrypted:
  79. isOther = 0;
  80. break;
  81. default:
  82. isOther = 1;
  83. }
  84. return isOther;
  85. }
  86. static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
  87. {
  88. if (PKCS7_type_is_data(p7))
  89. return p7->d.data;
  90. if (PKCS7_type_is_other(p7) && p7->d.other
  91. && (p7->d.other->type == V_ASN1_OCTET_STRING))
  92. return p7->d.other->value.octet_string;
  93. return NULL;
  94. }
  95. static int PKCS7_bio_add_digest(BIO **pbio, X509_ALGOR *alg)
  96. {
  97. BIO *btmp;
  98. const EVP_MD *md;
  99. if ((btmp = BIO_new(BIO_f_md())) == NULL) {
  100. PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
  101. goto err;
  102. }
  103. md = EVP_get_digestbyobj(alg->algorithm);
  104. if (md == NULL) {
  105. PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, PKCS7_R_UNKNOWN_DIGEST_TYPE);
  106. goto err;
  107. }
  108. BIO_set_md(btmp, md);
  109. if (*pbio == NULL)
  110. *pbio = btmp;
  111. else if (!BIO_push(*pbio, btmp)) {
  112. PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
  113. goto err;
  114. }
  115. btmp = NULL;
  116. return 1;
  117. err:
  118. BIO_free(btmp);
  119. return 0;
  120. }
  121. static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
  122. unsigned char *key, int keylen)
  123. {
  124. EVP_PKEY_CTX *pctx = NULL;
  125. EVP_PKEY *pkey = NULL;
  126. unsigned char *ek = NULL;
  127. int ret = 0;
  128. size_t eklen;
  129. pkey = X509_get_pubkey(ri->cert);
  130. if (!pkey)
  131. return 0;
  132. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  133. if (!pctx)
  134. return 0;
  135. if (EVP_PKEY_encrypt_init(pctx) <= 0)
  136. goto err;
  137. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
  138. EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) {
  139. PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR);
  140. goto err;
  141. }
  142. if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
  143. goto err;
  144. ek = OPENSSL_malloc(eklen);
  145. if (ek == NULL) {
  146. PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE);
  147. goto err;
  148. }
  149. if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
  150. goto err;
  151. ASN1_STRING_set0(ri->enc_key, ek, eklen);
  152. ek = NULL;
  153. ret = 1;
  154. err:
  155. EVP_PKEY_free(pkey);
  156. EVP_PKEY_CTX_free(pctx);
  157. OPENSSL_free(ek);
  158. return ret;
  159. }
  160. static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
  161. PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey)
  162. {
  163. EVP_PKEY_CTX *pctx = NULL;
  164. unsigned char *ek = NULL;
  165. size_t eklen;
  166. int ret = -1;
  167. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  168. if (!pctx)
  169. return -1;
  170. if (EVP_PKEY_decrypt_init(pctx) <= 0)
  171. goto err;
  172. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
  173. EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) {
  174. PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR);
  175. goto err;
  176. }
  177. if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
  178. ri->enc_key->data, ri->enc_key->length) <= 0)
  179. goto err;
  180. ek = OPENSSL_malloc(eklen);
  181. if (ek == NULL) {
  182. PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE);
  183. goto err;
  184. }
  185. if (EVP_PKEY_decrypt(pctx, ek, &eklen,
  186. ri->enc_key->data, ri->enc_key->length) <= 0) {
  187. ret = 0;
  188. PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB);
  189. goto err;
  190. }
  191. ret = 1;
  192. OPENSSL_clear_free(*pek, *peklen);
  193. *pek = ek;
  194. *peklen = eklen;
  195. err:
  196. EVP_PKEY_CTX_free(pctx);
  197. if (!ret)
  198. OPENSSL_free(ek);
  199. return ret;
  200. }
  201. BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
  202. {
  203. int i;
  204. BIO *out = NULL, *btmp = NULL;
  205. X509_ALGOR *xa = NULL;
  206. const EVP_CIPHER *evp_cipher = NULL;
  207. STACK_OF(X509_ALGOR) *md_sk = NULL;
  208. STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
  209. X509_ALGOR *xalg = NULL;
  210. PKCS7_RECIP_INFO *ri = NULL;
  211. ASN1_OCTET_STRING *os = NULL;
  212. if (p7 == NULL) {
  213. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER);
  214. return NULL;
  215. }
  216. /*
  217. * The content field in the PKCS7 ContentInfo is optional, but that really
  218. * only applies to inner content (precisely, detached signatures).
  219. *
  220. * When reading content, missing outer content is therefore treated as an
  221. * error.
  222. *
  223. * When creating content, PKCS7_content_new() must be called before
  224. * calling this method, so a NULL p7->d is always an error.
  225. */
  226. if (p7->d.ptr == NULL) {
  227. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT);
  228. return NULL;
  229. }
  230. i = OBJ_obj2nid(p7->type);
  231. p7->state = PKCS7_S_HEADER;
  232. switch (i) {
  233. case NID_pkcs7_signed:
  234. md_sk = p7->d.sign->md_algs;
  235. os = PKCS7_get_octet_string(p7->d.sign->contents);
  236. break;
  237. case NID_pkcs7_signedAndEnveloped:
  238. rsk = p7->d.signed_and_enveloped->recipientinfo;
  239. md_sk = p7->d.signed_and_enveloped->md_algs;
  240. xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
  241. evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
  242. if (evp_cipher == NULL) {
  243. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
  244. goto err;
  245. }
  246. break;
  247. case NID_pkcs7_enveloped:
  248. rsk = p7->d.enveloped->recipientinfo;
  249. xalg = p7->d.enveloped->enc_data->algorithm;
  250. evp_cipher = p7->d.enveloped->enc_data->cipher;
  251. if (evp_cipher == NULL) {
  252. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
  253. goto err;
  254. }
  255. break;
  256. case NID_pkcs7_digest:
  257. xa = p7->d.digest->md;
  258. os = PKCS7_get_octet_string(p7->d.digest->contents);
  259. break;
  260. case NID_pkcs7_data:
  261. break;
  262. default:
  263. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  264. goto err;
  265. }
  266. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
  267. if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i)))
  268. goto err;
  269. if (xa && !PKCS7_bio_add_digest(&out, xa))
  270. goto err;
  271. if (evp_cipher != NULL) {
  272. unsigned char key[EVP_MAX_KEY_LENGTH];
  273. unsigned char iv[EVP_MAX_IV_LENGTH];
  274. int keylen, ivlen;
  275. EVP_CIPHER_CTX *ctx;
  276. if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
  277. PKCS7err(PKCS7_F_PKCS7_DATAINIT, ERR_R_BIO_LIB);
  278. goto err;
  279. }
  280. BIO_get_cipher_ctx(btmp, &ctx);
  281. keylen = EVP_CIPHER_key_length(evp_cipher);
  282. ivlen = EVP_CIPHER_iv_length(evp_cipher);
  283. xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
  284. if (ivlen > 0)
  285. if (RAND_bytes(iv, ivlen) <= 0)
  286. goto err;
  287. if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1) <= 0)
  288. goto err;
  289. if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
  290. goto err;
  291. if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
  292. goto err;
  293. if (ivlen > 0) {
  294. if (xalg->parameter == NULL) {
  295. xalg->parameter = ASN1_TYPE_new();
  296. if (xalg->parameter == NULL)
  297. goto err;
  298. }
  299. if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
  300. goto err;
  301. }
  302. /* Lets do the pub key stuff :-) */
  303. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  304. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  305. if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
  306. goto err;
  307. }
  308. OPENSSL_cleanse(key, keylen);
  309. if (out == NULL)
  310. out = btmp;
  311. else
  312. BIO_push(out, btmp);
  313. btmp = NULL;
  314. }
  315. if (bio == NULL) {
  316. if (PKCS7_is_detached(p7))
  317. bio = BIO_new(BIO_s_null());
  318. else if (os && os->length > 0)
  319. bio = BIO_new_mem_buf(os->data, os->length);
  320. if (bio == NULL) {
  321. bio = BIO_new(BIO_s_mem());
  322. if (bio == NULL)
  323. goto err;
  324. BIO_set_mem_eof_return(bio, 0);
  325. }
  326. }
  327. if (out)
  328. BIO_push(out, bio);
  329. else
  330. out = bio;
  331. return out;
  332. err:
  333. BIO_free_all(out);
  334. BIO_free_all(btmp);
  335. return NULL;
  336. }
  337. static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
  338. {
  339. int ret;
  340. ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
  341. pcert->cert_info->issuer);
  342. if (ret)
  343. return ret;
  344. return ASN1_INTEGER_cmp(pcert->cert_info->serialNumber,
  345. ri->issuer_and_serial->serial);
  346. }
  347. /* int */
  348. BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
  349. {
  350. int i, j;
  351. BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
  352. X509_ALGOR *xa;
  353. ASN1_OCTET_STRING *data_body = NULL;
  354. const EVP_MD *evp_md;
  355. const EVP_CIPHER *evp_cipher = NULL;
  356. EVP_CIPHER_CTX *evp_ctx = NULL;
  357. X509_ALGOR *enc_alg = NULL;
  358. STACK_OF(X509_ALGOR) *md_sk = NULL;
  359. STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
  360. PKCS7_RECIP_INFO *ri = NULL;
  361. unsigned char *ek = NULL, *tkey = NULL;
  362. int eklen = 0, tkeylen = 0;
  363. if (p7 == NULL) {
  364. PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_NULL_POINTER);
  365. return NULL;
  366. }
  367. if (p7->d.ptr == NULL) {
  368. PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
  369. return NULL;
  370. }
  371. i = OBJ_obj2nid(p7->type);
  372. p7->state = PKCS7_S_HEADER;
  373. switch (i) {
  374. case NID_pkcs7_signed:
  375. data_body = PKCS7_get_octet_string(p7->d.sign->contents);
  376. if (!PKCS7_is_detached(p7) && data_body == NULL) {
  377. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  378. PKCS7_R_INVALID_SIGNED_DATA_TYPE);
  379. goto err;
  380. }
  381. md_sk = p7->d.sign->md_algs;
  382. break;
  383. case NID_pkcs7_signedAndEnveloped:
  384. rsk = p7->d.signed_and_enveloped->recipientinfo;
  385. md_sk = p7->d.signed_and_enveloped->md_algs;
  386. data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
  387. enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
  388. evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
  389. if (evp_cipher == NULL) {
  390. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  391. PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  392. goto err;
  393. }
  394. break;
  395. case NID_pkcs7_enveloped:
  396. rsk = p7->d.enveloped->recipientinfo;
  397. enc_alg = p7->d.enveloped->enc_data->algorithm;
  398. data_body = p7->d.enveloped->enc_data->enc_data;
  399. evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
  400. if (evp_cipher == NULL) {
  401. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  402. PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  403. goto err;
  404. }
  405. break;
  406. default:
  407. PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  408. goto err;
  409. }
  410. /* We will be checking the signature */
  411. if (md_sk != NULL) {
  412. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
  413. xa = sk_X509_ALGOR_value(md_sk, i);
  414. if ((btmp = BIO_new(BIO_f_md())) == NULL) {
  415. PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
  416. goto err;
  417. }
  418. j = OBJ_obj2nid(xa->algorithm);
  419. evp_md = EVP_get_digestbynid(j);
  420. if (evp_md == NULL) {
  421. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  422. PKCS7_R_UNKNOWN_DIGEST_TYPE);
  423. goto err;
  424. }
  425. BIO_set_md(btmp, evp_md);
  426. if (out == NULL)
  427. out = btmp;
  428. else
  429. BIO_push(out, btmp);
  430. btmp = NULL;
  431. }
  432. }
  433. if (evp_cipher != NULL) {
  434. if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
  435. PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
  436. goto err;
  437. }
  438. /*
  439. * It was encrypted, we need to decrypt the secret key with the
  440. * private key
  441. */
  442. /*
  443. * Find the recipientInfo which matches the passed certificate (if
  444. * any)
  445. */
  446. if (pcert) {
  447. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  448. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  449. if (!pkcs7_cmp_ri(ri, pcert))
  450. break;
  451. ri = NULL;
  452. }
  453. if (ri == NULL) {
  454. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  455. PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
  456. goto err;
  457. }
  458. }
  459. /* If we haven't got a certificate try each ri in turn */
  460. if (pcert == NULL) {
  461. /*
  462. * Always attempt to decrypt all rinfo even after success as a
  463. * defence against MMA timing attacks.
  464. */
  465. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  466. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  467. if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0)
  468. goto err;
  469. ERR_clear_error();
  470. }
  471. } else {
  472. /* Only exit on fatal errors, not decrypt failure */
  473. if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0)
  474. goto err;
  475. ERR_clear_error();
  476. }
  477. evp_ctx = NULL;
  478. BIO_get_cipher_ctx(etmp, &evp_ctx);
  479. if (EVP_CipherInit_ex(evp_ctx, evp_cipher, NULL, NULL, NULL, 0) <= 0)
  480. goto err;
  481. if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0)
  482. goto err;
  483. /* Generate random key as MMA defence */
  484. tkeylen = EVP_CIPHER_CTX_key_length(evp_ctx);
  485. tkey = OPENSSL_malloc(tkeylen);
  486. if (!tkey)
  487. goto err;
  488. if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
  489. goto err;
  490. if (ek == NULL) {
  491. ek = tkey;
  492. eklen = tkeylen;
  493. tkey = NULL;
  494. }
  495. if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) {
  496. /*
  497. * Some S/MIME clients don't use the same key and effective key
  498. * length. The key length is determined by the size of the
  499. * decrypted RSA key.
  500. */
  501. if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) {
  502. /* Use random key as MMA defence */
  503. OPENSSL_clear_free(ek, eklen);
  504. ek = tkey;
  505. eklen = tkeylen;
  506. tkey = NULL;
  507. }
  508. }
  509. /* Clear errors so we don't leak information useful in MMA */
  510. ERR_clear_error();
  511. if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
  512. goto err;
  513. OPENSSL_clear_free(ek, eklen);
  514. ek = NULL;
  515. OPENSSL_clear_free(tkey, tkeylen);
  516. tkey = NULL;
  517. if (out == NULL)
  518. out = etmp;
  519. else
  520. BIO_push(out, etmp);
  521. etmp = NULL;
  522. }
  523. if (PKCS7_is_detached(p7) || (in_bio != NULL)) {
  524. bio = in_bio;
  525. } else {
  526. if (data_body->length > 0)
  527. bio = BIO_new_mem_buf(data_body->data, data_body->length);
  528. else {
  529. bio = BIO_new(BIO_s_mem());
  530. BIO_set_mem_eof_return(bio, 0);
  531. }
  532. if (bio == NULL)
  533. goto err;
  534. }
  535. BIO_push(out, bio);
  536. bio = NULL;
  537. return out;
  538. err:
  539. OPENSSL_clear_free(ek, eklen);
  540. OPENSSL_clear_free(tkey, tkeylen);
  541. BIO_free_all(out);
  542. BIO_free_all(btmp);
  543. BIO_free_all(etmp);
  544. BIO_free_all(bio);
  545. return NULL;
  546. }
  547. static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
  548. {
  549. for (;;) {
  550. bio = BIO_find_type(bio, BIO_TYPE_MD);
  551. if (bio == NULL) {
  552. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
  553. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  554. return NULL;
  555. }
  556. BIO_get_md_ctx(bio, pmd);
  557. if (*pmd == NULL) {
  558. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR);
  559. return NULL;
  560. }
  561. if (EVP_MD_CTX_type(*pmd) == nid)
  562. return bio;
  563. bio = BIO_next(bio);
  564. }
  565. return NULL;
  566. }
  567. static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
  568. {
  569. unsigned char md_data[EVP_MAX_MD_SIZE];
  570. unsigned int md_len;
  571. /* Add signing time if not already present */
  572. if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
  573. if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
  574. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
  575. return 0;
  576. }
  577. }
  578. /* Add digest */
  579. if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
  580. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
  581. return 0;
  582. }
  583. if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
  584. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
  585. return 0;
  586. }
  587. /* Now sign the attributes */
  588. if (!PKCS7_SIGNER_INFO_sign(si))
  589. return 0;
  590. return 1;
  591. }
  592. int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
  593. {
  594. int ret = 0;
  595. int i, j;
  596. BIO *btmp;
  597. PKCS7_SIGNER_INFO *si;
  598. EVP_MD_CTX *mdc, ctx_tmp;
  599. STACK_OF(X509_ATTRIBUTE) *sk;
  600. STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
  601. ASN1_OCTET_STRING *os = NULL;
  602. if (p7 == NULL) {
  603. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER);
  604. return 0;
  605. }
  606. if (p7->d.ptr == NULL) {
  607. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
  608. return 0;
  609. }
  610. EVP_MD_CTX_init(&ctx_tmp);
  611. i = OBJ_obj2nid(p7->type);
  612. p7->state = PKCS7_S_HEADER;
  613. switch (i) {
  614. case NID_pkcs7_data:
  615. os = p7->d.data;
  616. break;
  617. case NID_pkcs7_signedAndEnveloped:
  618. /* XXXXXXXXXXXXXXXX */
  619. si_sk = p7->d.signed_and_enveloped->signer_info;
  620. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  621. if (!os) {
  622. os = ASN1_OCTET_STRING_new();
  623. if (!os) {
  624. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
  625. goto err;
  626. }
  627. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  628. }
  629. break;
  630. case NID_pkcs7_enveloped:
  631. /* XXXXXXXXXXXXXXXX */
  632. os = p7->d.enveloped->enc_data->enc_data;
  633. if (!os) {
  634. os = ASN1_OCTET_STRING_new();
  635. if (!os) {
  636. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
  637. goto err;
  638. }
  639. p7->d.enveloped->enc_data->enc_data = os;
  640. }
  641. break;
  642. case NID_pkcs7_signed:
  643. si_sk = p7->d.sign->signer_info;
  644. os = PKCS7_get_octet_string(p7->d.sign->contents);
  645. /* If detached data then the content is excluded */
  646. if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
  647. ASN1_OCTET_STRING_free(os);
  648. os = NULL;
  649. p7->d.sign->contents->d.data = NULL;
  650. }
  651. break;
  652. case NID_pkcs7_digest:
  653. os = PKCS7_get_octet_string(p7->d.digest->contents);
  654. /* If detached data then the content is excluded */
  655. if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
  656. ASN1_OCTET_STRING_free(os);
  657. os = NULL;
  658. p7->d.digest->contents->d.data = NULL;
  659. }
  660. break;
  661. default:
  662. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  663. goto err;
  664. }
  665. if (si_sk != NULL) {
  666. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
  667. si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
  668. if (si->pkey == NULL)
  669. continue;
  670. j = OBJ_obj2nid(si->digest_alg->algorithm);
  671. btmp = bio;
  672. btmp = PKCS7_find_digest(&mdc, btmp, j);
  673. if (btmp == NULL)
  674. goto err;
  675. /*
  676. * We now have the EVP_MD_CTX, lets do the signing.
  677. */
  678. if (!EVP_MD_CTX_copy_ex(&ctx_tmp, mdc))
  679. goto err;
  680. sk = si->auth_attr;
  681. /*
  682. * If there are attributes, we add the digest attribute and only
  683. * sign the attributes
  684. */
  685. if (sk_X509_ATTRIBUTE_num(sk) > 0) {
  686. if (!do_pkcs7_signed_attrib(si, &ctx_tmp))
  687. goto err;
  688. } else {
  689. unsigned char *abuf = NULL;
  690. unsigned int abuflen;
  691. abuflen = EVP_PKEY_size(si->pkey);
  692. abuf = OPENSSL_malloc(abuflen);
  693. if (!abuf)
  694. goto err;
  695. if (!EVP_SignFinal(&ctx_tmp, abuf, &abuflen, si->pkey)) {
  696. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB);
  697. goto err;
  698. }
  699. ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
  700. }
  701. }
  702. } else if (i == NID_pkcs7_digest) {
  703. unsigned char md_data[EVP_MAX_MD_SIZE];
  704. unsigned int md_len;
  705. if (!PKCS7_find_digest(&mdc, bio,
  706. OBJ_obj2nid(p7->d.digest->md->algorithm)))
  707. goto err;
  708. if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
  709. goto err;
  710. ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len);
  711. }
  712. if (!PKCS7_is_detached(p7)) {
  713. /*
  714. * NOTE(emilia): I think we only reach os == NULL here because detached
  715. * digested data support is broken.
  716. */
  717. if (os == NULL)
  718. goto err;
  719. if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
  720. char *cont;
  721. long contlen;
  722. btmp = BIO_find_type(bio, BIO_TYPE_MEM);
  723. if (btmp == NULL) {
  724. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
  725. goto err;
  726. }
  727. contlen = BIO_get_mem_data(btmp, &cont);
  728. /*
  729. * Mark the BIO read only then we can use its copy of the data
  730. * instead of making an extra copy.
  731. */
  732. BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
  733. BIO_set_mem_eof_return(btmp, 0);
  734. ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
  735. }
  736. }
  737. ret = 1;
  738. err:
  739. EVP_MD_CTX_cleanup(&ctx_tmp);
  740. return (ret);
  741. }
  742. int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
  743. {
  744. EVP_MD_CTX mctx;
  745. EVP_PKEY_CTX *pctx;
  746. unsigned char *abuf = NULL;
  747. int alen;
  748. size_t siglen;
  749. const EVP_MD *md = NULL;
  750. md = EVP_get_digestbyobj(si->digest_alg->algorithm);
  751. if (md == NULL)
  752. return 0;
  753. EVP_MD_CTX_init(&mctx);
  754. if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
  755. goto err;
  756. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  757. EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
  758. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
  759. goto err;
  760. }
  761. alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
  762. ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
  763. if (!abuf)
  764. goto err;
  765. if (EVP_DigestSignUpdate(&mctx, abuf, alen) <= 0)
  766. goto err;
  767. OPENSSL_free(abuf);
  768. abuf = NULL;
  769. if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0)
  770. goto err;
  771. abuf = OPENSSL_malloc(siglen);
  772. if (!abuf)
  773. goto err;
  774. if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0)
  775. goto err;
  776. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  777. EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
  778. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
  779. goto err;
  780. }
  781. EVP_MD_CTX_cleanup(&mctx);
  782. ASN1_STRING_set0(si->enc_digest, abuf, siglen);
  783. return 1;
  784. err:
  785. OPENSSL_free(abuf);
  786. EVP_MD_CTX_cleanup(&mctx);
  787. return 0;
  788. }
  789. int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
  790. PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  791. {
  792. PKCS7_ISSUER_AND_SERIAL *ias;
  793. int ret = 0, i;
  794. STACK_OF(X509) *cert;
  795. X509 *x509;
  796. if (p7 == NULL) {
  797. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER);
  798. return 0;
  799. }
  800. if (p7->d.ptr == NULL) {
  801. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
  802. return 0;
  803. }
  804. if (PKCS7_type_is_signed(p7)) {
  805. cert = p7->d.sign->cert;
  806. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  807. cert = p7->d.signed_and_enveloped->cert;
  808. } else {
  809. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
  810. goto err;
  811. }
  812. /* XXXXXXXXXXXXXXXXXXXXXXX */
  813. ias = si->issuer_and_serial;
  814. x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
  815. /* were we able to find the cert in passed to us */
  816. if (x509 == NULL) {
  817. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
  818. PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
  819. goto err;
  820. }
  821. /* Lets verify */
  822. if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
  823. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
  824. goto err;
  825. }
  826. X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
  827. i = X509_verify_cert(ctx);
  828. if (i <= 0) {
  829. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
  830. X509_STORE_CTX_cleanup(ctx);
  831. goto err;
  832. }
  833. X509_STORE_CTX_cleanup(ctx);
  834. return PKCS7_signatureVerify(bio, p7, si, x509);
  835. err:
  836. return ret;
  837. }
  838. int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
  839. X509 *x509)
  840. {
  841. ASN1_OCTET_STRING *os;
  842. EVP_MD_CTX mdc_tmp, *mdc;
  843. int ret = 0, i;
  844. int md_type;
  845. STACK_OF(X509_ATTRIBUTE) *sk;
  846. BIO *btmp;
  847. EVP_PKEY *pkey;
  848. EVP_MD_CTX_init(&mdc_tmp);
  849. if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
  850. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
  851. goto err;
  852. }
  853. md_type = OBJ_obj2nid(si->digest_alg->algorithm);
  854. btmp = bio;
  855. for (;;) {
  856. if ((btmp == NULL) ||
  857. ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
  858. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  859. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  860. goto err;
  861. }
  862. BIO_get_md_ctx(btmp, &mdc);
  863. if (mdc == NULL) {
  864. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR);
  865. goto err;
  866. }
  867. if (EVP_MD_CTX_type(mdc) == md_type)
  868. break;
  869. /*
  870. * Workaround for some broken clients that put the signature OID
  871. * instead of the digest OID in digest_alg->algorithm
  872. */
  873. if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
  874. break;
  875. btmp = BIO_next(btmp);
  876. }
  877. /*
  878. * mdc is the digest ctx that we want, unless there are attributes, in
  879. * which case the digest is the signed attributes
  880. */
  881. if (!EVP_MD_CTX_copy_ex(&mdc_tmp, mdc))
  882. goto err;
  883. sk = si->auth_attr;
  884. if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
  885. unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
  886. unsigned int md_len;
  887. int alen;
  888. ASN1_OCTET_STRING *message_digest;
  889. if (!EVP_DigestFinal_ex(&mdc_tmp, md_dat, &md_len))
  890. goto err;
  891. message_digest = PKCS7_digest_from_attributes(sk);
  892. if (!message_digest) {
  893. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  894. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  895. goto err;
  896. }
  897. if ((message_digest->length != (int)md_len) ||
  898. (memcmp(message_digest->data, md_dat, md_len))) {
  899. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE);
  900. ret = -1;
  901. goto err;
  902. }
  903. if (!EVP_VerifyInit_ex(&mdc_tmp, EVP_get_digestbynid(md_type), NULL))
  904. goto err;
  905. alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
  906. ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
  907. if (alen <= 0) {
  908. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB);
  909. ret = -1;
  910. goto err;
  911. }
  912. if (!EVP_VerifyUpdate(&mdc_tmp, abuf, alen))
  913. goto err;
  914. OPENSSL_free(abuf);
  915. }
  916. os = si->enc_digest;
  917. pkey = X509_get_pubkey(x509);
  918. if (!pkey) {
  919. ret = -1;
  920. goto err;
  921. }
  922. i = EVP_VerifyFinal(&mdc_tmp, os->data, os->length, pkey);
  923. EVP_PKEY_free(pkey);
  924. if (i <= 0) {
  925. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE);
  926. ret = -1;
  927. goto err;
  928. }
  929. ret = 1;
  930. err:
  931. EVP_MD_CTX_cleanup(&mdc_tmp);
  932. return (ret);
  933. }
  934. PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
  935. {
  936. STACK_OF(PKCS7_RECIP_INFO) *rsk;
  937. PKCS7_RECIP_INFO *ri;
  938. int i;
  939. i = OBJ_obj2nid(p7->type);
  940. if (i != NID_pkcs7_signedAndEnveloped)
  941. return NULL;
  942. if (p7->d.signed_and_enveloped == NULL)
  943. return NULL;
  944. rsk = p7->d.signed_and_enveloped->recipientinfo;
  945. if (rsk == NULL)
  946. return NULL;
  947. ri = sk_PKCS7_RECIP_INFO_value(rsk, 0);
  948. if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
  949. return (NULL);
  950. ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
  951. return (ri->issuer_and_serial);
  952. }
  953. ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
  954. {
  955. return (get_attribute(si->auth_attr, nid));
  956. }
  957. ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
  958. {
  959. return (get_attribute(si->unauth_attr, nid));
  960. }
  961. static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
  962. {
  963. int idx;
  964. X509_ATTRIBUTE *xa;
  965. idx = X509at_get_attr_by_NID(sk, nid, -1);
  966. xa = X509at_get_attr(sk, idx);
  967. return X509_ATTRIBUTE_get0_type(xa, 0);
  968. }
  969. ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
  970. {
  971. ASN1_TYPE *astype;
  972. if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
  973. return NULL;
  974. return astype->value.octet_string;
  975. }
  976. int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
  977. STACK_OF(X509_ATTRIBUTE) *sk)
  978. {
  979. int i;
  980. sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
  981. p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
  982. if (p7si->auth_attr == NULL)
  983. return 0;
  984. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  985. if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
  986. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  987. (sk, i))))
  988. == NULL)
  989. return (0);
  990. }
  991. return (1);
  992. }
  993. int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
  994. STACK_OF(X509_ATTRIBUTE) *sk)
  995. {
  996. int i;
  997. sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
  998. p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
  999. if (p7si->unauth_attr == NULL)
  1000. return 0;
  1001. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1002. if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
  1003. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  1004. (sk, i))))
  1005. == NULL)
  1006. return (0);
  1007. }
  1008. return (1);
  1009. }
  1010. int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1011. void *value)
  1012. {
  1013. return (add_attribute(&(p7si->auth_attr), nid, atrtype, value));
  1014. }
  1015. int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1016. void *value)
  1017. {
  1018. return (add_attribute(&(p7si->unauth_attr), nid, atrtype, value));
  1019. }
  1020. static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
  1021. void *value)
  1022. {
  1023. X509_ATTRIBUTE *attr = NULL;
  1024. if (*sk == NULL) {
  1025. if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
  1026. return 0;
  1027. new_attrib:
  1028. if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
  1029. return 0;
  1030. if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
  1031. X509_ATTRIBUTE_free(attr);
  1032. return 0;
  1033. }
  1034. } else {
  1035. int i;
  1036. for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
  1037. attr = sk_X509_ATTRIBUTE_value(*sk, i);
  1038. if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
  1039. X509_ATTRIBUTE_free(attr);
  1040. attr = X509_ATTRIBUTE_create(nid, atrtype, value);
  1041. if (attr == NULL)
  1042. return 0;
  1043. if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
  1044. X509_ATTRIBUTE_free(attr);
  1045. return 0;
  1046. }
  1047. goto end;
  1048. }
  1049. }
  1050. goto new_attrib;
  1051. }
  1052. end:
  1053. return (1);
  1054. }