pk7_doit.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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_get0_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_CTX_free(pctx);
  156. OPENSSL_free(ek);
  157. return ret;
  158. }
  159. static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
  160. PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey)
  161. {
  162. EVP_PKEY_CTX *pctx = NULL;
  163. unsigned char *ek = NULL;
  164. size_t eklen;
  165. int ret = -1;
  166. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  167. if (!pctx)
  168. return -1;
  169. if (EVP_PKEY_decrypt_init(pctx) <= 0)
  170. goto err;
  171. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
  172. EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) {
  173. PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR);
  174. goto err;
  175. }
  176. if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
  177. ri->enc_key->data, ri->enc_key->length) <= 0)
  178. goto err;
  179. ek = OPENSSL_malloc(eklen);
  180. if (ek == NULL) {
  181. PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE);
  182. goto err;
  183. }
  184. if (EVP_PKEY_decrypt(pctx, ek, &eklen,
  185. ri->enc_key->data, ri->enc_key->length) <= 0) {
  186. ret = 0;
  187. PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB);
  188. goto err;
  189. }
  190. ret = 1;
  191. OPENSSL_clear_free(*pek, *peklen);
  192. *pek = ek;
  193. *peklen = eklen;
  194. err:
  195. EVP_PKEY_CTX_free(pctx);
  196. if (!ret)
  197. OPENSSL_free(ek);
  198. return ret;
  199. }
  200. BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
  201. {
  202. int i;
  203. BIO *out = NULL, *btmp = NULL;
  204. X509_ALGOR *xa = NULL;
  205. const EVP_CIPHER *evp_cipher = NULL;
  206. STACK_OF(X509_ALGOR) *md_sk = NULL;
  207. STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
  208. X509_ALGOR *xalg = NULL;
  209. PKCS7_RECIP_INFO *ri = NULL;
  210. ASN1_OCTET_STRING *os = NULL;
  211. if (p7 == NULL) {
  212. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER);
  213. return NULL;
  214. }
  215. /*
  216. * The content field in the PKCS7 ContentInfo is optional, but that really
  217. * only applies to inner content (precisely, detached signatures).
  218. *
  219. * When reading content, missing outer content is therefore treated as an
  220. * error.
  221. *
  222. * When creating content, PKCS7_content_new() must be called before
  223. * calling this method, so a NULL p7->d is always an error.
  224. */
  225. if (p7->d.ptr == NULL) {
  226. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT);
  227. return NULL;
  228. }
  229. i = OBJ_obj2nid(p7->type);
  230. p7->state = PKCS7_S_HEADER;
  231. switch (i) {
  232. case NID_pkcs7_signed:
  233. md_sk = p7->d.sign->md_algs;
  234. os = PKCS7_get_octet_string(p7->d.sign->contents);
  235. break;
  236. case NID_pkcs7_signedAndEnveloped:
  237. rsk = p7->d.signed_and_enveloped->recipientinfo;
  238. md_sk = p7->d.signed_and_enveloped->md_algs;
  239. xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
  240. evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
  241. if (evp_cipher == NULL) {
  242. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
  243. goto err;
  244. }
  245. break;
  246. case NID_pkcs7_enveloped:
  247. rsk = p7->d.enveloped->recipientinfo;
  248. xalg = p7->d.enveloped->enc_data->algorithm;
  249. evp_cipher = p7->d.enveloped->enc_data->cipher;
  250. if (evp_cipher == NULL) {
  251. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
  252. goto err;
  253. }
  254. break;
  255. case NID_pkcs7_digest:
  256. xa = p7->d.digest->md;
  257. os = PKCS7_get_octet_string(p7->d.digest->contents);
  258. break;
  259. case NID_pkcs7_data:
  260. break;
  261. default:
  262. PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  263. goto err;
  264. }
  265. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
  266. if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i)))
  267. goto err;
  268. if (xa && !PKCS7_bio_add_digest(&out, xa))
  269. goto err;
  270. if (evp_cipher != NULL) {
  271. unsigned char key[EVP_MAX_KEY_LENGTH];
  272. unsigned char iv[EVP_MAX_IV_LENGTH];
  273. int keylen, ivlen;
  274. EVP_CIPHER_CTX *ctx;
  275. if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
  276. PKCS7err(PKCS7_F_PKCS7_DATAINIT, ERR_R_BIO_LIB);
  277. goto err;
  278. }
  279. BIO_get_cipher_ctx(btmp, &ctx);
  280. keylen = EVP_CIPHER_key_length(evp_cipher);
  281. ivlen = EVP_CIPHER_iv_length(evp_cipher);
  282. xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
  283. if (ivlen > 0)
  284. if (RAND_bytes(iv, ivlen) <= 0)
  285. goto err;
  286. if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1) <= 0)
  287. goto err;
  288. if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
  289. goto err;
  290. if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
  291. goto err;
  292. if (ivlen > 0) {
  293. if (xalg->parameter == NULL) {
  294. xalg->parameter = ASN1_TYPE_new();
  295. if (xalg->parameter == NULL)
  296. goto err;
  297. }
  298. if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
  299. goto err;
  300. }
  301. /* Lets do the pub key stuff :-) */
  302. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  303. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  304. if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
  305. goto err;
  306. }
  307. OPENSSL_cleanse(key, keylen);
  308. if (out == NULL)
  309. out = btmp;
  310. else
  311. BIO_push(out, btmp);
  312. btmp = NULL;
  313. }
  314. if (bio == NULL) {
  315. if (PKCS7_is_detached(p7))
  316. bio = BIO_new(BIO_s_null());
  317. else if (os && os->length > 0)
  318. bio = BIO_new_mem_buf(os->data, os->length);
  319. if (bio == NULL) {
  320. bio = BIO_new(BIO_s_mem());
  321. if (bio == NULL)
  322. goto err;
  323. BIO_set_mem_eof_return(bio, 0);
  324. }
  325. }
  326. if (out)
  327. BIO_push(out, bio);
  328. else
  329. out = bio;
  330. return out;
  331. err:
  332. BIO_free_all(out);
  333. BIO_free_all(btmp);
  334. return NULL;
  335. }
  336. static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
  337. {
  338. int ret;
  339. ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
  340. X509_get_issuer_name(pcert));
  341. if (ret)
  342. return ret;
  343. return ASN1_INTEGER_cmp(X509_get_serialNumber(pcert),
  344. ri->issuer_and_serial->serial);
  345. }
  346. /* int */
  347. BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
  348. {
  349. int i, j;
  350. BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
  351. X509_ALGOR *xa;
  352. ASN1_OCTET_STRING *data_body = NULL;
  353. const EVP_MD *evp_md;
  354. const EVP_CIPHER *evp_cipher = NULL;
  355. EVP_CIPHER_CTX *evp_ctx = NULL;
  356. X509_ALGOR *enc_alg = NULL;
  357. STACK_OF(X509_ALGOR) *md_sk = NULL;
  358. STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
  359. PKCS7_RECIP_INFO *ri = NULL;
  360. unsigned char *ek = NULL, *tkey = NULL;
  361. int eklen = 0, tkeylen = 0;
  362. if (p7 == NULL) {
  363. PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_NULL_POINTER);
  364. return NULL;
  365. }
  366. if (p7->d.ptr == NULL) {
  367. PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
  368. return NULL;
  369. }
  370. i = OBJ_obj2nid(p7->type);
  371. p7->state = PKCS7_S_HEADER;
  372. switch (i) {
  373. case NID_pkcs7_signed:
  374. /*
  375. * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
  376. * field and optional content.
  377. * data_body is NULL if that structure has no (=detached) content
  378. * or if the contentType is wrong (i.e., not "data").
  379. */
  380. data_body = PKCS7_get_octet_string(p7->d.sign->contents);
  381. if (!PKCS7_is_detached(p7) && data_body == NULL) {
  382. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  383. PKCS7_R_INVALID_SIGNED_DATA_TYPE);
  384. goto err;
  385. }
  386. md_sk = p7->d.sign->md_algs;
  387. break;
  388. case NID_pkcs7_signedAndEnveloped:
  389. rsk = p7->d.signed_and_enveloped->recipientinfo;
  390. md_sk = p7->d.signed_and_enveloped->md_algs;
  391. /* data_body is NULL if the optional EncryptedContent is missing. */
  392. data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
  393. enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
  394. evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
  395. if (evp_cipher == NULL) {
  396. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  397. PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  398. goto err;
  399. }
  400. break;
  401. case NID_pkcs7_enveloped:
  402. rsk = p7->d.enveloped->recipientinfo;
  403. enc_alg = p7->d.enveloped->enc_data->algorithm;
  404. /* data_body is NULL if the optional EncryptedContent is missing. */
  405. data_body = p7->d.enveloped->enc_data->enc_data;
  406. evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
  407. if (evp_cipher == NULL) {
  408. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  409. PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  410. goto err;
  411. }
  412. break;
  413. default:
  414. PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  415. goto err;
  416. }
  417. /* Detached content must be supplied via in_bio instead. */
  418. if (data_body == NULL && in_bio == NULL) {
  419. PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
  420. goto err;
  421. }
  422. /* We will be checking the signature */
  423. if (md_sk != NULL) {
  424. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
  425. xa = sk_X509_ALGOR_value(md_sk, i);
  426. if ((btmp = BIO_new(BIO_f_md())) == NULL) {
  427. PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
  428. goto err;
  429. }
  430. j = OBJ_obj2nid(xa->algorithm);
  431. evp_md = EVP_get_digestbynid(j);
  432. if (evp_md == NULL) {
  433. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  434. PKCS7_R_UNKNOWN_DIGEST_TYPE);
  435. goto err;
  436. }
  437. BIO_set_md(btmp, evp_md);
  438. if (out == NULL)
  439. out = btmp;
  440. else
  441. BIO_push(out, btmp);
  442. btmp = NULL;
  443. }
  444. }
  445. if (evp_cipher != NULL) {
  446. if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
  447. PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
  448. goto err;
  449. }
  450. /*
  451. * It was encrypted, we need to decrypt the secret key with the
  452. * private key
  453. */
  454. /*
  455. * Find the recipientInfo which matches the passed certificate (if
  456. * any)
  457. */
  458. if (pcert) {
  459. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  460. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  461. if (!pkcs7_cmp_ri(ri, pcert))
  462. break;
  463. ri = NULL;
  464. }
  465. if (ri == NULL) {
  466. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  467. PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
  468. goto err;
  469. }
  470. }
  471. /* If we haven't got a certificate try each ri in turn */
  472. if (pcert == NULL) {
  473. /*
  474. * Always attempt to decrypt all rinfo even after success as a
  475. * defence against MMA timing attacks.
  476. */
  477. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  478. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  479. if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0)
  480. goto err;
  481. ERR_clear_error();
  482. }
  483. } else {
  484. /* Only exit on fatal errors, not decrypt failure */
  485. if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0)
  486. goto err;
  487. ERR_clear_error();
  488. }
  489. evp_ctx = NULL;
  490. BIO_get_cipher_ctx(etmp, &evp_ctx);
  491. if (EVP_CipherInit_ex(evp_ctx, evp_cipher, NULL, NULL, NULL, 0) <= 0)
  492. goto err;
  493. if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0)
  494. goto err;
  495. /* Generate random key as MMA defence */
  496. tkeylen = EVP_CIPHER_CTX_key_length(evp_ctx);
  497. tkey = OPENSSL_malloc(tkeylen);
  498. if (tkey == NULL)
  499. goto err;
  500. if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
  501. goto err;
  502. if (ek == NULL) {
  503. ek = tkey;
  504. eklen = tkeylen;
  505. tkey = NULL;
  506. }
  507. if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) {
  508. /*
  509. * Some S/MIME clients don't use the same key and effective key
  510. * length. The key length is determined by the size of the
  511. * decrypted RSA key.
  512. */
  513. if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) {
  514. /* Use random key as MMA defence */
  515. OPENSSL_clear_free(ek, eklen);
  516. ek = tkey;
  517. eklen = tkeylen;
  518. tkey = NULL;
  519. }
  520. }
  521. /* Clear errors so we don't leak information useful in MMA */
  522. ERR_clear_error();
  523. if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
  524. goto err;
  525. OPENSSL_clear_free(ek, eklen);
  526. ek = NULL;
  527. OPENSSL_clear_free(tkey, tkeylen);
  528. tkey = NULL;
  529. if (out == NULL)
  530. out = etmp;
  531. else
  532. BIO_push(out, etmp);
  533. etmp = NULL;
  534. }
  535. if (in_bio != NULL) {
  536. bio = in_bio;
  537. } else {
  538. if (data_body->length > 0)
  539. bio = BIO_new_mem_buf(data_body->data, data_body->length);
  540. else {
  541. bio = BIO_new(BIO_s_mem());
  542. if (bio == NULL)
  543. goto err;
  544. BIO_set_mem_eof_return(bio, 0);
  545. }
  546. if (bio == NULL)
  547. goto err;
  548. }
  549. BIO_push(out, bio);
  550. bio = NULL;
  551. return out;
  552. err:
  553. OPENSSL_clear_free(ek, eklen);
  554. OPENSSL_clear_free(tkey, tkeylen);
  555. BIO_free_all(out);
  556. BIO_free_all(btmp);
  557. BIO_free_all(etmp);
  558. BIO_free_all(bio);
  559. return NULL;
  560. }
  561. static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
  562. {
  563. for (;;) {
  564. bio = BIO_find_type(bio, BIO_TYPE_MD);
  565. if (bio == NULL) {
  566. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
  567. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  568. return NULL;
  569. }
  570. BIO_get_md_ctx(bio, pmd);
  571. if (*pmd == NULL) {
  572. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR);
  573. return NULL;
  574. }
  575. if (EVP_MD_CTX_type(*pmd) == nid)
  576. return bio;
  577. bio = BIO_next(bio);
  578. }
  579. return NULL;
  580. }
  581. static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
  582. {
  583. unsigned char md_data[EVP_MAX_MD_SIZE];
  584. unsigned int md_len;
  585. /* Add signing time if not already present */
  586. if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
  587. if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
  588. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
  589. return 0;
  590. }
  591. }
  592. /* Add digest */
  593. if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
  594. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
  595. return 0;
  596. }
  597. if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
  598. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
  599. return 0;
  600. }
  601. /* Now sign the attributes */
  602. if (!PKCS7_SIGNER_INFO_sign(si))
  603. return 0;
  604. return 1;
  605. }
  606. int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
  607. {
  608. int ret = 0;
  609. int i, j;
  610. BIO *btmp;
  611. PKCS7_SIGNER_INFO *si;
  612. EVP_MD_CTX *mdc, *ctx_tmp;
  613. STACK_OF(X509_ATTRIBUTE) *sk;
  614. STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
  615. ASN1_OCTET_STRING *os = NULL;
  616. if (p7 == NULL) {
  617. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER);
  618. return 0;
  619. }
  620. if (p7->d.ptr == NULL) {
  621. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
  622. return 0;
  623. }
  624. ctx_tmp = EVP_MD_CTX_new();
  625. if (ctx_tmp == NULL) {
  626. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
  627. return 0;
  628. }
  629. i = OBJ_obj2nid(p7->type);
  630. p7->state = PKCS7_S_HEADER;
  631. switch (i) {
  632. case NID_pkcs7_data:
  633. os = p7->d.data;
  634. break;
  635. case NID_pkcs7_signedAndEnveloped:
  636. /* XXXXXXXXXXXXXXXX */
  637. si_sk = p7->d.signed_and_enveloped->signer_info;
  638. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  639. if (os == NULL) {
  640. os = ASN1_OCTET_STRING_new();
  641. if (os == NULL) {
  642. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
  643. goto err;
  644. }
  645. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  646. }
  647. break;
  648. case NID_pkcs7_enveloped:
  649. /* XXXXXXXXXXXXXXXX */
  650. os = p7->d.enveloped->enc_data->enc_data;
  651. if (os == NULL) {
  652. os = ASN1_OCTET_STRING_new();
  653. if (os == NULL) {
  654. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
  655. goto err;
  656. }
  657. p7->d.enveloped->enc_data->enc_data = os;
  658. }
  659. break;
  660. case NID_pkcs7_signed:
  661. si_sk = p7->d.sign->signer_info;
  662. os = PKCS7_get_octet_string(p7->d.sign->contents);
  663. /* If detached data then the content is excluded */
  664. if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
  665. ASN1_OCTET_STRING_free(os);
  666. os = NULL;
  667. p7->d.sign->contents->d.data = NULL;
  668. }
  669. break;
  670. case NID_pkcs7_digest:
  671. os = PKCS7_get_octet_string(p7->d.digest->contents);
  672. /* If detached data then the content is excluded */
  673. if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
  674. ASN1_OCTET_STRING_free(os);
  675. os = NULL;
  676. p7->d.digest->contents->d.data = NULL;
  677. }
  678. break;
  679. default:
  680. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  681. goto err;
  682. }
  683. if (si_sk != NULL) {
  684. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
  685. si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
  686. if (si->pkey == NULL)
  687. continue;
  688. j = OBJ_obj2nid(si->digest_alg->algorithm);
  689. btmp = bio;
  690. btmp = PKCS7_find_digest(&mdc, btmp, j);
  691. if (btmp == NULL)
  692. goto err;
  693. /*
  694. * We now have the EVP_MD_CTX, lets do the signing.
  695. */
  696. if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
  697. goto err;
  698. sk = si->auth_attr;
  699. /*
  700. * If there are attributes, we add the digest attribute and only
  701. * sign the attributes
  702. */
  703. if (sk_X509_ATTRIBUTE_num(sk) > 0) {
  704. if (!do_pkcs7_signed_attrib(si, ctx_tmp))
  705. goto err;
  706. } else {
  707. unsigned char *abuf = NULL;
  708. unsigned int abuflen;
  709. abuflen = EVP_PKEY_size(si->pkey);
  710. abuf = OPENSSL_malloc(abuflen);
  711. if (abuf == NULL)
  712. goto err;
  713. if (!EVP_SignFinal(ctx_tmp, abuf, &abuflen, si->pkey)) {
  714. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB);
  715. goto err;
  716. }
  717. ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
  718. }
  719. }
  720. } else if (i == NID_pkcs7_digest) {
  721. unsigned char md_data[EVP_MAX_MD_SIZE];
  722. unsigned int md_len;
  723. if (!PKCS7_find_digest(&mdc, bio,
  724. OBJ_obj2nid(p7->d.digest->md->algorithm)))
  725. goto err;
  726. if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
  727. goto err;
  728. ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len);
  729. }
  730. if (!PKCS7_is_detached(p7)) {
  731. /*
  732. * NOTE(emilia): I think we only reach os == NULL here because detached
  733. * digested data support is broken.
  734. */
  735. if (os == NULL)
  736. goto err;
  737. if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
  738. char *cont;
  739. long contlen;
  740. btmp = BIO_find_type(bio, BIO_TYPE_MEM);
  741. if (btmp == NULL) {
  742. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
  743. goto err;
  744. }
  745. contlen = BIO_get_mem_data(btmp, &cont);
  746. /*
  747. * Mark the BIO read only then we can use its copy of the data
  748. * instead of making an extra copy.
  749. */
  750. BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
  751. BIO_set_mem_eof_return(btmp, 0);
  752. ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
  753. }
  754. }
  755. ret = 1;
  756. err:
  757. EVP_MD_CTX_free(ctx_tmp);
  758. return (ret);
  759. }
  760. int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
  761. {
  762. EVP_MD_CTX *mctx;
  763. EVP_PKEY_CTX *pctx;
  764. unsigned char *abuf = NULL;
  765. int alen;
  766. size_t siglen;
  767. const EVP_MD *md = NULL;
  768. md = EVP_get_digestbyobj(si->digest_alg->algorithm);
  769. if (md == NULL)
  770. return 0;
  771. mctx = EVP_MD_CTX_new();
  772. if (mctx == NULL) {
  773. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, ERR_R_MALLOC_FAILURE);
  774. goto err;
  775. }
  776. if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
  777. goto err;
  778. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  779. EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
  780. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
  781. goto err;
  782. }
  783. alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
  784. ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
  785. if (!abuf)
  786. goto err;
  787. if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
  788. goto err;
  789. OPENSSL_free(abuf);
  790. abuf = NULL;
  791. if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
  792. goto err;
  793. abuf = OPENSSL_malloc(siglen);
  794. if (abuf == NULL)
  795. goto err;
  796. if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
  797. goto err;
  798. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  799. EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
  800. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
  801. goto err;
  802. }
  803. EVP_MD_CTX_free(mctx);
  804. ASN1_STRING_set0(si->enc_digest, abuf, siglen);
  805. return 1;
  806. err:
  807. OPENSSL_free(abuf);
  808. EVP_MD_CTX_free(mctx);
  809. return 0;
  810. }
  811. int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
  812. PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  813. {
  814. PKCS7_ISSUER_AND_SERIAL *ias;
  815. int ret = 0, i;
  816. STACK_OF(X509) *cert;
  817. X509 *x509;
  818. if (p7 == NULL) {
  819. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER);
  820. return 0;
  821. }
  822. if (p7->d.ptr == NULL) {
  823. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
  824. return 0;
  825. }
  826. if (PKCS7_type_is_signed(p7)) {
  827. cert = p7->d.sign->cert;
  828. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  829. cert = p7->d.signed_and_enveloped->cert;
  830. } else {
  831. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
  832. goto err;
  833. }
  834. /* XXXXXXXXXXXXXXXXXXXXXXX */
  835. ias = si->issuer_and_serial;
  836. x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
  837. /* were we able to find the cert in passed to us */
  838. if (x509 == NULL) {
  839. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
  840. PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
  841. goto err;
  842. }
  843. /* Lets verify */
  844. if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
  845. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
  846. goto err;
  847. }
  848. X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
  849. i = X509_verify_cert(ctx);
  850. if (i <= 0) {
  851. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
  852. X509_STORE_CTX_cleanup(ctx);
  853. goto err;
  854. }
  855. X509_STORE_CTX_cleanup(ctx);
  856. return PKCS7_signatureVerify(bio, p7, si, x509);
  857. err:
  858. return ret;
  859. }
  860. int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
  861. X509 *x509)
  862. {
  863. ASN1_OCTET_STRING *os;
  864. EVP_MD_CTX *mdc_tmp, *mdc;
  865. int ret = 0, i;
  866. int md_type;
  867. STACK_OF(X509_ATTRIBUTE) *sk;
  868. BIO *btmp;
  869. EVP_PKEY *pkey;
  870. mdc_tmp = EVP_MD_CTX_new();
  871. if (mdc_tmp == NULL) {
  872. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_MALLOC_FAILURE);
  873. goto err;
  874. }
  875. if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
  876. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
  877. goto err;
  878. }
  879. md_type = OBJ_obj2nid(si->digest_alg->algorithm);
  880. btmp = bio;
  881. for (;;) {
  882. if ((btmp == NULL) ||
  883. ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
  884. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  885. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  886. goto err;
  887. }
  888. BIO_get_md_ctx(btmp, &mdc);
  889. if (mdc == NULL) {
  890. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR);
  891. goto err;
  892. }
  893. if (EVP_MD_CTX_type(mdc) == md_type)
  894. break;
  895. /*
  896. * Workaround for some broken clients that put the signature OID
  897. * instead of the digest OID in digest_alg->algorithm
  898. */
  899. if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
  900. break;
  901. btmp = BIO_next(btmp);
  902. }
  903. /*
  904. * mdc is the digest ctx that we want, unless there are attributes, in
  905. * which case the digest is the signed attributes
  906. */
  907. if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
  908. goto err;
  909. sk = si->auth_attr;
  910. if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
  911. unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
  912. unsigned int md_len;
  913. int alen;
  914. ASN1_OCTET_STRING *message_digest;
  915. if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
  916. goto err;
  917. message_digest = PKCS7_digest_from_attributes(sk);
  918. if (!message_digest) {
  919. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  920. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  921. goto err;
  922. }
  923. if ((message_digest->length != (int)md_len) ||
  924. (memcmp(message_digest->data, md_dat, md_len))) {
  925. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE);
  926. ret = -1;
  927. goto err;
  928. }
  929. if (!EVP_VerifyInit_ex(mdc_tmp, EVP_get_digestbynid(md_type), NULL))
  930. goto err;
  931. alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
  932. ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
  933. if (alen <= 0) {
  934. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB);
  935. ret = -1;
  936. goto err;
  937. }
  938. if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
  939. goto err;
  940. OPENSSL_free(abuf);
  941. }
  942. os = si->enc_digest;
  943. pkey = X509_get0_pubkey(x509);
  944. if (!pkey) {
  945. ret = -1;
  946. goto err;
  947. }
  948. i = EVP_VerifyFinal(mdc_tmp, os->data, os->length, pkey);
  949. if (i <= 0) {
  950. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE);
  951. ret = -1;
  952. goto err;
  953. }
  954. ret = 1;
  955. err:
  956. EVP_MD_CTX_free(mdc_tmp);
  957. return (ret);
  958. }
  959. PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
  960. {
  961. STACK_OF(PKCS7_RECIP_INFO) *rsk;
  962. PKCS7_RECIP_INFO *ri;
  963. int i;
  964. i = OBJ_obj2nid(p7->type);
  965. if (i != NID_pkcs7_signedAndEnveloped)
  966. return NULL;
  967. if (p7->d.signed_and_enveloped == NULL)
  968. return NULL;
  969. rsk = p7->d.signed_and_enveloped->recipientinfo;
  970. if (rsk == NULL)
  971. return NULL;
  972. if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
  973. return (NULL);
  974. ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
  975. return (ri->issuer_and_serial);
  976. }
  977. ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
  978. {
  979. return (get_attribute(si->auth_attr, nid));
  980. }
  981. ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
  982. {
  983. return (get_attribute(si->unauth_attr, nid));
  984. }
  985. static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
  986. {
  987. int idx;
  988. X509_ATTRIBUTE *xa;
  989. idx = X509at_get_attr_by_NID(sk, nid, -1);
  990. xa = X509at_get_attr(sk, idx);
  991. return X509_ATTRIBUTE_get0_type(xa, 0);
  992. }
  993. ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
  994. {
  995. ASN1_TYPE *astype;
  996. if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
  997. return NULL;
  998. return astype->value.octet_string;
  999. }
  1000. int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
  1001. STACK_OF(X509_ATTRIBUTE) *sk)
  1002. {
  1003. int i;
  1004. sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
  1005. p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
  1006. if (p7si->auth_attr == NULL)
  1007. return 0;
  1008. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1009. if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
  1010. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  1011. (sk, i))))
  1012. == NULL)
  1013. return (0);
  1014. }
  1015. return (1);
  1016. }
  1017. int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
  1018. STACK_OF(X509_ATTRIBUTE) *sk)
  1019. {
  1020. int i;
  1021. sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
  1022. p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
  1023. if (p7si->unauth_attr == NULL)
  1024. return 0;
  1025. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1026. if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
  1027. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  1028. (sk, i))))
  1029. == NULL)
  1030. return (0);
  1031. }
  1032. return (1);
  1033. }
  1034. int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1035. void *value)
  1036. {
  1037. return (add_attribute(&(p7si->auth_attr), nid, atrtype, value));
  1038. }
  1039. int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1040. void *value)
  1041. {
  1042. return (add_attribute(&(p7si->unauth_attr), nid, atrtype, value));
  1043. }
  1044. static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
  1045. void *value)
  1046. {
  1047. X509_ATTRIBUTE *attr = NULL;
  1048. if (*sk == NULL) {
  1049. if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
  1050. return 0;
  1051. new_attrib:
  1052. if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
  1053. return 0;
  1054. if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
  1055. X509_ATTRIBUTE_free(attr);
  1056. return 0;
  1057. }
  1058. } else {
  1059. int i;
  1060. for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
  1061. attr = sk_X509_ATTRIBUTE_value(*sk, i);
  1062. if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
  1063. X509_ATTRIBUTE_free(attr);
  1064. attr = X509_ATTRIBUTE_create(nid, atrtype, value);
  1065. if (attr == NULL)
  1066. return 0;
  1067. if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
  1068. X509_ATTRIBUTE_free(attr);
  1069. return 0;
  1070. }
  1071. goto end;
  1072. }
  1073. }
  1074. goto new_attrib;
  1075. }
  1076. end:
  1077. return (1);
  1078. }