pk7_doit.c 38 KB

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