pk7_doit.c 38 KB

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