pk7_doit.c 38 KB

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