pk7_doit.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. {
  74. case NID_pkcs7_data:
  75. case NID_pkcs7_signed:
  76. case NID_pkcs7_enveloped:
  77. case NID_pkcs7_signedAndEnveloped:
  78. case NID_pkcs7_digest:
  79. case NID_pkcs7_encrypted:
  80. isOther=0;
  81. break;
  82. default:
  83. isOther=1;
  84. }
  85. return isOther;
  86. }
  87. static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
  88. {
  89. if ( PKCS7_type_is_data(p7))
  90. return p7->d.data;
  91. if ( PKCS7_type_is_other(p7) && p7->d.other
  92. && (p7->d.other->type == V_ASN1_OCTET_STRING))
  93. return p7->d.other->value.octet_string;
  94. return NULL;
  95. }
  96. static int PKCS7_bio_add_digest(BIO **pbio, X509_ALGOR *alg)
  97. {
  98. BIO *btmp;
  99. const EVP_MD *md;
  100. if ((btmp=BIO_new(BIO_f_md())) == NULL)
  101. {
  102. PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST,ERR_R_BIO_LIB);
  103. goto err;
  104. }
  105. md=EVP_get_digestbyobj(alg->algorithm);
  106. if (md == NULL)
  107. {
  108. PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST,PKCS7_R_UNKNOWN_DIGEST_TYPE);
  109. goto err;
  110. }
  111. BIO_set_md(btmp,md);
  112. if (*pbio == NULL)
  113. *pbio=btmp;
  114. else if (!BIO_push(*pbio,btmp))
  115. {
  116. PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST,ERR_R_BIO_LIB);
  117. goto err;
  118. }
  119. btmp=NULL;
  120. return 1;
  121. err:
  122. if (btmp)
  123. BIO_free(btmp);
  124. return 0;
  125. }
  126. BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
  127. {
  128. int i;
  129. BIO *out=NULL,*btmp=NULL;
  130. X509_ALGOR *xa = NULL;
  131. const EVP_CIPHER *evp_cipher=NULL;
  132. STACK_OF(X509_ALGOR) *md_sk=NULL;
  133. STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL;
  134. X509_ALGOR *xalg=NULL;
  135. PKCS7_RECIP_INFO *ri=NULL;
  136. EVP_PKEY *pkey;
  137. ASN1_OCTET_STRING *os=NULL;
  138. i=OBJ_obj2nid(p7->type);
  139. p7->state=PKCS7_S_HEADER;
  140. switch (i)
  141. {
  142. case NID_pkcs7_signed:
  143. md_sk=p7->d.sign->md_algs;
  144. os = PKCS7_get_octet_string(p7->d.sign->contents);
  145. break;
  146. case NID_pkcs7_signedAndEnveloped:
  147. rsk=p7->d.signed_and_enveloped->recipientinfo;
  148. md_sk=p7->d.signed_and_enveloped->md_algs;
  149. xalg=p7->d.signed_and_enveloped->enc_data->algorithm;
  150. evp_cipher=p7->d.signed_and_enveloped->enc_data->cipher;
  151. if (evp_cipher == NULL)
  152. {
  153. PKCS7err(PKCS7_F_PKCS7_DATAINIT,
  154. PKCS7_R_CIPHER_NOT_INITIALIZED);
  155. goto err;
  156. }
  157. break;
  158. case NID_pkcs7_enveloped:
  159. rsk=p7->d.enveloped->recipientinfo;
  160. xalg=p7->d.enveloped->enc_data->algorithm;
  161. evp_cipher=p7->d.enveloped->enc_data->cipher;
  162. if (evp_cipher == NULL)
  163. {
  164. PKCS7err(PKCS7_F_PKCS7_DATAINIT,
  165. PKCS7_R_CIPHER_NOT_INITIALIZED);
  166. goto err;
  167. }
  168. break;
  169. case NID_pkcs7_digest:
  170. xa = p7->d.digest->md;
  171. os = PKCS7_get_octet_string(p7->d.digest->contents);
  172. break;
  173. default:
  174. PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  175. goto err;
  176. }
  177. for (i=0; i<sk_X509_ALGOR_num(md_sk); i++)
  178. if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i)))
  179. goto err;
  180. if (xa && !PKCS7_bio_add_digest(&out, xa))
  181. goto err;
  182. if (evp_cipher != NULL)
  183. {
  184. unsigned char key[EVP_MAX_KEY_LENGTH];
  185. unsigned char iv[EVP_MAX_IV_LENGTH];
  186. int keylen,ivlen;
  187. int jj,max;
  188. unsigned char *tmp;
  189. EVP_CIPHER_CTX *ctx;
  190. if ((btmp=BIO_new(BIO_f_cipher())) == NULL)
  191. {
  192. PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB);
  193. goto err;
  194. }
  195. BIO_get_cipher_ctx(btmp, &ctx);
  196. keylen=EVP_CIPHER_key_length(evp_cipher);
  197. ivlen=EVP_CIPHER_iv_length(evp_cipher);
  198. xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
  199. if (ivlen > 0)
  200. if (RAND_pseudo_bytes(iv,ivlen) <= 0)
  201. goto err;
  202. if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1)<=0)
  203. goto err;
  204. if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
  205. goto err;
  206. if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
  207. goto err;
  208. if (ivlen > 0) {
  209. if (xalg->parameter == NULL) {
  210. xalg->parameter = ASN1_TYPE_new();
  211. if (xalg->parameter == NULL)
  212. goto err;
  213. }
  214. if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
  215. goto err;
  216. }
  217. /* Lets do the pub key stuff :-) */
  218. max=0;
  219. for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++)
  220. {
  221. ri=sk_PKCS7_RECIP_INFO_value(rsk,i);
  222. if (ri->cert == NULL)
  223. {
  224. PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_MISSING_CERIPEND_INFO);
  225. goto err;
  226. }
  227. if ((pkey=X509_get_pubkey(ri->cert)) == NULL)
  228. goto err;
  229. jj=EVP_PKEY_size(pkey);
  230. EVP_PKEY_free(pkey);
  231. if (max < jj) max=jj;
  232. }
  233. if ((tmp=(unsigned char *)OPENSSL_malloc(max)) == NULL)
  234. {
  235. PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_MALLOC_FAILURE);
  236. goto err;
  237. }
  238. for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++)
  239. {
  240. ri=sk_PKCS7_RECIP_INFO_value(rsk,i);
  241. if ((pkey=X509_get_pubkey(ri->cert)) == NULL)
  242. goto err;
  243. jj=EVP_PKEY_encrypt(tmp,key,keylen,pkey);
  244. EVP_PKEY_free(pkey);
  245. if (jj <= 0)
  246. {
  247. PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_EVP_LIB);
  248. OPENSSL_free(tmp);
  249. goto err;
  250. }
  251. if (!M_ASN1_OCTET_STRING_set(ri->enc_key,tmp,jj))
  252. {
  253. PKCS7err(PKCS7_F_PKCS7_DATAINIT,
  254. ERR_R_MALLOC_FAILURE);
  255. OPENSSL_free(tmp);
  256. goto err;
  257. }
  258. }
  259. OPENSSL_free(tmp);
  260. OPENSSL_cleanse(key, keylen);
  261. if (out == NULL)
  262. out=btmp;
  263. else
  264. BIO_push(out,btmp);
  265. btmp=NULL;
  266. }
  267. if (bio == NULL)
  268. {
  269. if (PKCS7_is_detached(p7))
  270. bio=BIO_new(BIO_s_null());
  271. else if (os && os->length > 0)
  272. bio = BIO_new_mem_buf(os->data, os->length);
  273. if(bio == NULL)
  274. {
  275. bio=BIO_new(BIO_s_mem());
  276. if (bio == NULL)
  277. goto err;
  278. BIO_set_mem_eof_return(bio,0);
  279. }
  280. }
  281. BIO_push(out,bio);
  282. bio=NULL;
  283. if (0)
  284. {
  285. err:
  286. if (out != NULL)
  287. BIO_free_all(out);
  288. if (btmp != NULL)
  289. BIO_free_all(btmp);
  290. out=NULL;
  291. }
  292. return(out);
  293. }
  294. static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
  295. {
  296. int ret;
  297. ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
  298. pcert->cert_info->issuer);
  299. if (ret)
  300. return ret;
  301. return M_ASN1_INTEGER_cmp(pcert->cert_info->serialNumber,
  302. ri->issuer_and_serial->serial);
  303. }
  304. /* int */
  305. BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
  306. {
  307. int i,j;
  308. BIO *out=NULL,*btmp=NULL,*etmp=NULL,*bio=NULL;
  309. unsigned char *tmp=NULL;
  310. X509_ALGOR *xa;
  311. ASN1_OCTET_STRING *data_body=NULL;
  312. const EVP_MD *evp_md;
  313. const EVP_CIPHER *evp_cipher=NULL;
  314. EVP_CIPHER_CTX *evp_ctx=NULL;
  315. X509_ALGOR *enc_alg=NULL;
  316. STACK_OF(X509_ALGOR) *md_sk=NULL;
  317. STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL;
  318. X509_ALGOR *xalg=NULL;
  319. PKCS7_RECIP_INFO *ri=NULL;
  320. i=OBJ_obj2nid(p7->type);
  321. p7->state=PKCS7_S_HEADER;
  322. switch (i)
  323. {
  324. case NID_pkcs7_signed:
  325. data_body=PKCS7_get_octet_string(p7->d.sign->contents);
  326. md_sk=p7->d.sign->md_algs;
  327. break;
  328. case NID_pkcs7_signedAndEnveloped:
  329. rsk=p7->d.signed_and_enveloped->recipientinfo;
  330. md_sk=p7->d.signed_and_enveloped->md_algs;
  331. data_body=p7->d.signed_and_enveloped->enc_data->enc_data;
  332. enc_alg=p7->d.signed_and_enveloped->enc_data->algorithm;
  333. evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm);
  334. if (evp_cipher == NULL)
  335. {
  336. PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  337. goto err;
  338. }
  339. xalg=p7->d.signed_and_enveloped->enc_data->algorithm;
  340. break;
  341. case NID_pkcs7_enveloped:
  342. rsk=p7->d.enveloped->recipientinfo;
  343. enc_alg=p7->d.enveloped->enc_data->algorithm;
  344. data_body=p7->d.enveloped->enc_data->enc_data;
  345. evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm);
  346. if (evp_cipher == NULL)
  347. {
  348. PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
  349. goto err;
  350. }
  351. xalg=p7->d.enveloped->enc_data->algorithm;
  352. break;
  353. default:
  354. PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  355. goto err;
  356. }
  357. /* We will be checking the signature */
  358. if (md_sk != NULL)
  359. {
  360. for (i=0; i<sk_X509_ALGOR_num(md_sk); i++)
  361. {
  362. xa=sk_X509_ALGOR_value(md_sk,i);
  363. if ((btmp=BIO_new(BIO_f_md())) == NULL)
  364. {
  365. PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB);
  366. goto err;
  367. }
  368. j=OBJ_obj2nid(xa->algorithm);
  369. evp_md=EVP_get_digestbynid(j);
  370. if (evp_md == NULL)
  371. {
  372. PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNKNOWN_DIGEST_TYPE);
  373. goto err;
  374. }
  375. BIO_set_md(btmp,evp_md);
  376. if (out == NULL)
  377. out=btmp;
  378. else
  379. BIO_push(out,btmp);
  380. btmp=NULL;
  381. }
  382. }
  383. if (evp_cipher != NULL)
  384. {
  385. #if 0
  386. unsigned char key[EVP_MAX_KEY_LENGTH];
  387. unsigned char iv[EVP_MAX_IV_LENGTH];
  388. unsigned char *p;
  389. int keylen,ivlen;
  390. int max;
  391. X509_OBJECT ret;
  392. #endif
  393. int jj;
  394. if ((etmp=BIO_new(BIO_f_cipher())) == NULL)
  395. {
  396. PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB);
  397. goto err;
  398. }
  399. /* It was encrypted, we need to decrypt the secret key
  400. * with the private key */
  401. /* Find the recipientInfo which matches the passed certificate
  402. * (if any)
  403. */
  404. if (pcert) {
  405. for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) {
  406. ri=sk_PKCS7_RECIP_INFO_value(rsk,i);
  407. if (!pkcs7_cmp_ri(ri, pcert))
  408. break;
  409. ri=NULL;
  410. }
  411. if (ri == NULL) {
  412. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  413. PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
  414. goto err;
  415. }
  416. }
  417. jj=EVP_PKEY_size(pkey);
  418. tmp=(unsigned char *)OPENSSL_malloc(jj+10);
  419. if (tmp == NULL)
  420. {
  421. PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_MALLOC_FAILURE);
  422. goto err;
  423. }
  424. /* If we haven't got a certificate try each ri in turn */
  425. if (pcert == NULL)
  426. {
  427. for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++)
  428. {
  429. ri=sk_PKCS7_RECIP_INFO_value(rsk,i);
  430. jj=EVP_PKEY_decrypt(tmp,
  431. M_ASN1_STRING_data(ri->enc_key),
  432. M_ASN1_STRING_length(ri->enc_key),
  433. pkey);
  434. if (jj > 0)
  435. break;
  436. ERR_clear_error();
  437. ri = NULL;
  438. }
  439. if (ri == NULL)
  440. {
  441. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  442. PKCS7_R_NO_RECIPIENT_MATCHES_KEY);
  443. goto err;
  444. }
  445. }
  446. else
  447. {
  448. jj=EVP_PKEY_decrypt(tmp,
  449. M_ASN1_STRING_data(ri->enc_key),
  450. M_ASN1_STRING_length(ri->enc_key), pkey);
  451. if (jj <= 0)
  452. {
  453. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  454. ERR_R_EVP_LIB);
  455. goto err;
  456. }
  457. }
  458. evp_ctx=NULL;
  459. BIO_get_cipher_ctx(etmp,&evp_ctx);
  460. if (EVP_CipherInit_ex(evp_ctx,evp_cipher,NULL,NULL,NULL,0) <= 0)
  461. goto err;
  462. if (EVP_CIPHER_asn1_to_param(evp_ctx,enc_alg->parameter) < 0)
  463. goto err;
  464. if (jj != EVP_CIPHER_CTX_key_length(evp_ctx)) {
  465. /* Some S/MIME clients don't use the same key
  466. * and effective key length. The key length is
  467. * determined by the size of the decrypted RSA key.
  468. */
  469. if(!EVP_CIPHER_CTX_set_key_length(evp_ctx, jj))
  470. {
  471. PKCS7err(PKCS7_F_PKCS7_DATADECODE,
  472. PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH);
  473. goto err;
  474. }
  475. }
  476. if (EVP_CipherInit_ex(evp_ctx,NULL,NULL,tmp,NULL,0) <= 0)
  477. goto err;
  478. OPENSSL_cleanse(tmp,jj);
  479. if (out == NULL)
  480. out=etmp;
  481. else
  482. BIO_push(out,etmp);
  483. etmp=NULL;
  484. }
  485. #if 1
  486. if (PKCS7_is_detached(p7) || (in_bio != NULL))
  487. {
  488. bio=in_bio;
  489. }
  490. else
  491. {
  492. #if 0
  493. bio=BIO_new(BIO_s_mem());
  494. /* We need to set this so that when we have read all
  495. * the data, the encrypt BIO, if present, will read
  496. * EOF and encode the last few bytes */
  497. BIO_set_mem_eof_return(bio,0);
  498. if (data_body->length > 0)
  499. BIO_write(bio,(char *)data_body->data,data_body->length);
  500. #else
  501. if (data_body->length > 0)
  502. bio = BIO_new_mem_buf(data_body->data,data_body->length);
  503. else {
  504. bio=BIO_new(BIO_s_mem());
  505. BIO_set_mem_eof_return(bio,0);
  506. }
  507. if (bio == NULL)
  508. goto err;
  509. #endif
  510. }
  511. BIO_push(out,bio);
  512. bio=NULL;
  513. #endif
  514. if (0)
  515. {
  516. err:
  517. if (out != NULL) BIO_free_all(out);
  518. if (btmp != NULL) BIO_free_all(btmp);
  519. if (etmp != NULL) BIO_free_all(etmp);
  520. if (bio != NULL) BIO_free_all(bio);
  521. out=NULL;
  522. }
  523. if (tmp != NULL)
  524. OPENSSL_free(tmp);
  525. return(out);
  526. }
  527. static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
  528. {
  529. for (;;)
  530. {
  531. bio=BIO_find_type(bio,BIO_TYPE_MD);
  532. if (bio == NULL)
  533. {
  534. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  535. return NULL;
  536. }
  537. BIO_get_md_ctx(bio,pmd);
  538. if (*pmd == NULL)
  539. {
  540. PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,ERR_R_INTERNAL_ERROR);
  541. return NULL;
  542. }
  543. if (EVP_MD_CTX_type(*pmd) == nid)
  544. return bio;
  545. bio=BIO_next(bio);
  546. }
  547. return NULL;
  548. }
  549. int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
  550. {
  551. int ret=0;
  552. int i,j;
  553. BIO *btmp;
  554. BUF_MEM *buf_mem=NULL;
  555. BUF_MEM *buf=NULL;
  556. PKCS7_SIGNER_INFO *si;
  557. EVP_MD_CTX *mdc,ctx_tmp;
  558. STACK_OF(X509_ATTRIBUTE) *sk;
  559. STACK_OF(PKCS7_SIGNER_INFO) *si_sk=NULL;
  560. ASN1_OCTET_STRING *os=NULL;
  561. EVP_MD_CTX_init(&ctx_tmp);
  562. i=OBJ_obj2nid(p7->type);
  563. p7->state=PKCS7_S_HEADER;
  564. switch (i)
  565. {
  566. case NID_pkcs7_signedAndEnveloped:
  567. /* XXXXXXXXXXXXXXXX */
  568. si_sk=p7->d.signed_and_enveloped->signer_info;
  569. if (!(os=M_ASN1_OCTET_STRING_new()))
  570. {
  571. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_MALLOC_FAILURE);
  572. goto err;
  573. }
  574. p7->d.signed_and_enveloped->enc_data->enc_data=os;
  575. break;
  576. case NID_pkcs7_enveloped:
  577. /* XXXXXXXXXXXXXXXX */
  578. if (!(os=M_ASN1_OCTET_STRING_new()))
  579. {
  580. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_MALLOC_FAILURE);
  581. goto err;
  582. }
  583. p7->d.enveloped->enc_data->enc_data=os;
  584. break;
  585. case NID_pkcs7_signed:
  586. si_sk=p7->d.sign->signer_info;
  587. os=PKCS7_get_octet_string(p7->d.sign->contents);
  588. /* If detached data then the content is excluded */
  589. if(PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
  590. M_ASN1_OCTET_STRING_free(os);
  591. p7->d.sign->contents->d.data = NULL;
  592. }
  593. break;
  594. case NID_pkcs7_digest:
  595. os=PKCS7_get_octet_string(p7->d.digest->contents);
  596. /* If detached data then the content is excluded */
  597. if(PKCS7_type_is_data(p7->d.digest->contents) && p7->detached)
  598. {
  599. M_ASN1_OCTET_STRING_free(os);
  600. p7->d.digest->contents->d.data = NULL;
  601. }
  602. break;
  603. }
  604. if (si_sk != NULL)
  605. {
  606. if ((buf=BUF_MEM_new()) == NULL)
  607. {
  608. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_BIO_LIB);
  609. goto err;
  610. }
  611. for (i=0; i<sk_PKCS7_SIGNER_INFO_num(si_sk); i++)
  612. {
  613. si=sk_PKCS7_SIGNER_INFO_value(si_sk,i);
  614. if (si->pkey == NULL) continue;
  615. j=OBJ_obj2nid(si->digest_alg->algorithm);
  616. btmp=bio;
  617. btmp = PKCS7_find_digest(&mdc, btmp, j);
  618. if (btmp == NULL)
  619. goto err;
  620. /* We now have the EVP_MD_CTX, lets do the
  621. * signing. */
  622. EVP_MD_CTX_copy_ex(&ctx_tmp,mdc);
  623. if (!BUF_MEM_grow_clean(buf,EVP_PKEY_size(si->pkey)))
  624. {
  625. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_BIO_LIB);
  626. goto err;
  627. }
  628. sk=si->auth_attr;
  629. /* If there are attributes, we add the digest
  630. * attribute and only sign the attributes */
  631. if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0))
  632. {
  633. unsigned char md_data[EVP_MAX_MD_SIZE], *abuf=NULL;
  634. unsigned int md_len, alen;
  635. ASN1_OCTET_STRING *digest;
  636. ASN1_UTCTIME *sign_time;
  637. const EVP_MD *md_tmp;
  638. /* Add signing time if not already present */
  639. if (!PKCS7_get_signed_attribute(si,
  640. NID_pkcs9_signingTime))
  641. {
  642. if (!(sign_time=X509_gmtime_adj(NULL,0)))
  643. {
  644. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
  645. ERR_R_MALLOC_FAILURE);
  646. goto err;
  647. }
  648. if (!PKCS7_add_signed_attribute(si,
  649. NID_pkcs9_signingTime,
  650. V_ASN1_UTCTIME,sign_time))
  651. {
  652. M_ASN1_UTCTIME_free(sign_time);
  653. goto err;
  654. }
  655. }
  656. /* Add digest */
  657. md_tmp=EVP_MD_CTX_md(&ctx_tmp);
  658. EVP_DigestFinal_ex(&ctx_tmp,md_data,&md_len);
  659. if (!(digest=M_ASN1_OCTET_STRING_new()))
  660. {
  661. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
  662. ERR_R_MALLOC_FAILURE);
  663. goto err;
  664. }
  665. if (!M_ASN1_OCTET_STRING_set(digest,md_data,
  666. md_len))
  667. {
  668. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
  669. ERR_R_MALLOC_FAILURE);
  670. M_ASN1_OCTET_STRING_free(digest);
  671. goto err;
  672. }
  673. if (!PKCS7_add_signed_attribute(si,
  674. NID_pkcs9_messageDigest,
  675. V_ASN1_OCTET_STRING,digest))
  676. {
  677. M_ASN1_OCTET_STRING_free(digest);
  678. goto err;
  679. }
  680. /* Now sign the attributes */
  681. EVP_SignInit_ex(&ctx_tmp,md_tmp,NULL);
  682. alen = ASN1_item_i2d((ASN1_VALUE *)sk,&abuf,
  683. ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
  684. if(!abuf) goto err;
  685. EVP_SignUpdate(&ctx_tmp,abuf,alen);
  686. OPENSSL_free(abuf);
  687. }
  688. #ifndef OPENSSL_NO_DSA
  689. if (si->pkey->type == EVP_PKEY_DSA)
  690. ctx_tmp.digest=EVP_dss1();
  691. #endif
  692. #ifndef OPENSSL_NO_ECDSA
  693. if (si->pkey->type == EVP_PKEY_EC)
  694. ctx_tmp.digest=EVP_ecdsa();
  695. #endif
  696. if (!EVP_SignFinal(&ctx_tmp,(unsigned char *)buf->data,
  697. (unsigned int *)&buf->length,si->pkey))
  698. {
  699. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_EVP_LIB);
  700. goto err;
  701. }
  702. if (!ASN1_STRING_set(si->enc_digest,
  703. (unsigned char *)buf->data,buf->length))
  704. {
  705. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_ASN1_LIB);
  706. goto err;
  707. }
  708. }
  709. }
  710. else if (i == NID_pkcs7_digest)
  711. {
  712. unsigned char md_data[EVP_MAX_MD_SIZE];
  713. unsigned int md_len;
  714. if (!PKCS7_find_digest(&mdc, bio,
  715. OBJ_obj2nid(p7->d.digest->md->algorithm)))
  716. goto err;
  717. EVP_DigestFinal_ex(mdc,md_data,&md_len);
  718. M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len);
  719. }
  720. if (!PKCS7_is_detached(p7))
  721. {
  722. btmp=BIO_find_type(bio,BIO_TYPE_MEM);
  723. if (btmp == NULL)
  724. {
  725. PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
  726. goto err;
  727. }
  728. BIO_get_mem_ptr(btmp,&buf_mem);
  729. /* Mark the BIO read only then we can use its copy of the data
  730. * instead of making an extra copy.
  731. */
  732. BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
  733. BIO_set_mem_eof_return(btmp, 0);
  734. os->data = (unsigned char *)buf_mem->data;
  735. os->length = buf_mem->length;
  736. #if 0
  737. M_ASN1_OCTET_STRING_set(os,
  738. (unsigned char *)buf_mem->data,buf_mem->length);
  739. #endif
  740. }
  741. ret=1;
  742. err:
  743. EVP_MD_CTX_cleanup(&ctx_tmp);
  744. if (buf != NULL) BUF_MEM_free(buf);
  745. return(ret);
  746. }
  747. int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
  748. PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  749. {
  750. PKCS7_ISSUER_AND_SERIAL *ias;
  751. int ret=0,i;
  752. STACK_OF(X509) *cert;
  753. X509 *x509;
  754. if (PKCS7_type_is_signed(p7))
  755. {
  756. cert=p7->d.sign->cert;
  757. }
  758. else if (PKCS7_type_is_signedAndEnveloped(p7))
  759. {
  760. cert=p7->d.signed_and_enveloped->cert;
  761. }
  762. else
  763. {
  764. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_WRONG_PKCS7_TYPE);
  765. goto err;
  766. }
  767. /* XXXXXXXXXXXXXXXXXXXXXXX */
  768. ias=si->issuer_and_serial;
  769. x509=X509_find_by_issuer_and_serial(cert,ias->issuer,ias->serial);
  770. /* were we able to find the cert in passed to us */
  771. if (x509 == NULL)
  772. {
  773. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
  774. goto err;
  775. }
  776. /* Lets verify */
  777. if(!X509_STORE_CTX_init(ctx,cert_store,x509,cert))
  778. {
  779. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB);
  780. goto err;
  781. }
  782. X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
  783. i=X509_verify_cert(ctx);
  784. if (i <= 0)
  785. {
  786. PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB);
  787. X509_STORE_CTX_cleanup(ctx);
  788. goto err;
  789. }
  790. X509_STORE_CTX_cleanup(ctx);
  791. return PKCS7_signatureVerify(bio, p7, si, x509);
  792. err:
  793. return ret;
  794. }
  795. int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
  796. X509 *x509)
  797. {
  798. ASN1_OCTET_STRING *os;
  799. EVP_MD_CTX mdc_tmp,*mdc;
  800. int ret=0,i;
  801. int md_type;
  802. STACK_OF(X509_ATTRIBUTE) *sk;
  803. BIO *btmp;
  804. EVP_PKEY *pkey;
  805. EVP_MD_CTX_init(&mdc_tmp);
  806. if (!PKCS7_type_is_signed(p7) &&
  807. !PKCS7_type_is_signedAndEnveloped(p7)) {
  808. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  809. PKCS7_R_WRONG_PKCS7_TYPE);
  810. goto err;
  811. }
  812. md_type=OBJ_obj2nid(si->digest_alg->algorithm);
  813. btmp=bio;
  814. for (;;)
  815. {
  816. if ((btmp == NULL) ||
  817. ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL))
  818. {
  819. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  820. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  821. goto err;
  822. }
  823. BIO_get_md_ctx(btmp,&mdc);
  824. if (mdc == NULL)
  825. {
  826. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  827. ERR_R_INTERNAL_ERROR);
  828. goto err;
  829. }
  830. if (EVP_MD_CTX_type(mdc) == md_type)
  831. break;
  832. /* Workaround for some broken clients that put the signature
  833. * OID instead of the digest OID in digest_alg->algorithm
  834. */
  835. if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
  836. break;
  837. btmp=BIO_next(btmp);
  838. }
  839. /* mdc is the digest ctx that we want, unless there are attributes,
  840. * in which case the digest is the signed attributes */
  841. EVP_MD_CTX_copy_ex(&mdc_tmp,mdc);
  842. sk=si->auth_attr;
  843. if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0))
  844. {
  845. unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
  846. unsigned int md_len, alen;
  847. ASN1_OCTET_STRING *message_digest;
  848. EVP_DigestFinal_ex(&mdc_tmp,md_dat,&md_len);
  849. message_digest=PKCS7_digest_from_attributes(sk);
  850. if (!message_digest)
  851. {
  852. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  853. PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
  854. goto err;
  855. }
  856. if ((message_digest->length != (int)md_len) ||
  857. (memcmp(message_digest->data,md_dat,md_len)))
  858. {
  859. #if 0
  860. {
  861. int ii;
  862. for (ii=0; ii<message_digest->length; ii++)
  863. printf("%02X",message_digest->data[ii]); printf(" sent\n");
  864. for (ii=0; ii<md_len; ii++) printf("%02X",md_dat[ii]); printf(" calc\n");
  865. }
  866. #endif
  867. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  868. PKCS7_R_DIGEST_FAILURE);
  869. ret= -1;
  870. goto err;
  871. }
  872. EVP_VerifyInit_ex(&mdc_tmp,EVP_get_digestbynid(md_type), NULL);
  873. alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
  874. ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
  875. EVP_VerifyUpdate(&mdc_tmp, abuf, alen);
  876. OPENSSL_free(abuf);
  877. }
  878. os=si->enc_digest;
  879. pkey = X509_get_pubkey(x509);
  880. if (!pkey)
  881. {
  882. ret = -1;
  883. goto err;
  884. }
  885. #ifndef OPENSSL_NO_DSA
  886. if(pkey->type == EVP_PKEY_DSA) mdc_tmp.digest=EVP_dss1();
  887. #endif
  888. #ifndef OPENSSL_NO_ECDSA
  889. if (pkey->type == EVP_PKEY_EC) mdc_tmp.digest=EVP_ecdsa();
  890. #endif
  891. i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length, pkey);
  892. EVP_PKEY_free(pkey);
  893. if (i <= 0)
  894. {
  895. PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
  896. PKCS7_R_SIGNATURE_FAILURE);
  897. ret= -1;
  898. goto err;
  899. }
  900. else
  901. ret=1;
  902. err:
  903. EVP_MD_CTX_cleanup(&mdc_tmp);
  904. return(ret);
  905. }
  906. PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
  907. {
  908. STACK_OF(PKCS7_RECIP_INFO) *rsk;
  909. PKCS7_RECIP_INFO *ri;
  910. int i;
  911. i=OBJ_obj2nid(p7->type);
  912. if (i != NID_pkcs7_signedAndEnveloped)
  913. return NULL;
  914. if (p7->d.signed_and_enveloped == NULL)
  915. return NULL;
  916. rsk=p7->d.signed_and_enveloped->recipientinfo;
  917. if (rsk == NULL)
  918. return NULL;
  919. ri=sk_PKCS7_RECIP_INFO_value(rsk,0);
  920. if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL);
  921. ri=sk_PKCS7_RECIP_INFO_value(rsk,idx);
  922. return(ri->issuer_and_serial);
  923. }
  924. ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
  925. {
  926. return(get_attribute(si->auth_attr,nid));
  927. }
  928. ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
  929. {
  930. return(get_attribute(si->unauth_attr,nid));
  931. }
  932. static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
  933. {
  934. int i;
  935. X509_ATTRIBUTE *xa;
  936. ASN1_OBJECT *o;
  937. o=OBJ_nid2obj(nid);
  938. if (!o || !sk) return(NULL);
  939. for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
  940. {
  941. xa=sk_X509_ATTRIBUTE_value(sk,i);
  942. if (OBJ_cmp(xa->object,o) == 0)
  943. {
  944. if (!xa->single && sk_ASN1_TYPE_num(xa->value.set))
  945. return(sk_ASN1_TYPE_value(xa->value.set,0));
  946. else
  947. return(NULL);
  948. }
  949. }
  950. return(NULL);
  951. }
  952. ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
  953. {
  954. ASN1_TYPE *astype;
  955. if(!(astype = get_attribute(sk, NID_pkcs9_messageDigest))) return NULL;
  956. return astype->value.octet_string;
  957. }
  958. int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
  959. STACK_OF(X509_ATTRIBUTE) *sk)
  960. {
  961. int i;
  962. if (p7si->auth_attr != NULL)
  963. sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr,X509_ATTRIBUTE_free);
  964. p7si->auth_attr=sk_X509_ATTRIBUTE_dup(sk);
  965. if (p7si->auth_attr == NULL)
  966. return 0;
  967. for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
  968. {
  969. if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr,i,
  970. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i))))
  971. == NULL)
  972. return(0);
  973. }
  974. return(1);
  975. }
  976. int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk)
  977. {
  978. int i;
  979. if (p7si->unauth_attr != NULL)
  980. sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr,
  981. X509_ATTRIBUTE_free);
  982. p7si->unauth_attr=sk_X509_ATTRIBUTE_dup(sk);
  983. if (p7si->unauth_attr == NULL)
  984. return 0;
  985. for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
  986. {
  987. if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr,i,
  988. X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i))))
  989. == NULL)
  990. return(0);
  991. }
  992. return(1);
  993. }
  994. int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  995. void *value)
  996. {
  997. return(add_attribute(&(p7si->auth_attr),nid,atrtype,value));
  998. }
  999. int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  1000. void *value)
  1001. {
  1002. return(add_attribute(&(p7si->unauth_attr),nid,atrtype,value));
  1003. }
  1004. static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
  1005. void *value)
  1006. {
  1007. X509_ATTRIBUTE *attr=NULL;
  1008. if (*sk == NULL)
  1009. {
  1010. if (!(*sk = sk_X509_ATTRIBUTE_new_null()))
  1011. return 0;
  1012. new_attrib:
  1013. if (!(attr=X509_ATTRIBUTE_create(nid,atrtype,value)))
  1014. return 0;
  1015. if (!sk_X509_ATTRIBUTE_push(*sk,attr))
  1016. {
  1017. X509_ATTRIBUTE_free(attr);
  1018. return 0;
  1019. }
  1020. }
  1021. else
  1022. {
  1023. int i;
  1024. for (i=0; i<sk_X509_ATTRIBUTE_num(*sk); i++)
  1025. {
  1026. attr=sk_X509_ATTRIBUTE_value(*sk,i);
  1027. if (OBJ_obj2nid(attr->object) == nid)
  1028. {
  1029. X509_ATTRIBUTE_free(attr);
  1030. attr=X509_ATTRIBUTE_create(nid,atrtype,value);
  1031. if (attr == NULL)
  1032. return 0;
  1033. if (!sk_X509_ATTRIBUTE_set(*sk,i,attr))
  1034. {
  1035. X509_ATTRIBUTE_free(attr);
  1036. return 0;
  1037. }
  1038. goto end;
  1039. }
  1040. }
  1041. goto new_attrib;
  1042. }
  1043. end:
  1044. return(1);
  1045. }