pkcs7-openssl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. // Copyright (C) 2004-2024 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/pdf.h"
  24. #include "mupdf/helpers/pkcs7-openssl.h"
  25. #ifndef HAVE_LIBCRYPTO
  26. pdf_pkcs7_signer *
  27. pkcs7_openssl_read_pfx(fz_context *ctx, const char *pfile, const char *pw)
  28. {
  29. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OpenSSL support.");
  30. }
  31. pdf_pkcs7_verifier *
  32. pkcs7_openssl_new_verifier(fz_context *ctx)
  33. {
  34. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OpenSSL support.");
  35. }
  36. #else
  37. #include <limits.h>
  38. #include <string.h>
  39. /* Generated from resources/certs/AdobeCA.p7c */
  40. static const char AdobeCA_p7c[] = {
  41. 48,130,4,208,6,9,42,134,72,134,247,13,1,7,2,160,130,4,193,48,130,4,189,2,
  42. 1,1,49,0,48,11,6,9,42,134,72,134,247,13,1,7,1,160,130,4,165,48,130,4,161,
  43. 48,130,3,137,160,3,2,1,2,2,4,62,28,189,40,48,13,6,9,42,134,72,134,247,13,
  44. 1,1,5,5,0,48,105,49,11,48,9,6,3,85,4,6,19,2,85,83,49,35,48,33,6,3,85,4,10,
  45. 19,26,65,100,111,98,101,32,83,121,115,116,101,109,115,32,73,110,99,111,114,
  46. 112,111,114,97,116,101,100,49,29,48,27,6,3,85,4,11,19,20,65,100,111,98,101,
  47. 32,84,114,117,115,116,32,83,101,114,118,105,99,101,115,49,22,48,20,6,3,85,
  48. 4,3,19,13,65,100,111,98,101,32,82,111,111,116,32,67,65,48,30,23,13,48,51,
  49. 48,49,48,56,50,51,51,55,50,51,90,23,13,50,51,48,49,48,57,48,48,48,55,50,51,
  50. 90,48,105,49,11,48,9,6,3,85,4,6,19,2,85,83,49,35,48,33,6,3,85,4,10,19,26,
  51. 65,100,111,98,101,32,83,121,115,116,101,109,115,32,73,110,99,111,114,112,
  52. 111,114,97,116,101,100,49,29,48,27,6,3,85,4,11,19,20,65,100,111,98,101,32,
  53. 84,114,117,115,116,32,83,101,114,118,105,99,101,115,49,22,48,20,6,3,85,4,
  54. 3,19,13,65,100,111,98,101,32,82,111,111,116,32,67,65,48,130,1,34,48,13,6,
  55. 9,42,134,72,134,247,13,1,1,1,5,0,3,130,1,15,0,48,130,1,10,2,130,1,1,0,204,
  56. 79,84,132,247,167,162,231,51,83,127,63,156,18,136,107,44,153,71,103,126,15,
  57. 30,185,173,20,136,249,195,16,216,29,240,240,213,159,105,10,47,89,53,176,204,
  58. 108,169,76,156,21,160,159,206,32,191,160,207,84,226,224,32,102,69,63,57,134,
  59. 56,126,156,196,142,7,34,198,36,246,1,18,176,53,223,85,234,105,144,176,219,
  60. 133,55,30,226,78,7,178,66,161,106,19,105,160,102,234,128,145,17,89,42,155,
  61. 8,121,90,32,68,45,201,189,115,56,139,60,47,224,67,27,93,179,11,240,175,53,
  62. 26,41,254,239,166,146,221,129,76,157,61,89,142,173,49,60,64,126,155,145,54,
  63. 6,252,226,92,141,209,141,38,213,92,69,207,175,101,63,177,170,210,98,150,244,
  64. 168,56,234,186,96,66,244,244,28,74,53,21,206,248,78,34,86,15,149,24,197,248,
  65. 150,159,159,251,176,183,120,37,233,128,107,189,214,10,240,198,116,148,157,
  66. 243,15,80,219,154,119,206,75,112,131,35,141,160,202,120,32,68,92,60,84,100,
  67. 241,234,162,48,25,159,234,76,6,77,6,120,75,94,146,223,34,210,201,103,179,
  68. 122,210,1,2,3,1,0,1,163,130,1,79,48,130,1,75,48,17,6,9,96,134,72,1,134,248,
  69. 66,1,1,4,4,3,2,0,7,48,129,142,6,3,85,29,31,4,129,134,48,129,131,48,129,128,
  70. 160,126,160,124,164,122,48,120,49,11,48,9,6,3,85,4,6,19,2,85,83,49,35,48,
  71. 33,6,3,85,4,10,19,26,65,100,111,98,101,32,83,121,115,116,101,109,115,32,73,
  72. 110,99,111,114,112,111,114,97,116,101,100,49,29,48,27,6,3,85,4,11,19,20,65,
  73. 100,111,98,101,32,84,114,117,115,116,32,83,101,114,118,105,99,101,115,49,
  74. 22,48,20,6,3,85,4,3,19,13,65,100,111,98,101,32,82,111,111,116,32,67,65,49,
  75. 13,48,11,6,3,85,4,3,19,4,67,82,76,49,48,43,6,3,85,29,16,4,36,48,34,128,15,
  76. 50,48,48,51,48,49,48,56,50,51,51,55,50,51,90,129,15,50,48,50,51,48,49,48,
  77. 57,48,48,48,55,50,51,90,48,11,6,3,85,29,15,4,4,3,2,1,6,48,31,6,3,85,29,35,
  78. 4,24,48,22,128,20,130,183,56,74,147,170,155,16,239,128,187,217,84,226,241,
  79. 15,251,128,156,222,48,29,6,3,85,29,14,4,22,4,20,130,183,56,74,147,170,155,
  80. 16,239,128,187,217,84,226,241,15,251,128,156,222,48,12,6,3,85,29,19,4,5,48,
  81. 3,1,1,255,48,29,6,9,42,134,72,134,246,125,7,65,0,4,16,48,14,27,8,86,54,46,
  82. 48,58,52,46,48,3,2,4,144,48,13,6,9,42,134,72,134,247,13,1,1,5,5,0,3,130,1,
  83. 1,0,50,218,159,67,117,193,250,111,201,111,219,171,29,54,55,62,188,97,25,54,
  84. 183,2,60,29,35,89,152,108,158,238,77,133,231,84,200,32,31,167,212,187,226,
  85. 191,0,119,125,36,107,112,47,92,193,58,118,73,181,211,224,35,132,42,113,106,
  86. 34,243,193,39,41,152,21,246,53,144,228,4,76,195,141,188,159,97,28,231,253,
  87. 36,140,209,68,67,140,22,186,155,77,165,212,53,47,188,17,206,189,247,81,55,
  88. 141,159,144,228,20,241,24,63,190,233,89,18,53,249,51,146,243,158,224,213,
  89. 107,154,113,155,153,75,200,113,195,225,177,97,9,196,229,250,145,240,66,58,
  90. 55,125,52,249,114,232,205,170,98,28,33,233,213,244,130,16,227,123,5,182,45,
  91. 104,86,11,126,126,146,44,111,77,114,130,12,237,86,116,178,157,185,171,45,
  92. 43,29,16,95,219,39,117,112,143,253,29,215,226,2,160,121,229,28,229,255,175,
  93. 100,64,81,45,158,155,71,219,66,165,124,31,194,166,72,176,215,190,146,105,
  94. 77,164,246,41,87,197,120,17,24,220,135,81,202,19,178,98,157,79,43,50,189,
  95. 49,165,193,250,82,171,5,136,200,49,0
  96. };
  97. #include "openssl/err.h"
  98. #include "openssl/bio.h"
  99. #include "openssl/asn1.h"
  100. #include "openssl/x509.h"
  101. #include "openssl/x509v3.h"
  102. #include "openssl/err.h"
  103. #include "openssl/objects.h"
  104. #include "openssl/pem.h"
  105. #include "openssl/pkcs7.h"
  106. #include "openssl/pkcs12.h"
  107. #include "openssl/opensslv.h"
  108. #ifndef OPENSSL_VERSION_NUMBER
  109. #warning detect version of openssl at compile time
  110. #endif
  111. #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3060000fL
  112. static inline const STACK_OF(PKCS12_SAFEBAG) *
  113. PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag)
  114. {
  115. return (OBJ_obj2nid(bag->type) == NID_safeContentsBag) ? bag->value.safes : NULL;
  116. }
  117. static inline const PKCS8_PRIV_KEY_INFO *
  118. PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag)
  119. {
  120. return (OBJ_obj2nid(bag->type) == NID_keyBag) ? bag->value.keybag : NULL;
  121. }
  122. #endif
  123. typedef struct
  124. {
  125. fz_context *ctx;
  126. fz_stream *stm;
  127. } BIO_stream_data;
  128. static int stream_read(BIO *b, char *buf, int size)
  129. {
  130. BIO_stream_data *data = (BIO_stream_data *)BIO_get_data(b);
  131. size_t read = 0;
  132. fz_try(data->ctx)
  133. read = fz_read(data->ctx, data->stm, (unsigned char *) buf, size);
  134. fz_catch(data->ctx)
  135. return -1;
  136. return (int)read;
  137. }
  138. static long stream_ctrl(BIO *b, int cmd, long arg1, void *arg2)
  139. {
  140. BIO_stream_data *data = (BIO_stream_data *)BIO_get_data(b);
  141. long ret;
  142. switch (cmd)
  143. {
  144. case BIO_C_FILE_SEEK:
  145. fz_try(data->ctx)
  146. {
  147. fz_seek(data->ctx, data->stm, arg1, SEEK_SET);
  148. ret = fz_tell(data->ctx, data->stm);
  149. }
  150. fz_catch(data->ctx)
  151. return -1;
  152. return ret;
  153. default:
  154. return 1;
  155. }
  156. }
  157. static int stream_new(BIO *b)
  158. {
  159. BIO_stream_data *data = (BIO_stream_data *)malloc(sizeof(BIO_stream_data));
  160. if (!data)
  161. return 0;
  162. data->ctx = NULL;
  163. data->stm = NULL;
  164. BIO_set_init(b, 1);
  165. BIO_set_data(b, data);
  166. BIO_clear_flags(b, INT_MAX);
  167. return 1;
  168. }
  169. static int stream_free(BIO *b)
  170. {
  171. if (b == NULL)
  172. return 0;
  173. free(BIO_get_data(b));
  174. BIO_set_data(b, NULL);
  175. BIO_set_init(b, 0);
  176. BIO_clear_flags(b, INT_MAX);
  177. return 1;
  178. }
  179. static long stream_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  180. {
  181. return 1;
  182. }
  183. static BIO *BIO_new_stream(fz_context *ctx, fz_stream *stm)
  184. {
  185. static BIO_METHOD *methods = NULL;
  186. BIO *bio;
  187. BIO_stream_data *data;
  188. if (!methods)
  189. {
  190. methods = BIO_meth_new(BIO_TYPE_NONE, "segment reader");
  191. if (!methods)
  192. return NULL;
  193. BIO_meth_set_read(methods, stream_read);
  194. BIO_meth_set_ctrl(methods, stream_ctrl);
  195. BIO_meth_set_create(methods, stream_new);
  196. BIO_meth_set_destroy(methods, stream_free);
  197. BIO_meth_set_callback_ctrl(methods, stream_callback_ctrl);
  198. }
  199. bio = BIO_new(methods);
  200. if (!bio)
  201. {
  202. BIO_meth_free(methods);
  203. return NULL;
  204. }
  205. data = BIO_get_data(bio);
  206. data->ctx = ctx;
  207. data->stm = stm;
  208. return bio;
  209. }
  210. static int verify_callback(int ok, X509_STORE_CTX *ctx)
  211. {
  212. int err, depth;
  213. err = X509_STORE_CTX_get_error(ctx);
  214. depth = X509_STORE_CTX_get_error_depth(ctx);
  215. if (!ok && depth >= 6)
  216. {
  217. X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_CHAIN_TOO_LONG);
  218. }
  219. switch (err)
  220. {
  221. case X509_V_ERR_INVALID_PURPOSE:
  222. case X509_V_ERR_CERT_HAS_EXPIRED:
  223. case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
  224. X509_STORE_CTX_set_error(ctx, X509_V_OK);
  225. ok = 1;
  226. break;
  227. default:
  228. break;
  229. }
  230. return ok;
  231. }
  232. /* Get the certificates from a PKCS7 object */
  233. static STACK_OF(X509) *pk7_certs(PKCS7 *pk7)
  234. {
  235. if (pk7 == NULL || pk7->d.ptr == NULL)
  236. return NULL;
  237. if (PKCS7_type_is_signed(pk7))
  238. return pk7->d.sign->cert;
  239. else if (PKCS7_type_is_signedAndEnveloped(pk7))
  240. return pk7->d.signed_and_enveloped->cert;
  241. else
  242. return NULL;
  243. }
  244. /* Get the signing certificate from a PKCS7 object */
  245. static X509 *pk7_signer(STACK_OF(X509) *certs, PKCS7_SIGNER_INFO *si)
  246. {
  247. PKCS7_ISSUER_AND_SERIAL *ias = si->issuer_and_serial;
  248. if (certs == NULL)
  249. return NULL;
  250. return X509_find_by_issuer_and_serial(certs, ias->issuer, ias->serial);
  251. }
  252. static pdf_signature_error pk7_verify_sig(PKCS7 *p7, BIO *detached)
  253. {
  254. BIO *p7bio=NULL;
  255. char readbuf[1024*4];
  256. int res = PDF_SIGNATURE_ERROR_UNKNOWN;
  257. int i;
  258. STACK_OF(PKCS7_SIGNER_INFO) *sk;
  259. ERR_clear_error();
  260. p7bio = PKCS7_dataInit(p7, detached);
  261. if (!p7bio)
  262. goto exit;
  263. /* We now have to 'read' from p7bio to calculate digests etc. */
  264. while (BIO_read(p7bio, readbuf, sizeof(readbuf)) > 0)
  265. ;
  266. /* We can now verify signatures */
  267. sk = PKCS7_get_signer_info(p7);
  268. if (sk == NULL || sk_PKCS7_SIGNER_INFO_num(sk) <= 0)
  269. {
  270. /* there are no signatures on this data */
  271. res = PDF_SIGNATURE_ERROR_NO_SIGNATURES;
  272. goto exit;
  273. }
  274. for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sk); i++)
  275. {
  276. int rc;
  277. PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(sk, i);
  278. X509 *x509 = pk7_signer(pk7_certs(p7), si);
  279. /* were we able to find the cert in passed to us */
  280. if (x509 == NULL)
  281. goto exit;
  282. rc = PKCS7_signatureVerify(p7bio, p7, si, x509);
  283. if (rc > 0)
  284. {
  285. res = PDF_SIGNATURE_ERROR_OKAY;
  286. }
  287. else
  288. {
  289. long err = ERR_GET_REASON(ERR_get_error());
  290. switch (err)
  291. {
  292. case PKCS7_R_DIGEST_FAILURE:
  293. res = PDF_SIGNATURE_ERROR_DIGEST_FAILURE;
  294. break;
  295. default:
  296. break;
  297. }
  298. goto exit;
  299. }
  300. }
  301. exit:
  302. BIO_free(p7bio);
  303. ERR_free_strings();
  304. return res;
  305. }
  306. static pdf_signature_error pk7_verify_cert(X509_STORE *cert_store, PKCS7 *p7)
  307. {
  308. int res = PDF_SIGNATURE_ERROR_OKAY;
  309. int i;
  310. STACK_OF(PKCS7_SIGNER_INFO) *sk;
  311. X509_STORE_CTX *ctx;
  312. ctx = X509_STORE_CTX_new();
  313. if (!ctx)
  314. return PDF_SIGNATURE_ERROR_UNKNOWN;
  315. ERR_clear_error();
  316. X509_STORE_set_verify_cb_func(cert_store, verify_callback);
  317. /* We can now verify signatures */
  318. sk = PKCS7_get_signer_info(p7);
  319. if (sk == NULL)
  320. {
  321. /* there are no signatures on this data */
  322. res = PDF_SIGNATURE_ERROR_NO_SIGNATURES;
  323. goto exit;
  324. }
  325. for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sk); i++)
  326. {
  327. int ctx_err;
  328. PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(sk, i);
  329. STACK_OF(X509) *certs = pk7_certs(p7);
  330. X509 *cert = pk7_signer(certs, si);
  331. if (cert == NULL)
  332. {
  333. res = PDF_SIGNATURE_ERROR_NO_CERTIFICATE;
  334. goto exit;
  335. }
  336. /* Acrobat reader creates self-signed certificates that don't list
  337. * certificate signing within the key usage parameters. openssl does
  338. * not recognise those as self signed. We work around this by removing
  339. * the key usage parameters before the verification check */
  340. {
  341. int i = X509_get_ext_by_NID(cert, NID_key_usage, -1);
  342. if (i >= 0)
  343. {
  344. X509_EXTENSION *ext = X509_get_ext(cert, i);
  345. X509_delete_ext(cert, i);
  346. X509_EXTENSION_free(ext);
  347. }
  348. }
  349. if (!X509_STORE_CTX_init(ctx, cert_store, cert, certs))
  350. {
  351. res = PDF_SIGNATURE_ERROR_UNKNOWN;
  352. goto exit;
  353. }
  354. if (!X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN))
  355. {
  356. res = PDF_SIGNATURE_ERROR_UNKNOWN;
  357. goto exit;
  358. }
  359. /* X509_verify_cert may return an error, but in all such cases
  360. * it sets a context error */
  361. X509_verify_cert(ctx);
  362. X509_STORE_CTX_cleanup(ctx);
  363. ctx_err = X509_STORE_CTX_get_error(ctx);
  364. switch (ctx_err)
  365. {
  366. case X509_V_OK:
  367. break;
  368. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  369. res = PDF_SIGNATURE_ERROR_SELF_SIGNED;
  370. goto exit;
  371. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  372. res = PDF_SIGNATURE_ERROR_SELF_SIGNED_IN_CHAIN;
  373. goto exit;
  374. default:
  375. res = PDF_SIGNATURE_ERROR_UNKNOWN;
  376. goto exit;
  377. }
  378. }
  379. exit:
  380. X509_STORE_CTX_free(ctx);
  381. ERR_free_strings();
  382. return res;
  383. }
  384. static pdf_signature_error
  385. check_digest(fz_context *ctx, pdf_pkcs7_verifier *verifier, fz_stream *stm, unsigned char *sig, size_t sig_len)
  386. {
  387. PKCS7 *pk7sig = NULL;
  388. BIO *bsig = NULL;
  389. BIO *bdata = NULL;
  390. int res = PDF_SIGNATURE_ERROR_UNKNOWN;
  391. if (sig_len > INT_MAX)
  392. fz_throw(ctx, FZ_ERROR_LIMIT, "Signature length too large");
  393. bsig = BIO_new_mem_buf(sig, (int)sig_len);
  394. pk7sig = d2i_PKCS7_bio(bsig, NULL);
  395. if (pk7sig == NULL)
  396. goto exit;
  397. bdata = BIO_new_stream(ctx, stm);
  398. if (bdata == NULL)
  399. goto exit;
  400. res = pk7_verify_sig(pk7sig, bdata);
  401. exit:
  402. BIO_free(bdata);
  403. PKCS7_free(pk7sig);
  404. BIO_free(bsig);
  405. return res;
  406. }
  407. static pdf_signature_error
  408. check_certificate(fz_context *ctx, pdf_pkcs7_verifier *verifier, unsigned char *sig, size_t sig_len)
  409. {
  410. PKCS7 *pk7sig = NULL;
  411. PKCS7 *pk7cert = NULL;
  412. X509_STORE *st = NULL;
  413. BIO *bsig = NULL;
  414. BIO *bcert = NULL;
  415. STACK_OF(X509) *certs = NULL;
  416. int res = PDF_SIGNATURE_ERROR_UNKNOWN;
  417. if (sig_len > INT_MAX)
  418. return res;
  419. bsig = BIO_new_mem_buf(sig, (int)sig_len);
  420. pk7sig = d2i_PKCS7_bio(bsig, NULL);
  421. if (pk7sig == NULL)
  422. goto exit;
  423. /* Find the certificates in the pk7 file */
  424. bcert = BIO_new_mem_buf((void*)AdobeCA_p7c, sizeof AdobeCA_p7c);
  425. pk7cert = d2i_PKCS7_bio(bcert, NULL);
  426. if (pk7cert == NULL)
  427. goto exit;
  428. certs = pk7_certs(pk7cert);
  429. st = X509_STORE_new();
  430. if (st == NULL)
  431. goto exit;
  432. /* Add the certificates to the store */
  433. if (certs != NULL)
  434. {
  435. int i, n = sk_X509_num(certs);
  436. for (i = 0; i < n; i++)
  437. {
  438. X509 *c = sk_X509_value(certs, i);
  439. X509_STORE_add_cert(st, c);
  440. }
  441. }
  442. res = pk7_verify_cert(st, pk7sig);
  443. exit:
  444. X509_STORE_free(st);
  445. PKCS7_free(pk7cert);
  446. BIO_free(bcert);
  447. PKCS7_free(pk7sig);
  448. BIO_free(bsig);
  449. return res;
  450. }
  451. typedef struct
  452. {
  453. pdf_pkcs7_distinguished_name base;
  454. char buf[8192];
  455. } pdf_pkcs7_distinguished_name_openssl;
  456. typedef struct
  457. {
  458. pdf_pkcs7_signer base;
  459. int refs;
  460. X509 *x509;
  461. EVP_PKEY *pkey;
  462. } openssl_signer;
  463. typedef struct
  464. {
  465. pdf_pkcs7_verifier base;
  466. } openssl_verifier;
  467. static void add_from_bags(X509 **pX509, EVP_PKEY **pPkey, const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pw);
  468. static void add_from_bag(X509 **pX509, EVP_PKEY **pPkey, PKCS12_SAFEBAG *bag, const char *pw)
  469. {
  470. EVP_PKEY *pkey = NULL;
  471. X509 *x509 = NULL;
  472. switch (M_PKCS12_bag_type(bag))
  473. {
  474. case NID_keyBag:
  475. {
  476. const PKCS8_PRIV_KEY_INFO *p8 = PKCS12_SAFEBAG_get0_p8inf(bag);
  477. pkey = EVP_PKCS82PKEY(p8);
  478. }
  479. break;
  480. case NID_pkcs8ShroudedKeyBag:
  481. {
  482. PKCS8_PRIV_KEY_INFO *p8 = PKCS12_decrypt_skey(bag, pw, (int)strlen(pw));
  483. if (p8)
  484. {
  485. pkey = EVP_PKCS82PKEY(p8);
  486. PKCS8_PRIV_KEY_INFO_free(p8);
  487. }
  488. }
  489. break;
  490. case NID_certBag:
  491. if (M_PKCS12_cert_bag_type(bag) == NID_x509Certificate)
  492. x509 = PKCS12_certbag2x509(bag);
  493. break;
  494. case NID_safeContentsBag:
  495. add_from_bags(pX509, pPkey, PKCS12_SAFEBAG_get0_safes(bag), pw);
  496. break;
  497. }
  498. if (pkey)
  499. {
  500. if (!*pPkey)
  501. *pPkey = pkey;
  502. else
  503. EVP_PKEY_free(pkey);
  504. }
  505. if (x509)
  506. {
  507. if (!*pX509)
  508. *pX509 = x509;
  509. else
  510. X509_free(x509);
  511. }
  512. }
  513. static void add_from_bags(X509 **pX509, EVP_PKEY **pPkey, const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pw)
  514. {
  515. int i;
  516. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++)
  517. add_from_bag(pX509, pPkey, sk_PKCS12_SAFEBAG_value(bags, i), pw);
  518. }
  519. static pdf_pkcs7_signer *keep_signer(fz_context *ctx, pdf_pkcs7_signer *signer)
  520. {
  521. openssl_signer *osigner = (openssl_signer *)signer;
  522. return fz_keep_imp(ctx, osigner, &osigner->refs);
  523. }
  524. static void drop_signer(fz_context *ctx, pdf_pkcs7_signer *signer)
  525. {
  526. openssl_signer *osigner = (openssl_signer *)signer;
  527. if (fz_drop_imp(ctx, osigner, &osigner->refs))
  528. {
  529. X509_free(osigner->x509);
  530. EVP_PKEY_free(osigner->pkey);
  531. fz_free(ctx, osigner);
  532. }
  533. }
  534. static char *x509_get_name_entry_string(fz_context *ctx, X509_NAME *name, int nid)
  535. {
  536. int idx = X509_NAME_get_index_by_NID(name, nid, -1);
  537. X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, idx);
  538. ASN1_STRING *data = X509_NAME_ENTRY_get_data(entry);
  539. return data ? fz_strdup(ctx, (const char *)ASN1_STRING_get0_data(data)) : NULL;
  540. }
  541. static pdf_pkcs7_distinguished_name *x509_distinguished_name(fz_context *ctx, X509 *x509)
  542. {
  543. pdf_pkcs7_distinguished_name *dn;
  544. if (x509 == NULL)
  545. return NULL;
  546. dn = fz_malloc_struct(ctx, pdf_pkcs7_distinguished_name);
  547. fz_try(ctx)
  548. {
  549. X509_NAME *subj = X509_get_subject_name(x509);
  550. dn->cn = x509_get_name_entry_string(ctx, subj, NID_commonName);
  551. dn->o = x509_get_name_entry_string(ctx, subj, NID_organizationName);
  552. dn->ou = x509_get_name_entry_string(ctx, subj, NID_organizationalUnitName);
  553. dn->email = x509_get_name_entry_string(ctx, subj, NID_pkcs9_emailAddress);
  554. dn->c = x509_get_name_entry_string(ctx, subj, NID_countryName);
  555. }
  556. fz_catch(ctx)
  557. {
  558. pdf_signature_drop_distinguished_name(ctx, dn);
  559. fz_rethrow(ctx);
  560. }
  561. return (pdf_pkcs7_distinguished_name *)dn;
  562. }
  563. static pdf_pkcs7_distinguished_name *signer_distinguished_name(fz_context *ctx, pdf_pkcs7_signer *signer)
  564. {
  565. openssl_signer *osigner = (openssl_signer *)signer;
  566. X509 *x509 = osigner->x509;
  567. pdf_pkcs7_distinguished_name *dn = fz_malloc_struct(ctx, pdf_pkcs7_distinguished_name);
  568. fz_try(ctx)
  569. {
  570. X509_NAME *subj = X509_get_subject_name(x509);
  571. dn->cn = x509_get_name_entry_string(ctx, subj, NID_commonName);
  572. dn->o = x509_get_name_entry_string(ctx, subj, NID_organizationName);
  573. dn->ou = x509_get_name_entry_string(ctx, subj, NID_organizationalUnitName);
  574. dn->email = x509_get_name_entry_string(ctx, subj, NID_pkcs9_emailAddress);
  575. dn->c = x509_get_name_entry_string(ctx, subj, NID_countryName);
  576. }
  577. fz_catch(ctx)
  578. {
  579. pdf_signature_drop_distinguished_name(ctx, dn);
  580. fz_rethrow(ctx);
  581. }
  582. return dn;
  583. }
  584. static int signer_create_digest(fz_context *ctx, pdf_pkcs7_signer *signer, fz_stream *in, unsigned char *digest, size_t digest_len)
  585. {
  586. openssl_signer *osigner = (openssl_signer *)signer;
  587. int res = 0;
  588. BIO *bdata = NULL;
  589. BIO *bp7in = NULL;
  590. BIO *bp7 = NULL;
  591. PKCS7 *p7 = NULL;
  592. PKCS7_SIGNER_INFO *si;
  593. unsigned char *p7_ptr;
  594. size_t p7_len;
  595. if (in != NULL)
  596. {
  597. bdata = BIO_new_stream(ctx, in);
  598. if (bdata == NULL)
  599. goto exit;
  600. }
  601. p7 = PKCS7_new();
  602. if (p7 == NULL)
  603. goto exit;
  604. PKCS7_set_type(p7, NID_pkcs7_signed);
  605. si = PKCS7_add_signature(p7, osigner->x509, osigner->pkey, EVP_sha1());
  606. if (si == NULL)
  607. goto exit;
  608. #ifdef CLUSTER
  609. {
  610. ASN1_UTCTIME *sign_time;
  611. time_t now = 1112281971; /* release date of MuPDF 0.1 */
  612. sign_time = X509_time_adj(NULL, 0, &now);
  613. PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime, V_ASN1_UTCTIME, sign_time);
  614. }
  615. #endif
  616. PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data));
  617. PKCS7_add_certificate(p7, osigner->x509);
  618. PKCS7_content_new(p7, NID_pkcs7_data);
  619. PKCS7_set_detached(p7, 1);
  620. bp7in = PKCS7_dataInit(p7, NULL);
  621. if (bp7in == NULL)
  622. goto exit;
  623. while(bdata) /* bdata knowingly not changed in the loop */
  624. {
  625. char buf[4096];
  626. int n = BIO_read(bdata, buf, sizeof(buf));
  627. if (n <= 0)
  628. break;
  629. BIO_write(bp7in, buf, n);
  630. }
  631. if (!PKCS7_dataFinal(p7, bp7in))
  632. goto exit;
  633. BIO_free(bdata);
  634. bdata = NULL;
  635. bp7 = BIO_new(BIO_s_mem());
  636. if (bp7 == NULL || !i2d_PKCS7_bio(bp7, p7))
  637. goto exit;
  638. p7_len = (size_t)BIO_get_mem_data(bp7, &p7_ptr);
  639. if (digest && p7_len > digest_len)
  640. goto exit;
  641. if (digest)
  642. memcpy(digest, p7_ptr, p7_len);
  643. res = p7_len;
  644. exit:
  645. BIO_free(bp7);
  646. BIO_free_all(bp7in);
  647. PKCS7_free(p7);
  648. BIO_free(bdata);
  649. return res;
  650. }
  651. static size_t max_digest_size(fz_context *ctx, pdf_pkcs7_signer *signer)
  652. {
  653. /* Perform a test digest generation to find the required size. Size
  654. * is assumed independent of data being hashed */
  655. return signer_create_digest(ctx, signer, NULL, NULL, 0);
  656. }
  657. pdf_pkcs7_signer *pkcs7_openssl_read_pfx(fz_context *ctx, const char *pfile, const char *pw)
  658. {
  659. BIO *pfxbio = NULL;
  660. PKCS12 *p12 = NULL;
  661. STACK_OF(PKCS7) *asafes;
  662. openssl_signer *signer = NULL;
  663. int i;
  664. fz_var(pfxbio);
  665. fz_var(p12);
  666. fz_var(signer);
  667. fz_try(ctx)
  668. {
  669. signer = fz_malloc_struct(ctx, openssl_signer);
  670. signer->base.keep = keep_signer;
  671. signer->base.drop = drop_signer;
  672. signer->base.get_signing_name = signer_distinguished_name;
  673. signer->base.max_digest_size = max_digest_size;
  674. signer->base.create_digest = signer_create_digest;
  675. signer->refs = 1;
  676. OpenSSL_add_all_algorithms();
  677. ERR_load_crypto_strings();
  678. ERR_clear_error();
  679. pfxbio = BIO_new_file(pfile, "rb");
  680. if (pfxbio == NULL)
  681. fz_throw(ctx, FZ_ERROR_LIBRARY, "Can't open pfx file: %s", pfile);
  682. p12 = d2i_PKCS12_bio(pfxbio, NULL);
  683. if (p12 == NULL)
  684. fz_throw(ctx, FZ_ERROR_LIBRARY, "Invalid pfx file: %s", pfile);
  685. asafes = PKCS12_unpack_authsafes(p12);
  686. if (asafes == NULL)
  687. fz_throw(ctx, FZ_ERROR_LIBRARY, "Invalid pfx file: %s", pfile);
  688. /* Nothing in this for loop can fz_throw */
  689. for (i = 0; i < sk_PKCS7_num(asafes); i++)
  690. {
  691. PKCS7 *p7;
  692. STACK_OF(PKCS12_SAFEBAG) *bags;
  693. int bagnid;
  694. p7 = sk_PKCS7_value(asafes, i);
  695. bagnid = OBJ_obj2nid(p7->type);
  696. switch (bagnid)
  697. {
  698. case NID_pkcs7_data:
  699. bags = PKCS12_unpack_p7data(p7);
  700. break;
  701. case NID_pkcs7_encrypted:
  702. bags = PKCS12_unpack_p7encdata(p7, pw, (int)strlen(pw));
  703. break;
  704. default:
  705. continue;
  706. }
  707. if (bags)
  708. {
  709. add_from_bags(&signer->x509, &signer->pkey, bags, pw);
  710. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  711. }
  712. }
  713. sk_PKCS7_pop_free (asafes, PKCS7_free);
  714. if (signer->pkey == NULL)
  715. fz_throw(ctx, FZ_ERROR_LIBRARY, "Failed to obtain public key");
  716. if (signer->x509 == NULL)
  717. fz_throw(ctx, FZ_ERROR_LIBRARY, "Failed to obtain certificate");
  718. }
  719. fz_always(ctx)
  720. {
  721. PKCS12_free(p12);
  722. BIO_free(pfxbio);
  723. }
  724. fz_catch(ctx)
  725. {
  726. drop_signer(ctx, &signer->base);
  727. fz_rethrow(ctx);
  728. }
  729. return &signer->base;
  730. }
  731. pdf_pkcs7_distinguished_name *get_signatory(fz_context *ctx, pdf_pkcs7_verifier *verifier, unsigned char *sig, size_t sig_len)
  732. {
  733. pdf_pkcs7_distinguished_name *name = NULL;
  734. PKCS7 *pk7sig = NULL;
  735. BIO *bsig = NULL;
  736. STACK_OF(PKCS7_SIGNER_INFO) *sk = NULL;
  737. X509 *x509 = NULL;
  738. if (sig_len > INT_MAX)
  739. fz_throw(ctx, FZ_ERROR_LIMIT, "Signature length too large");
  740. bsig = BIO_new_mem_buf(sig, (int)sig_len);
  741. pk7sig = d2i_PKCS7_bio(bsig, NULL);
  742. if (pk7sig == NULL)
  743. goto exit;
  744. sk = PKCS7_get_signer_info(pk7sig);
  745. if (sk == NULL || sk_PKCS7_SIGNER_INFO_num(sk) <= 0)
  746. goto exit;
  747. x509 = pk7_signer(pk7_certs(pk7sig), sk_PKCS7_SIGNER_INFO_value(sk, 0));
  748. fz_try(ctx)
  749. name = x509_distinguished_name(ctx, x509);
  750. fz_catch(ctx)
  751. {
  752. PKCS7_free(pk7sig);
  753. BIO_free(bsig);
  754. fz_rethrow(ctx);
  755. }
  756. exit:
  757. PKCS7_free(pk7sig);
  758. BIO_free(bsig);
  759. return name;
  760. }
  761. static void
  762. drop_openssl_verifier(fz_context *ctx, pdf_pkcs7_verifier *verifier)
  763. {
  764. fz_free(ctx, verifier);
  765. }
  766. pdf_pkcs7_verifier *
  767. pkcs7_openssl_new_verifier(fz_context *ctx)
  768. {
  769. openssl_verifier *verifier = NULL;
  770. verifier = fz_malloc_struct(ctx, openssl_verifier);
  771. verifier->base.drop = drop_openssl_verifier;
  772. verifier->base.check_digest = check_digest;
  773. verifier->base.check_certificate = check_certificate;
  774. verifier->base.get_signatory = get_signatory;
  775. return &verifier->base;
  776. }
  777. #endif