pk7_doit.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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_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. if (bio == NULL)
  571. goto err;
  572. /*
  573. * We need to set this so that when we have read all the data, the
  574. * encrypt BIO, if present, will read EOF and encode the last few
  575. * bytes
  576. */
  577. BIO_set_mem_eof_return(bio, 0);
  578. if (data_body->length > 0)
  579. BIO_write(bio, (char *)data_body->data, data_body->length);
  580. # else
  581. if (data_body->length > 0)
  582. bio = BIO_new_mem_buf(data_body->data, data_body->length);
  583. else {
  584. bio = BIO_new(BIO_s_mem());
  585. if (bio == NULL)
  586. goto err;
  587. BIO_set_mem_eof_return(bio, 0);
  588. }
  589. if (bio == NULL)
  590. goto err;
  591. # endif
  592. }
  593. BIO_push(out, bio);
  594. bio = NULL;
  595. #endif
  596. if (0) {
  597. err:
  598. if (ek) {
  599. OPENSSL_cleanse(ek, eklen);
  600. OPENSSL_free(ek);
  601. }
  602. if (tkey) {
  603. OPENSSL_cleanse(tkey, tkeylen);
  604. OPENSSL_free(tkey);
  605. }
  606. if (out != NULL)
  607. BIO_free_all(out);
  608. if (btmp != NULL)
  609. BIO_free_all(btmp);
  610. if (etmp != NULL)
  611. BIO_free_all(etmp);
  612. if (bio != NULL)
  613. BIO_free_all(bio);
  614. out = NULL;
  615. }
  616. return (out);
  617. }
  618. static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
  619. {
  620. for (;;) {
  621. bio = BIO_find_type(bio, BIO_TYPE_MD);
  622. if (bio == NULL) {
  623. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
  624. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  625. return NULL;
  626. }
  627. BIO_get_md_ctx(bio, pmd);
  628. if (*pmd == NULL) {
  629. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR);
  630. return NULL;
  631. }
  632. if (EVP_MD_CTX_type(*pmd) == nid)
  633. return bio;
  634. bio = BIO_next(bio);
  635. }
  636. return NULL;
  637. }
  638. static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
  639. {
  640. unsigned char md_data[EVP_MAX_MD_SIZE];
  641. unsigned int md_len;
  642. /* Add signing time if not already present */
  643. if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
  644. if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
  645. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
  646. return 0;
  647. }
  648. }
  649. /* Add digest */
  650. if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
  651. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
  652. return 0;
  653. }
  654. if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
  655. PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
  656. return 0;
  657. }
  658. /* Now sign the attributes */
  659. if (!PKCS7_SIGNER_INFO_sign(si))
  660. return 0;
  661. return 1;
  662. }
  663. int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
  664. {
  665. int ret = 0;
  666. int i, j;
  667. BIO *btmp;
  668. PKCS7_SIGNER_INFO *si;
  669. EVP_MD_CTX *mdc, ctx_tmp;
  670. STACK_OF(X509_ATTRIBUTE) *sk;
  671. STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
  672. ASN1_OCTET_STRING *os = NULL;
  673. if (p7 == NULL) {
  674. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER);
  675. return 0;
  676. }
  677. if (p7->d.ptr == NULL) {
  678. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
  679. return 0;
  680. }
  681. EVP_MD_CTX_init(&ctx_tmp);
  682. i = OBJ_obj2nid(p7->type);
  683. p7->state = PKCS7_S_HEADER;
  684. switch (i) {
  685. case NID_pkcs7_data:
  686. os = p7->d.data;
  687. break;
  688. case NID_pkcs7_signedAndEnveloped:
  689. /* XXXXXXXXXXXXXXXX */
  690. si_sk = p7->d.signed_and_enveloped->signer_info;
  691. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  692. if (!os) {
  693. os = M_ASN1_OCTET_STRING_new();
  694. if (!os) {
  695. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
  696. goto err;
  697. }
  698. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  699. }
  700. break;
  701. case NID_pkcs7_enveloped:
  702. /* XXXXXXXXXXXXXXXX */
  703. os = p7->d.enveloped->enc_data->enc_data;
  704. if (!os) {
  705. os = M_ASN1_OCTET_STRING_new();
  706. if (!os) {
  707. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
  708. goto err;
  709. }
  710. p7->d.enveloped->enc_data->enc_data = os;
  711. }
  712. break;
  713. case NID_pkcs7_signed:
  714. si_sk = p7->d.sign->signer_info;
  715. os = PKCS7_get_octet_string(p7->d.sign->contents);
  716. /* If detached data then the content is excluded */
  717. if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
  718. M_ASN1_OCTET_STRING_free(os);
  719. os = NULL;
  720. p7->d.sign->contents->d.data = NULL;
  721. }
  722. break;
  723. case NID_pkcs7_digest:
  724. os = PKCS7_get_octet_string(p7->d.digest->contents);
  725. /* If detached data then the content is excluded */
  726. if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
  727. M_ASN1_OCTET_STRING_free(os);
  728. os = NULL;
  729. p7->d.digest->contents->d.data = NULL;
  730. }
  731. break;
  732. default:
  733. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  734. goto err;
  735. }
  736. if (si_sk != NULL) {
  737. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
  738. si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
  739. if (si->pkey == NULL)
  740. continue;
  741. j = OBJ_obj2nid(si->digest_alg->algorithm);
  742. btmp = bio;
  743. btmp = PKCS7_find_digest(&mdc, btmp, j);
  744. if (btmp == NULL)
  745. goto err;
  746. /*
  747. * We now have the EVP_MD_CTX, lets do the signing.
  748. */
  749. if (!EVP_MD_CTX_copy_ex(&ctx_tmp, mdc))
  750. goto err;
  751. sk = si->auth_attr;
  752. /*
  753. * If there are attributes, we add the digest attribute and only
  754. * sign the attributes
  755. */
  756. if (sk_X509_ATTRIBUTE_num(sk) > 0) {
  757. if (!do_pkcs7_signed_attrib(si, &ctx_tmp))
  758. goto err;
  759. } else {
  760. unsigned char *abuf = NULL;
  761. unsigned int abuflen;
  762. abuflen = EVP_PKEY_size(si->pkey);
  763. abuf = OPENSSL_malloc(abuflen);
  764. if (!abuf)
  765. goto err;
  766. if (!EVP_SignFinal(&ctx_tmp, abuf, &abuflen, si->pkey)) {
  767. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB);
  768. goto err;
  769. }
  770. ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
  771. }
  772. }
  773. } else if (i == NID_pkcs7_digest) {
  774. unsigned char md_data[EVP_MAX_MD_SIZE];
  775. unsigned int md_len;
  776. if (!PKCS7_find_digest(&mdc, bio,
  777. OBJ_obj2nid(p7->d.digest->md->algorithm)))
  778. goto err;
  779. if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
  780. goto err;
  781. M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len);
  782. }
  783. if (!PKCS7_is_detached(p7)) {
  784. /*
  785. * NOTE(emilia): I think we only reach os == NULL here because detached
  786. * digested data support is broken.
  787. */
  788. if (os == NULL)
  789. goto err;
  790. if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
  791. char *cont;
  792. long contlen;
  793. btmp = BIO_find_type(bio, BIO_TYPE_MEM);
  794. if (btmp == NULL) {
  795. PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
  796. goto err;
  797. }
  798. contlen = BIO_get_mem_data(btmp, &cont);
  799. /*
  800. * Mark the BIO read only then we can use its copy of the data
  801. * instead of making an extra copy.
  802. */
  803. BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
  804. BIO_set_mem_eof_return(btmp, 0);
  805. ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
  806. }
  807. }
  808. ret = 1;
  809. err:
  810. EVP_MD_CTX_cleanup(&ctx_tmp);
  811. return (ret);
  812. }
  813. int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
  814. {
  815. EVP_MD_CTX mctx;
  816. EVP_PKEY_CTX *pctx;
  817. unsigned char *abuf = NULL;
  818. int alen;
  819. size_t siglen;
  820. const EVP_MD *md = NULL;
  821. md = EVP_get_digestbyobj(si->digest_alg->algorithm);
  822. if (md == NULL)
  823. return 0;
  824. EVP_MD_CTX_init(&mctx);
  825. if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
  826. goto err;
  827. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  828. EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
  829. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
  830. goto err;
  831. }
  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)
  844. goto err;
  845. if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0)
  846. goto err;
  847. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  848. EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
  849. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
  850. goto err;
  851. }
  852. EVP_MD_CTX_cleanup(&mctx);
  853. ASN1_STRING_set0(si->enc_digest, abuf, siglen);
  854. return 1;
  855. err:
  856. if (abuf)
  857. OPENSSL_free(abuf);
  858. EVP_MD_CTX_cleanup(&mctx);
  859. return 0;
  860. }
  861. int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
  862. PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  863. {
  864. PKCS7_ISSUER_AND_SERIAL *ias;
  865. int ret = 0, i;
  866. STACK_OF(X509) *cert;
  867. X509 *x509;
  868. if (p7 == NULL) {
  869. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER);
  870. return 0;
  871. }
  872. if (p7->d.ptr == NULL) {
  873. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
  874. return 0;
  875. }
  876. if (PKCS7_type_is_signed(p7)) {
  877. cert = p7->d.sign->cert;
  878. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  879. cert = p7->d.signed_and_enveloped->cert;
  880. } else {
  881. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
  882. goto err;
  883. }
  884. /* XXXXXXXXXXXXXXXXXXXXXXX */
  885. ias = si->issuer_and_serial;
  886. x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
  887. /* were we able to find the cert in passed to us */
  888. if (x509 == NULL) {
  889. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
  890. PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
  891. goto err;
  892. }
  893. /* Lets verify */
  894. if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
  895. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
  896. goto err;
  897. }
  898. X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
  899. i = X509_verify_cert(ctx);
  900. if (i <= 0) {
  901. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
  902. X509_STORE_CTX_cleanup(ctx);
  903. goto err;
  904. }
  905. X509_STORE_CTX_cleanup(ctx);
  906. return PKCS7_signatureVerify(bio, p7, si, x509);
  907. err:
  908. return ret;
  909. }
  910. int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
  911. X509 *x509)
  912. {
  913. ASN1_OCTET_STRING *os;
  914. EVP_MD_CTX mdc_tmp, *mdc;
  915. int ret = 0, i;
  916. int md_type;
  917. STACK_OF(X509_ATTRIBUTE) *sk;
  918. BIO *btmp;
  919. EVP_PKEY *pkey;
  920. EVP_MD_CTX_init(&mdc_tmp);
  921. if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
  922. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
  923. goto err;
  924. }
  925. md_type = OBJ_obj2nid(si->digest_alg->algorithm);
  926. btmp = bio;
  927. for (;;) {
  928. if ((btmp == NULL) ||
  929. ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
  930. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  931. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  932. goto err;
  933. }
  934. BIO_get_md_ctx(btmp, &mdc);
  935. if (mdc == NULL) {
  936. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR);
  937. goto err;
  938. }
  939. if (EVP_MD_CTX_type(mdc) == md_type)
  940. break;
  941. /*
  942. * Workaround for some broken clients that put the signature OID
  943. * instead of the digest OID in digest_alg->algorithm
  944. */
  945. if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
  946. break;
  947. btmp = BIO_next(btmp);
  948. }
  949. /*
  950. * mdc is the digest ctx that we want, unless there are attributes, in
  951. * which case the digest is the signed attributes
  952. */
  953. if (!EVP_MD_CTX_copy_ex(&mdc_tmp, mdc))
  954. goto err;
  955. sk = si->auth_attr;
  956. if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
  957. unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
  958. unsigned int md_len;
  959. int alen;
  960. ASN1_OCTET_STRING *message_digest;
  961. if (!EVP_DigestFinal_ex(&mdc_tmp, md_dat, &md_len))
  962. goto err;
  963. message_digest = PKCS7_digest_from_attributes(sk);
  964. if (!message_digest) {
  965. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  966. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  967. goto err;
  968. }
  969. if ((message_digest->length != (int)md_len) ||
  970. (memcmp(message_digest->data, md_dat, md_len))) {
  971. #if 0
  972. {
  973. int ii;
  974. for (ii = 0; ii < message_digest->length; ii++)
  975. printf("%02X", message_digest->data[ii]);
  976. printf(" sent\n");
  977. for (ii = 0; ii < md_len; ii++)
  978. printf("%02X", md_dat[ii]);
  979. printf(" calc\n");
  980. }
  981. #endif
  982. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE);
  983. ret = -1;
  984. goto err;
  985. }
  986. if (!EVP_VerifyInit_ex(&mdc_tmp, EVP_get_digestbynid(md_type), NULL))
  987. goto err;
  988. alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
  989. ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
  990. if (alen <= 0) {
  991. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB);
  992. ret = -1;
  993. goto err;
  994. }
  995. if (!EVP_VerifyUpdate(&mdc_tmp, abuf, alen))
  996. goto err;
  997. OPENSSL_free(abuf);
  998. }
  999. os = si->enc_digest;
  1000. pkey = X509_get_pubkey(x509);
  1001. if (!pkey) {
  1002. ret = -1;
  1003. goto err;
  1004. }
  1005. i = EVP_VerifyFinal(&mdc_tmp, os->data, os->length, pkey);
  1006. EVP_PKEY_free(pkey);
  1007. if (i <= 0) {
  1008. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE);
  1009. ret = -1;
  1010. goto err;
  1011. } else
  1012. ret = 1;
  1013. err:
  1014. EVP_MD_CTX_cleanup(&mdc_tmp);
  1015. return (ret);
  1016. }
  1017. PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
  1018. {
  1019. STACK_OF(PKCS7_RECIP_INFO) *rsk;
  1020. PKCS7_RECIP_INFO *ri;
  1021. int i;
  1022. i = OBJ_obj2nid(p7->type);
  1023. if (i != NID_pkcs7_signedAndEnveloped)
  1024. return NULL;
  1025. if (p7->d.signed_and_enveloped == NULL)
  1026. return NULL;
  1027. rsk = p7->d.signed_and_enveloped->recipientinfo;
  1028. if (rsk == NULL)
  1029. return NULL;
  1030. if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
  1031. return (NULL);
  1032. ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
  1033. return (ri->issuer_and_serial);
  1034. }
  1035. ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
  1036. {
  1037. return (get_attribute(si->auth_attr, nid));
  1038. }
  1039. ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
  1040. {
  1041. return (get_attribute(si->unauth_attr, nid));
  1042. }
  1043. static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
  1044. {
  1045. int i;
  1046. X509_ATTRIBUTE *xa;
  1047. ASN1_OBJECT *o;
  1048. o = OBJ_nid2obj(nid);
  1049. if (!o || !sk)
  1050. return (NULL);
  1051. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1052. xa = sk_X509_ATTRIBUTE_value(sk, i);
  1053. if (OBJ_cmp(xa->object, o) == 0) {
  1054. if (!xa->single && sk_ASN1_TYPE_num(xa->value.set))
  1055. return (sk_ASN1_TYPE_value(xa->value.set, 0));
  1056. else
  1057. return (NULL);
  1058. }
  1059. }
  1060. return (NULL);
  1061. }
  1062. ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
  1063. {
  1064. ASN1_TYPE *astype;
  1065. if (!(astype = get_attribute(sk, NID_pkcs9_messageDigest)))
  1066. return NULL;
  1067. return astype->value.octet_string;
  1068. }
  1069. int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
  1070. STACK_OF(X509_ATTRIBUTE) *sk)
  1071. {
  1072. int i;
  1073. if (p7si->auth_attr != NULL)
  1074. sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
  1075. p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
  1076. if (p7si->auth_attr == NULL)
  1077. return 0;
  1078. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1079. if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
  1080. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  1081. (sk, i))))
  1082. == NULL)
  1083. return (0);
  1084. }
  1085. return (1);
  1086. }
  1087. int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
  1088. STACK_OF(X509_ATTRIBUTE) *sk)
  1089. {
  1090. int i;
  1091. if (p7si->unauth_attr != NULL)
  1092. sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
  1093. p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
  1094. if (p7si->unauth_attr == NULL)
  1095. return 0;
  1096. for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
  1097. if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
  1098. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
  1099. (sk, i))))
  1100. == NULL)
  1101. return (0);
  1102. }
  1103. return (1);
  1104. }
  1105. int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1106. void *value)
  1107. {
  1108. return (add_attribute(&(p7si->auth_attr), nid, atrtype, value));
  1109. }
  1110. int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1111. void *value)
  1112. {
  1113. return (add_attribute(&(p7si->unauth_attr), nid, atrtype, value));
  1114. }
  1115. static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
  1116. void *value)
  1117. {
  1118. X509_ATTRIBUTE *attr = NULL;
  1119. if (*sk == NULL) {
  1120. *sk = sk_X509_ATTRIBUTE_new_null();
  1121. if (*sk == NULL)
  1122. return 0;
  1123. new_attrib:
  1124. if (!(attr = X509_ATTRIBUTE_create(nid, atrtype, value)))
  1125. return 0;
  1126. if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
  1127. X509_ATTRIBUTE_free(attr);
  1128. return 0;
  1129. }
  1130. } else {
  1131. int i;
  1132. for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
  1133. attr = sk_X509_ATTRIBUTE_value(*sk, i);
  1134. if (OBJ_obj2nid(attr->object) == nid) {
  1135. X509_ATTRIBUTE_free(attr);
  1136. attr = X509_ATTRIBUTE_create(nid, atrtype, value);
  1137. if (attr == NULL)
  1138. return 0;
  1139. if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
  1140. X509_ATTRIBUTE_free(attr);
  1141. return 0;
  1142. }
  1143. goto end;
  1144. }
  1145. }
  1146. goto new_attrib;
  1147. }
  1148. end:
  1149. return (1);
  1150. }