pk7_doit.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. /*
  2. * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <openssl/rand.h>
  11. #include <openssl/objects.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/x509v3.h>
  14. #include <openssl/err.h>
  15. #include "internal/cryptlib.h"
  16. #include "internal/sizes.h"
  17. #include "crypto/evp.h"
  18. #include "pk7_local.h"
  19. static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
  20. void *value);
  21. static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid);
  22. int PKCS7_type_is_other(PKCS7 *p7)
  23. {
  24. int isOther = 1;
  25. int nid = OBJ_obj2nid(p7->type);
  26. switch (nid) {
  27. case NID_pkcs7_data:
  28. case NID_pkcs7_signed:
  29. case NID_pkcs7_enveloped:
  30. case NID_pkcs7_signedAndEnveloped:
  31. case NID_pkcs7_digest:
  32. case NID_pkcs7_encrypted:
  33. isOther = 0;
  34. break;
  35. default:
  36. isOther = 1;
  37. }
  38. return isOther;
  39. }
  40. ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
  41. {
  42. if (PKCS7_type_is_data(p7))
  43. return p7->d.data;
  44. if (PKCS7_type_is_other(p7) && p7->d.other
  45. && (p7->d.other->type == V_ASN1_OCTET_STRING))
  46. return p7->d.other->value.octet_string;
  47. return NULL;
  48. }
  49. static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
  50. const PKCS7_CTX *ctx)
  51. {
  52. BIO *btmp;
  53. char name[OSSL_MAX_NAME_SIZE];
  54. EVP_MD *fetched = NULL;
  55. const EVP_MD *md;
  56. if ((btmp = BIO_new(BIO_f_md())) == NULL) {
  57. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  58. goto err;
  59. }
  60. OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0);
  61. (void)ERR_set_mark();
  62. fetched = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
  63. ossl_pkcs7_ctx_get0_propq(ctx));
  64. if (fetched != NULL)
  65. md = fetched;
  66. else
  67. md = EVP_get_digestbyname(name);
  68. if (md == NULL) {
  69. (void)ERR_clear_last_mark();
  70. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
  71. goto err;
  72. }
  73. (void)ERR_pop_to_mark();
  74. if (BIO_set_md(btmp, md) <= 0) {
  75. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  76. EVP_MD_free(fetched);
  77. goto err;
  78. }
  79. EVP_MD_free(fetched);
  80. if (*pbio == NULL)
  81. *pbio = btmp;
  82. else if (!BIO_push(*pbio, btmp)) {
  83. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  84. goto err;
  85. }
  86. btmp = NULL;
  87. return 1;
  88. err:
  89. BIO_free(btmp);
  90. return 0;
  91. }
  92. static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
  93. unsigned char *key, int keylen)
  94. {
  95. EVP_PKEY_CTX *pctx = NULL;
  96. EVP_PKEY *pkey = NULL;
  97. unsigned char *ek = NULL;
  98. int ret = 0;
  99. size_t eklen;
  100. const PKCS7_CTX *ctx = ri->ctx;
  101. pkey = X509_get0_pubkey(ri->cert);
  102. if (pkey == NULL)
  103. return 0;
  104. pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
  105. ossl_pkcs7_ctx_get0_propq(ctx));
  106. if (pctx == NULL)
  107. return 0;
  108. if (EVP_PKEY_encrypt_init(pctx) <= 0)
  109. goto err;
  110. if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
  111. goto err;
  112. ek = OPENSSL_malloc(eklen);
  113. if (ek == NULL)
  114. goto err;
  115. if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
  116. goto err;
  117. ASN1_STRING_set0(ri->enc_key, ek, eklen);
  118. ek = NULL;
  119. ret = 1;
  120. err:
  121. EVP_PKEY_CTX_free(pctx);
  122. OPENSSL_free(ek);
  123. return ret;
  124. }
  125. static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
  126. PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
  127. size_t fixlen)
  128. {
  129. EVP_PKEY_CTX *pctx = NULL;
  130. unsigned char *ek = NULL;
  131. size_t eklen;
  132. int ret = -1;
  133. const PKCS7_CTX *ctx = ri->ctx;
  134. pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
  135. ossl_pkcs7_ctx_get0_propq(ctx));
  136. if (pctx == NULL)
  137. return -1;
  138. if (EVP_PKEY_decrypt_init(pctx) <= 0)
  139. goto err;
  140. if (EVP_PKEY_is_a(pkey, "RSA"))
  141. /* upper layer pkcs7 code incorrectly assumes that a successful RSA
  142. * decryption means that the key matches ciphertext (which never
  143. * was the case, implicit rejection or not), so to make it work
  144. * disable implicit rejection for RSA keys */
  145. EVP_PKEY_CTX_ctrl_str(pctx, "rsa_pkcs1_implicit_rejection", "0");
  146. ret = evp_pkey_decrypt_alloc(pctx, &ek, &eklen, fixlen,
  147. ri->enc_key->data, ri->enc_key->length);
  148. if (ret <= 0)
  149. goto err;
  150. ret = 1;
  151. OPENSSL_clear_free(*pek, *peklen);
  152. *pek = ek;
  153. *peklen = eklen;
  154. err:
  155. EVP_PKEY_CTX_free(pctx);
  156. if (!ret)
  157. OPENSSL_free(ek);
  158. return ret;
  159. }
  160. BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
  161. {
  162. int i;
  163. BIO *out = NULL, *btmp = NULL;
  164. X509_ALGOR *xa = NULL;
  165. EVP_CIPHER *fetched_cipher = NULL;
  166. const EVP_CIPHER *cipher;
  167. const EVP_CIPHER *evp_cipher = NULL;
  168. STACK_OF(X509_ALGOR) *md_sk = NULL;
  169. STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
  170. X509_ALGOR *xalg = NULL;
  171. PKCS7_RECIP_INFO *ri = NULL;
  172. ASN1_OCTET_STRING *os = NULL;
  173. const PKCS7_CTX *p7_ctx;
  174. OSSL_LIB_CTX *libctx;
  175. const char *propq;
  176. if (p7 == NULL) {
  177. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  178. return NULL;
  179. }
  180. p7_ctx = ossl_pkcs7_get0_ctx(p7);
  181. libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
  182. propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
  183. /*
  184. * The content field in the PKCS7 ContentInfo is optional, but that really
  185. * only applies to inner content (precisely, detached signatures).
  186. *
  187. * When reading content, missing outer content is therefore treated as an
  188. * error.
  189. *
  190. * When creating content, PKCS7_content_new() must be called before
  191. * calling this method, so a NULL p7->d is always an error.
  192. */
  193. if (p7->d.ptr == NULL) {
  194. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
  195. return NULL;
  196. }
  197. i = OBJ_obj2nid(p7->type);
  198. p7->state = PKCS7_S_HEADER;
  199. switch (i) {
  200. case NID_pkcs7_signed:
  201. md_sk = p7->d.sign->md_algs;
  202. os = PKCS7_get_octet_string(p7->d.sign->contents);
  203. break;
  204. case NID_pkcs7_signedAndEnveloped:
  205. rsk = p7->d.signed_and_enveloped->recipientinfo;
  206. md_sk = p7->d.signed_and_enveloped->md_algs;
  207. xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
  208. evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
  209. if (evp_cipher == NULL) {
  210. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
  211. goto err;
  212. }
  213. break;
  214. case NID_pkcs7_enveloped:
  215. rsk = p7->d.enveloped->recipientinfo;
  216. xalg = p7->d.enveloped->enc_data->algorithm;
  217. evp_cipher = p7->d.enveloped->enc_data->cipher;
  218. if (evp_cipher == NULL) {
  219. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
  220. goto err;
  221. }
  222. break;
  223. case NID_pkcs7_digest:
  224. xa = p7->d.digest->md;
  225. os = PKCS7_get_octet_string(p7->d.digest->contents);
  226. break;
  227. case NID_pkcs7_data:
  228. break;
  229. default:
  230. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  231. goto err;
  232. }
  233. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
  234. if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
  235. goto err;
  236. if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
  237. goto err;
  238. if (evp_cipher != NULL) {
  239. unsigned char key[EVP_MAX_KEY_LENGTH];
  240. unsigned char iv[EVP_MAX_IV_LENGTH];
  241. int keylen, ivlen;
  242. EVP_CIPHER_CTX *ctx;
  243. if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
  244. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  245. goto err;
  246. }
  247. BIO_get_cipher_ctx(btmp, &ctx);
  248. keylen = EVP_CIPHER_get_key_length(evp_cipher);
  249. ivlen = EVP_CIPHER_get_iv_length(evp_cipher);
  250. xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_get_type(evp_cipher));
  251. if (ivlen > 0)
  252. if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
  253. goto err;
  254. (void)ERR_set_mark();
  255. fetched_cipher = EVP_CIPHER_fetch(libctx,
  256. EVP_CIPHER_get0_name(evp_cipher),
  257. propq);
  258. (void)ERR_pop_to_mark();
  259. if (fetched_cipher != NULL)
  260. cipher = fetched_cipher;
  261. else
  262. cipher = evp_cipher;
  263. if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0)
  264. goto err;
  265. EVP_CIPHER_free(fetched_cipher);
  266. fetched_cipher = NULL;
  267. if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
  268. goto err;
  269. if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
  270. goto err;
  271. if (ivlen > 0) {
  272. if (xalg->parameter == NULL) {
  273. xalg->parameter = ASN1_TYPE_new();
  274. if (xalg->parameter == NULL)
  275. goto err;
  276. }
  277. if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) <= 0) {
  278. ASN1_TYPE_free(xalg->parameter);
  279. xalg->parameter = NULL;
  280. goto err;
  281. }
  282. }
  283. /* Lets do the pub key stuff :-) */
  284. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  285. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  286. if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
  287. goto err;
  288. }
  289. OPENSSL_cleanse(key, keylen);
  290. if (out == NULL)
  291. out = btmp;
  292. else
  293. BIO_push(out, btmp);
  294. btmp = NULL;
  295. }
  296. if (bio == NULL) {
  297. if (PKCS7_is_detached(p7)) {
  298. bio = BIO_new(BIO_s_null());
  299. } else if (os && os->length > 0) {
  300. bio = BIO_new_mem_buf(os->data, os->length);
  301. } else {
  302. bio = BIO_new(BIO_s_mem());
  303. if (bio == NULL)
  304. goto err;
  305. BIO_set_mem_eof_return(bio, 0);
  306. }
  307. if (bio == NULL)
  308. goto err;
  309. }
  310. if (out)
  311. BIO_push(out, bio);
  312. else
  313. out = bio;
  314. return out;
  315. err:
  316. EVP_CIPHER_free(fetched_cipher);
  317. BIO_free_all(out);
  318. BIO_free_all(btmp);
  319. return NULL;
  320. }
  321. static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
  322. {
  323. int ret;
  324. ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
  325. X509_get_issuer_name(pcert));
  326. if (ret)
  327. return ret;
  328. return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert),
  329. ri->issuer_and_serial->serial);
  330. }
  331. /* int */
  332. BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
  333. {
  334. int i, len;
  335. BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
  336. X509_ALGOR *xa;
  337. ASN1_OCTET_STRING *data_body = NULL;
  338. EVP_MD *evp_md = NULL;
  339. const EVP_MD *md;
  340. EVP_CIPHER *evp_cipher = NULL;
  341. const EVP_CIPHER *cipher = NULL;
  342. EVP_CIPHER_CTX *evp_ctx = NULL;
  343. X509_ALGOR *enc_alg = NULL;
  344. STACK_OF(X509_ALGOR) *md_sk = NULL;
  345. STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
  346. PKCS7_RECIP_INFO *ri = NULL;
  347. unsigned char *ek = NULL, *tkey = NULL;
  348. int eklen = 0, tkeylen = 0;
  349. char name[OSSL_MAX_NAME_SIZE];
  350. const PKCS7_CTX *p7_ctx;
  351. OSSL_LIB_CTX *libctx;
  352. const char *propq;
  353. if (p7 == NULL) {
  354. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  355. return NULL;
  356. }
  357. p7_ctx = ossl_pkcs7_get0_ctx(p7);
  358. libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
  359. propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
  360. if (p7->d.ptr == NULL) {
  361. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
  362. return NULL;
  363. }
  364. i = OBJ_obj2nid(p7->type);
  365. p7->state = PKCS7_S_HEADER;
  366. switch (i) {
  367. case NID_pkcs7_signed:
  368. /*
  369. * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
  370. * field and optional content.
  371. * data_body is NULL if that structure has no (=detached) content
  372. * or if the contentType is wrong (i.e., not "data").
  373. */
  374. data_body = PKCS7_get_octet_string(p7->d.sign->contents);
  375. if (!PKCS7_is_detached(p7) && data_body == NULL) {
  376. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_SIGNED_DATA_TYPE);
  377. goto err;
  378. }
  379. md_sk = p7->d.sign->md_algs;
  380. break;
  381. case NID_pkcs7_signedAndEnveloped:
  382. rsk = p7->d.signed_and_enveloped->recipientinfo;
  383. md_sk = p7->d.signed_and_enveloped->md_algs;
  384. /* data_body is NULL if the optional EncryptedContent is missing. */
  385. data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
  386. enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
  387. OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
  388. (void)ERR_set_mark();
  389. evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
  390. if (evp_cipher != NULL)
  391. cipher = evp_cipher;
  392. else
  393. cipher = EVP_get_cipherbyname(name);
  394. if (cipher == NULL) {
  395. (void)ERR_clear_last_mark();
  396. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  397. goto err;
  398. }
  399. (void)ERR_pop_to_mark();
  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. OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
  407. (void)ERR_set_mark();
  408. evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
  409. if (evp_cipher != NULL)
  410. cipher = evp_cipher;
  411. else
  412. cipher = EVP_get_cipherbyname(name);
  413. if (cipher == NULL) {
  414. (void)ERR_clear_last_mark();
  415. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  416. goto err;
  417. }
  418. (void)ERR_pop_to_mark();
  419. break;
  420. default:
  421. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  422. goto err;
  423. }
  424. /* Detached content must be supplied via in_bio instead. */
  425. if (data_body == NULL && in_bio == NULL) {
  426. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
  427. goto err;
  428. }
  429. /* We will be checking the signature */
  430. if (md_sk != NULL) {
  431. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
  432. xa = sk_X509_ALGOR_value(md_sk, i);
  433. if ((btmp = BIO_new(BIO_f_md())) == NULL) {
  434. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  435. goto err;
  436. }
  437. OBJ_obj2txt(name, sizeof(name), xa->algorithm, 0);
  438. (void)ERR_set_mark();
  439. evp_md = EVP_MD_fetch(libctx, name, propq);
  440. if (evp_md != NULL)
  441. md = evp_md;
  442. else
  443. md = EVP_get_digestbyname(name);
  444. if (md == NULL) {
  445. (void)ERR_clear_last_mark();
  446. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
  447. goto err;
  448. }
  449. (void)ERR_pop_to_mark();
  450. if (BIO_set_md(btmp, md) <= 0) {
  451. EVP_MD_free(evp_md);
  452. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  453. goto err;
  454. }
  455. EVP_MD_free(evp_md);
  456. if (out == NULL)
  457. out = btmp;
  458. else
  459. BIO_push(out, btmp);
  460. btmp = NULL;
  461. }
  462. }
  463. if (cipher != NULL) {
  464. if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
  465. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  466. goto err;
  467. }
  468. /*
  469. * It was encrypted, we need to decrypt the secret key with the
  470. * private key
  471. */
  472. /*
  473. * Find the recipientInfo which matches the passed certificate (if
  474. * any)
  475. */
  476. if (pcert) {
  477. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  478. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  479. if (!pkcs7_cmp_ri(ri, pcert))
  480. break;
  481. ri = NULL;
  482. }
  483. if (ri == NULL) {
  484. ERR_raise(ERR_LIB_PKCS7,
  485. PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
  486. goto err;
  487. }
  488. }
  489. /* If we haven't got a certificate try each ri in turn */
  490. if (pcert == NULL) {
  491. /*
  492. * Always attempt to decrypt all rinfo even after success as a
  493. * defence against MMA timing attacks.
  494. */
  495. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  496. ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
  497. ri->ctx = p7_ctx;
  498. if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
  499. EVP_CIPHER_get_key_length(cipher)) < 0)
  500. goto err;
  501. ERR_clear_error();
  502. }
  503. } else {
  504. ri->ctx = p7_ctx;
  505. /* Only exit on fatal errors, not decrypt failure */
  506. if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
  507. goto err;
  508. ERR_clear_error();
  509. }
  510. evp_ctx = NULL;
  511. BIO_get_cipher_ctx(etmp, &evp_ctx);
  512. if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0)
  513. goto err;
  514. if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) <= 0)
  515. goto err;
  516. /* Generate random key as MMA defence */
  517. len = EVP_CIPHER_CTX_get_key_length(evp_ctx);
  518. if (len <= 0)
  519. goto err;
  520. tkeylen = (size_t)len;
  521. tkey = OPENSSL_malloc(tkeylen);
  522. if (tkey == NULL)
  523. goto err;
  524. if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
  525. goto err;
  526. if (ek == NULL) {
  527. ek = tkey;
  528. eklen = tkeylen;
  529. tkey = NULL;
  530. }
  531. if (eklen != EVP_CIPHER_CTX_get_key_length(evp_ctx)) {
  532. /*
  533. * Some S/MIME clients don't use the same key and effective key
  534. * length. The key length is determined by the size of the
  535. * decrypted RSA key.
  536. */
  537. if (EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen) <= 0) {
  538. /* Use random key as MMA defence */
  539. OPENSSL_clear_free(ek, eklen);
  540. ek = tkey;
  541. eklen = tkeylen;
  542. tkey = NULL;
  543. }
  544. }
  545. /* Clear errors so we don't leak information useful in MMA */
  546. ERR_clear_error();
  547. if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
  548. goto err;
  549. OPENSSL_clear_free(ek, eklen);
  550. ek = NULL;
  551. OPENSSL_clear_free(tkey, tkeylen);
  552. tkey = NULL;
  553. if (out == NULL)
  554. out = etmp;
  555. else
  556. BIO_push(out, etmp);
  557. etmp = NULL;
  558. }
  559. if (in_bio != NULL) {
  560. bio = in_bio;
  561. } else {
  562. if (data_body->length > 0)
  563. bio = BIO_new_mem_buf(data_body->data, data_body->length);
  564. else {
  565. bio = BIO_new(BIO_s_mem());
  566. if (bio == NULL)
  567. goto err;
  568. BIO_set_mem_eof_return(bio, 0);
  569. }
  570. if (bio == NULL)
  571. goto err;
  572. }
  573. BIO_push(out, bio);
  574. bio = NULL;
  575. EVP_CIPHER_free(evp_cipher);
  576. return out;
  577. err:
  578. EVP_CIPHER_free(evp_cipher);
  579. OPENSSL_clear_free(ek, eklen);
  580. OPENSSL_clear_free(tkey, tkeylen);
  581. BIO_free_all(out);
  582. BIO_free_all(btmp);
  583. BIO_free_all(etmp);
  584. BIO_free_all(bio);
  585. return NULL;
  586. }
  587. static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
  588. {
  589. for (;;) {
  590. bio = BIO_find_type(bio, BIO_TYPE_MD);
  591. if (bio == NULL) {
  592. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  593. return NULL;
  594. }
  595. BIO_get_md_ctx(bio, pmd);
  596. if (*pmd == NULL) {
  597. ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
  598. return NULL;
  599. }
  600. if (EVP_MD_CTX_get_type(*pmd) == nid)
  601. return bio;
  602. bio = BIO_next(bio);
  603. }
  604. return NULL;
  605. }
  606. static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
  607. {
  608. unsigned char md_data[EVP_MAX_MD_SIZE];
  609. unsigned int md_len;
  610. /* Add signing time if not already present */
  611. if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
  612. if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
  613. ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
  614. return 0;
  615. }
  616. }
  617. /* Add digest */
  618. if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
  619. ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
  620. return 0;
  621. }
  622. if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
  623. ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
  624. return 0;
  625. }
  626. /* Now sign the attributes */
  627. if (!PKCS7_SIGNER_INFO_sign(si))
  628. return 0;
  629. return 1;
  630. }
  631. int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
  632. {
  633. int ret = 0;
  634. int i, j;
  635. BIO *btmp;
  636. PKCS7_SIGNER_INFO *si;
  637. EVP_MD_CTX *mdc, *ctx_tmp;
  638. STACK_OF(X509_ATTRIBUTE) *sk;
  639. STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
  640. ASN1_OCTET_STRING *os = NULL;
  641. const PKCS7_CTX *p7_ctx;
  642. if (p7 == NULL) {
  643. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  644. return 0;
  645. }
  646. p7_ctx = ossl_pkcs7_get0_ctx(p7);
  647. if (p7->d.ptr == NULL) {
  648. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
  649. return 0;
  650. }
  651. ctx_tmp = EVP_MD_CTX_new();
  652. if (ctx_tmp == NULL) {
  653. ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
  654. return 0;
  655. }
  656. i = OBJ_obj2nid(p7->type);
  657. p7->state = PKCS7_S_HEADER;
  658. switch (i) {
  659. case NID_pkcs7_data:
  660. os = p7->d.data;
  661. break;
  662. case NID_pkcs7_signedAndEnveloped:
  663. /* XXXXXXXXXXXXXXXX */
  664. si_sk = p7->d.signed_and_enveloped->signer_info;
  665. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  666. if (os == NULL) {
  667. os = ASN1_OCTET_STRING_new();
  668. if (os == NULL) {
  669. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  670. goto err;
  671. }
  672. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  673. }
  674. break;
  675. case NID_pkcs7_enveloped:
  676. /* XXXXXXXXXXXXXXXX */
  677. os = p7->d.enveloped->enc_data->enc_data;
  678. if (os == NULL) {
  679. os = ASN1_OCTET_STRING_new();
  680. if (os == NULL) {
  681. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  682. goto err;
  683. }
  684. p7->d.enveloped->enc_data->enc_data = os;
  685. }
  686. break;
  687. case NID_pkcs7_signed:
  688. si_sk = p7->d.sign->signer_info;
  689. os = PKCS7_get_octet_string(p7->d.sign->contents);
  690. /* If detached data then the content is excluded */
  691. if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
  692. ASN1_OCTET_STRING_free(os);
  693. os = NULL;
  694. p7->d.sign->contents->d.data = NULL;
  695. }
  696. break;
  697. case NID_pkcs7_digest:
  698. os = PKCS7_get_octet_string(p7->d.digest->contents);
  699. /* If detached data then the content is excluded */
  700. if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
  701. ASN1_OCTET_STRING_free(os);
  702. os = NULL;
  703. p7->d.digest->contents->d.data = NULL;
  704. }
  705. break;
  706. default:
  707. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  708. goto err;
  709. }
  710. if (si_sk != NULL) {
  711. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
  712. si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
  713. if (si->pkey == NULL)
  714. continue;
  715. j = OBJ_obj2nid(si->digest_alg->algorithm);
  716. btmp = bio;
  717. btmp = PKCS7_find_digest(&mdc, btmp, j);
  718. if (btmp == NULL)
  719. goto err;
  720. /*
  721. * We now have the EVP_MD_CTX, lets do the signing.
  722. */
  723. if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
  724. goto err;
  725. sk = si->auth_attr;
  726. /*
  727. * If there are attributes, we add the digest attribute and only
  728. * sign the attributes
  729. */
  730. if (sk_X509_ATTRIBUTE_num(sk) > 0) {
  731. if (!do_pkcs7_signed_attrib(si, ctx_tmp))
  732. goto err;
  733. } else {
  734. unsigned char *abuf = NULL;
  735. unsigned int abuflen = EVP_PKEY_get_size(si->pkey);
  736. if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL)
  737. goto err;
  738. if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
  739. ossl_pkcs7_ctx_get0_libctx(p7_ctx),
  740. ossl_pkcs7_ctx_get0_propq(p7_ctx))) {
  741. OPENSSL_free(abuf);
  742. ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
  743. goto err;
  744. }
  745. ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
  746. }
  747. }
  748. } else if (i == NID_pkcs7_digest) {
  749. unsigned char md_data[EVP_MAX_MD_SIZE];
  750. unsigned int md_len;
  751. if (!PKCS7_find_digest(&mdc, bio,
  752. OBJ_obj2nid(p7->d.digest->md->algorithm)))
  753. goto err;
  754. if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
  755. goto err;
  756. if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
  757. goto err;
  758. }
  759. if (!PKCS7_is_detached(p7)) {
  760. /*
  761. * NOTE(emilia): I think we only reach os == NULL here because detached
  762. * digested data support is broken.
  763. */
  764. if (os == NULL)
  765. goto err;
  766. if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
  767. char *cont;
  768. long contlen;
  769. btmp = BIO_find_type(bio, BIO_TYPE_MEM);
  770. if (btmp == NULL) {
  771. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
  772. goto err;
  773. }
  774. contlen = BIO_get_mem_data(btmp, &cont);
  775. /*
  776. * Mark the BIO read only then we can use its copy of the data
  777. * instead of making an extra copy.
  778. */
  779. BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
  780. BIO_set_mem_eof_return(btmp, 0);
  781. ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
  782. }
  783. }
  784. ret = 1;
  785. err:
  786. EVP_MD_CTX_free(ctx_tmp);
  787. return ret;
  788. }
  789. int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
  790. {
  791. EVP_MD_CTX *mctx;
  792. EVP_PKEY_CTX *pctx = NULL;
  793. unsigned char *abuf = NULL;
  794. int alen;
  795. size_t siglen;
  796. const EVP_MD *md = NULL;
  797. const PKCS7_CTX *ctx = si->ctx;
  798. md = EVP_get_digestbyobj(si->digest_alg->algorithm);
  799. if (md == NULL)
  800. return 0;
  801. mctx = EVP_MD_CTX_new();
  802. if (mctx == NULL) {
  803. ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
  804. goto err;
  805. }
  806. if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md),
  807. ossl_pkcs7_ctx_get0_libctx(ctx),
  808. ossl_pkcs7_ctx_get0_propq(ctx), si->pkey,
  809. NULL) <= 0)
  810. goto err;
  811. alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
  812. ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
  813. if (!abuf)
  814. goto err;
  815. if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
  816. goto err;
  817. OPENSSL_free(abuf);
  818. abuf = NULL;
  819. if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
  820. goto err;
  821. abuf = OPENSSL_malloc(siglen);
  822. if (abuf == NULL)
  823. goto err;
  824. if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
  825. goto err;
  826. EVP_MD_CTX_free(mctx);
  827. ASN1_STRING_set0(si->enc_digest, abuf, siglen);
  828. return 1;
  829. err:
  830. OPENSSL_free(abuf);
  831. EVP_MD_CTX_free(mctx);
  832. return 0;
  833. }
  834. /* This partly overlaps with PKCS7_verify(). It does not support flags. */
  835. int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
  836. PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  837. {
  838. PKCS7_ISSUER_AND_SERIAL *ias;
  839. int ret = 0, i;
  840. STACK_OF(X509) *untrusted;
  841. STACK_OF(X509_CRL) *crls;
  842. X509 *signer;
  843. if (p7 == NULL) {
  844. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  845. return 0;
  846. }
  847. if (p7->d.ptr == NULL) {
  848. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
  849. return 0;
  850. }
  851. if (PKCS7_type_is_signed(p7)) {
  852. untrusted = p7->d.sign->cert;
  853. crls = p7->d.sign->crl;
  854. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  855. untrusted = p7->d.signed_and_enveloped->cert;
  856. crls = p7->d.signed_and_enveloped->crl;
  857. } else {
  858. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
  859. goto err;
  860. }
  861. X509_STORE_CTX_set0_crls(ctx, crls);
  862. /* XXXXXXXXXXXXXXXXXXXXXXX */
  863. ias = si->issuer_and_serial;
  864. signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial);
  865. /* Were we able to find the signer certificate in passed to us? */
  866. if (signer == NULL) {
  867. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
  868. goto err;
  869. }
  870. /* Lets verify */
  871. if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) {
  872. ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
  873. goto err;
  874. }
  875. X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
  876. i = X509_verify_cert(ctx);
  877. if (i <= 0) {
  878. ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
  879. goto err;
  880. }
  881. return PKCS7_signatureVerify(bio, p7, si, signer);
  882. err:
  883. return ret;
  884. }
  885. int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
  886. X509 *signer)
  887. {
  888. ASN1_OCTET_STRING *os;
  889. EVP_MD_CTX *mdc_tmp, *mdc;
  890. const EVP_MD *md;
  891. EVP_MD *fetched_md = NULL;
  892. int ret = 0, i;
  893. int md_type;
  894. STACK_OF(X509_ATTRIBUTE) *sk;
  895. BIO *btmp;
  896. EVP_PKEY *pkey;
  897. unsigned char *abuf = NULL;
  898. const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
  899. OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
  900. const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
  901. mdc_tmp = EVP_MD_CTX_new();
  902. if (mdc_tmp == NULL) {
  903. ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
  904. goto err;
  905. }
  906. if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
  907. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
  908. goto err;
  909. }
  910. md_type = OBJ_obj2nid(si->digest_alg->algorithm);
  911. btmp = bio;
  912. for (;;) {
  913. if ((btmp == NULL) ||
  914. ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
  915. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  916. goto err;
  917. }
  918. BIO_get_md_ctx(btmp, &mdc);
  919. if (mdc == NULL) {
  920. ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
  921. goto err;
  922. }
  923. if (EVP_MD_CTX_get_type(mdc) == md_type)
  924. break;
  925. /*
  926. * Workaround for some broken clients that put the signature OID
  927. * instead of the digest OID in digest_alg->algorithm
  928. */
  929. if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
  930. break;
  931. btmp = BIO_next(btmp);
  932. }
  933. /*
  934. * mdc is the digest ctx that we want, unless there are attributes, in
  935. * which case the digest is the signed attributes
  936. */
  937. if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
  938. goto err;
  939. sk = si->auth_attr;
  940. if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
  941. unsigned char md_dat[EVP_MAX_MD_SIZE];
  942. unsigned int md_len;
  943. int alen;
  944. ASN1_OCTET_STRING *message_digest;
  945. if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
  946. goto err;
  947. message_digest = PKCS7_digest_from_attributes(sk);
  948. if (!message_digest) {
  949. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  950. goto err;
  951. }
  952. if ((message_digest->length != (int)md_len) ||
  953. (memcmp(message_digest->data, md_dat, md_len))) {
  954. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
  955. ret = -1;
  956. goto err;
  957. }
  958. (void)ERR_set_mark();
  959. fetched_md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
  960. if (fetched_md != NULL)
  961. md = fetched_md;
  962. else
  963. md = EVP_get_digestbynid(md_type);
  964. if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
  965. (void)ERR_clear_last_mark();
  966. goto err;
  967. }
  968. (void)ERR_pop_to_mark();
  969. alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
  970. ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
  971. if (alen <= 0) {
  972. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  973. ret = -1;
  974. goto err;
  975. }
  976. if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
  977. goto err;
  978. }
  979. os = si->enc_digest;
  980. pkey = X509_get0_pubkey(signer);
  981. if (pkey == NULL) {
  982. ret = -1;
  983. goto err;
  984. }
  985. i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
  986. if (i <= 0) {
  987. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
  988. ret = -1;
  989. goto err;
  990. }
  991. ret = 1;
  992. err:
  993. OPENSSL_free(abuf);
  994. EVP_MD_CTX_free(mdc_tmp);
  995. EVP_MD_free(fetched_md);
  996. return ret;
  997. }
  998. PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
  999. {
  1000. STACK_OF(PKCS7_RECIP_INFO) *rsk;
  1001. PKCS7_RECIP_INFO *ri;
  1002. int i;
  1003. i = OBJ_obj2nid(p7->type);
  1004. if (i != NID_pkcs7_signedAndEnveloped)
  1005. return NULL;
  1006. if (p7->d.signed_and_enveloped == NULL)
  1007. return NULL;
  1008. rsk = p7->d.signed_and_enveloped->recipientinfo;
  1009. if (rsk == NULL)
  1010. return NULL;
  1011. if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
  1012. return NULL;
  1013. ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
  1014. return ri->issuer_and_serial;
  1015. }
  1016. ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
  1017. {
  1018. return get_attribute(si->auth_attr, nid);
  1019. }
  1020. ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
  1021. {
  1022. return get_attribute(si->unauth_attr, nid);
  1023. }
  1024. static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
  1025. {
  1026. int idx = X509at_get_attr_by_NID(sk, nid, -1);
  1027. if (idx < 0)
  1028. return NULL;
  1029. return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
  1030. }
  1031. ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
  1032. {
  1033. ASN1_TYPE *astype;
  1034. if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
  1035. return NULL;
  1036. return astype->value.octet_string;
  1037. }
  1038. int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
  1039. STACK_OF(X509_ATTRIBUTE) *sk)
  1040. {
  1041. int i;
  1042. sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
  1043. p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
  1044. if (p7si->auth_attr == NULL)
  1045. return 0;
  1046. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1047. if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
  1048. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  1049. (sk, i))))
  1050. == NULL)
  1051. return 0;
  1052. }
  1053. return 1;
  1054. }
  1055. int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
  1056. STACK_OF(X509_ATTRIBUTE) *sk)
  1057. {
  1058. int i;
  1059. sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
  1060. p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
  1061. if (p7si->unauth_attr == NULL)
  1062. return 0;
  1063. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1064. if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
  1065. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  1066. (sk, i))))
  1067. == NULL)
  1068. return 0;
  1069. }
  1070. return 1;
  1071. }
  1072. int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1073. void *value)
  1074. {
  1075. return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
  1076. }
  1077. int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1078. void *value)
  1079. {
  1080. return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
  1081. }
  1082. static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
  1083. void *value)
  1084. {
  1085. X509_ATTRIBUTE *attr = NULL;
  1086. int i, n;
  1087. if (*sk == NULL) {
  1088. if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
  1089. return 0;
  1090. }
  1091. n = sk_X509_ATTRIBUTE_num(*sk);
  1092. for (i = 0; i < n; i++) {
  1093. attr = sk_X509_ATTRIBUTE_value(*sk, i);
  1094. if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid)
  1095. goto end;
  1096. }
  1097. if (!sk_X509_ATTRIBUTE_push(*sk, NULL))
  1098. return 0;
  1099. end:
  1100. attr = X509_ATTRIBUTE_create(nid, atrtype, value);
  1101. if (attr == NULL) {
  1102. if (i == n)
  1103. sk_X509_ATTRIBUTE_pop(*sk);
  1104. return 0;
  1105. }
  1106. X509_ATTRIBUTE_free(sk_X509_ATTRIBUTE_value(*sk, i));
  1107. (void) sk_X509_ATTRIBUTE_set(*sk, i, attr);
  1108. return 1;
  1109. }