pk7_doit.c 39 KB

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