comparison mupdf-source/source/helpers/pkcs7/pkcs7-openssl.c @ 2:b50eed0cc0ef upstream

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