forked from siemens/gencmpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmpClient.c
2596 lines (2356 loc) · 95.3 KB
/
cmpClient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* @file cmpClient.c
* @brief generic CMP client library demo/test client
*
* @author David von Oheimb, Siemens AG, [email protected]
*
* Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
* Copyright Nokia 2007-2019
* Copyright (c) 2015-2023 Siemens AG
*
* Licensed under the Apache License 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You can obtain a copy in the file LICENSE in the source distribution
* or at https://www.openssl.org/source/license.html
* SPDX-License-Identifier: Apache-2.0
*/
#include <genericCMPClient.h>
#include <openssl/ssl.h>
#include <secutils/config/config.h>
#include <secutils/credentials/cert.h>
#include <secutils/credentials/verify.h>
#include <secutils/certstatus/crl_mgmt.h> /* for CRLMGMT_load_crl_cb */
#ifdef LOCAL_DEFS
# include "genericCMPClient_use.h"
#endif
/*
* Use cases are split between CMP use cases and others,
* which do not use CMP and therefore do not need its complex setup.
*/
enum use_case { no_use_case,
/* CMP use cases: */
imprint, bootstrap, pkcs10, update,
revocation /* 'revoke' already defined in unistd.h */, genm,
default_case,
/* Non-CMP use cases: */
validate
};
#define RSA_SPEC "RSA:2048"
#define ECC_SPEC "EC:prime256v1"
#define CONFIG_DEFAULT "config/demo.cnf"
#define CONFIG_TEST "test_config.cnf" /* from OpenSSL test suite */
char *opt_config = CONFIG_DEFAULT; /* OpenSSL-style configuration file */
CONF *config = NULL; /* OpenSSL configuration structure */
char *opt_section = "EJBCA"; /* name(s) of config file section(s) to use */
#define DEFAULT_SECTION "default"
#define SECTION_NAME_MAX 40
char demo_sections[2 * (SECTION_NAME_MAX + 1)]; /* used for pattern "%s,%s" */
long opt_verbosity;
/* message transfer */
const char *opt_server;
const char *opt_proxy;
const char *opt_no_proxy;
const char *opt_path;
const char *opt_cdp_proxy;
const char *opt_crl_cache_dir;
long opt_keep_alive;
long opt_msg_timeout;
long opt_total_timeout;
/* server authentication */
const char *opt_trusted;
const char *opt_untrusted;
const char *opt_srvcert;
const char *opt_recipient;
const char *opt_expect_sender;
bool opt_ignore_keyusage;
bool opt_unprotected_errors;
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
bool opt_no_cache_extracerts;
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30200000L || defined USE_LIBCMP
const char *opt_srvcertout;
#endif
const char *opt_extracertsout;
const char *opt_extracerts_dir;
const char *opt_extracerts_dir_format;
const char *opt_cacertsout;
const char *opt_cacerts_dir;
const char *opt_cacerts_dir_format;
const char *opt_oldwithold;
const char *opt_newwithnew;
const char *opt_newwithold;
const char *opt_oldwithnew;
const char *opt_template;
const char *opt_oldcrl;
const char *opt_crlout;
/* client authentication */
const char *opt_ref;
const char *opt_secret;
/* maybe it would be worth re-adding a -creds option combining -cert and -key */
const char *opt_cert;
const char *opt_own_trusted;
const char *opt_key;
const char *opt_keypass;
const char *opt_digest;
const char *opt_mac;
const char *opt_extracerts;
bool opt_unprotected_requests;
/* generic message */
const char *opt_cmd;
const char *opt_infotype;
static int infotype = NID_undef;
const char *opt_profile;
char *opt_geninfo;
/* certificate enrollment */
const char *opt_newkeytype;
bool opt_centralkeygen;
const char *opt_newkey;
const char *opt_newkeypass;
const char *opt_subject;
long opt_days;
const char *opt_reqexts;
char *opt_sans;
bool opt_san_nodefault;
const char *opt_policies;
char *opt_policy_oids;
bool opt_policy_oids_critical;
long opt_popo;
const char *opt_csr;
const char *opt_out_trusted;
bool opt_implicit_confirm;
bool opt_disable_confirm;
const char *opt_certout;
const char *opt_chainout;
/* certificate enrollment and revocation */
const char *opt_oldcert;
long opt_revreason;
const char *opt_issuer;
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
char *opt_serial;
#endif
/* TODO? add credentials format options */
/* TODO add opt_engine */
/* TLS connection */
bool opt_tls_used;
/* TODO re-add tls_creds */
const char *opt_tls_cert;
const char *opt_tls_key;
const char *opt_tls_keypass;
const char *opt_tls_extra;
const char *opt_tls_trusted;
const char *opt_tls_host;
/* client-side debugging */
static char *opt_reqin = NULL;
static bool opt_reqin_new_tid = 0;
static char *opt_reqout = NULL;
static char *opt_rspin = NULL;
static char *opt_rspout = NULL;
/* TODO further extend verification options and align with OpenSSL:apps/cmp.c */
bool opt_check_all;
bool opt_check_any;
const char *opt_crls;
bool opt_use_cdp;
const char *opt_cdps;
long opt_crls_timeout;
size_t opt_crl_maxdownload_size;
bool opt_use_aia;
const char *opt_ocsp;
long opt_ocsp_timeout;
bool opt_ocsp_last;
bool opt_stapling;
X509_VERIFY_PARAM *vpm = NULL;
CRLMGMT_DATA *cmdata = NULL;
STACK_OF(X509_CRL) *crls = NULL;
opt_t cmp_opts[] = {
{ "help", OPT_BOOL, {.num = -1}, { NULL },
"Display this summary"},
{ "config", OPT_TXT, {.txt = NULL}, { NULL },
"Configuration file to use. \"\" means none. Default 'config/demo.cnf'"},
{ "section", OPT_TXT, {.txt = NULL}, { NULL },
"Section(s) in config file to use. \"\" means 'default'. Default 'EJBCA'"},
{ "verbosity", OPT_NUM, {.num = LOG_INFO}, {(const char **) &opt_verbosity},
"Logging level; 3=ERR, 4=WARN, 6=INFO, 7=DEBUG, 8=TRACE. Default 6 = INFO"},
OPT_HEADER("Generic message"),
{ "cmd", OPT_TXT, {.txt = NULL}, { &opt_cmd },
"CMP request to send: ir/cr/p10cr/kur/rr/genm. Overrides 'use_case' if given"},
{ "infotype", OPT_TXT, {.txt = NULL}, { &opt_infotype },
"InfoType name for requesting specific info in genm, "
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
"with specific support"
#else
"e.g., C<signKeyPairTypes>"
#endif
},
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
OPT_MORE("for 'caCerts', 'rootCaCert', 'certReqTemplate', and 'crlStatusList'"),
#endif
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
{ "profile", OPT_TXT, {.txt = NULL}, { &opt_profile },
"Cert profile name to place in generalInfo field of PKIHeader of requests"},
#endif
{ "geninfo", OPT_TXT, {.txt = NULL}, { (const char **)&opt_geninfo },
"Comma-separated list of OID and value to place in generalInfo PKIHeader"},
OPT_MORE("of form <OID>:int:<n> or <OID>:str:<s>, e.g. \'1.2.3.4:int:56789, id-kp:str:name'"),
{ "template", OPT_TXT, {.txt = NULL}, { &opt_template },
"File to save certTemplate received in genp of type certReqTemplate"},
OPT_HEADER("Certificate enrollment"),
{ "newkeytype", OPT_TXT, {.txt = NULL}, { &opt_newkeytype },
"Generate or request key for ir/cr/kur of given type, e.g., EC:secp521r1"},
{ "centralkeygen", OPT_BOOL, {.bit = false},
{ (const char **) &opt_centralkeygen},
"Request central (server-side) key generation. Default is local generation"},
{ "newkey", OPT_TXT, {.txt = NULL}, { &opt_newkey },
"Private or public key for for ir/cr/kur (defaulting to pubkey of -csr) if -newkeytype not given."},
OPT_MORE("File to save new key if -newkeytype is given"),
{ "newkeypass", OPT_TXT, {.txt = NULL}, { &opt_newkeypass },
"Pass phrase source for -newkey"},
{ "subject", OPT_TXT, {.txt = NULL}, { &opt_subject },
"Distinguished Name (DN) of subject to use in the requested cert template"},
OPT_MORE("For kur, default is subject of -csr arg, else subject of -oldcert"),
{ "days", OPT_NUM, {.num = 0}, { (const char **) &opt_days },
"Requested validity time of new cert in number of days"},
{ "reqexts", OPT_TXT, {.txt = NULL}, { &opt_reqexts },
"Name of config file section defining certificate request extensions"},
OPT_MORE("Augments or replaces any extensions contained CSR given with -csr"),
{ "sans", OPT_TXT, {.txt = NULL}, { (const char **) &opt_sans },
"Subject Alt Names (IPADDR/DNS/URI) to add as (critical) cert req extension"},
{ "san_nodefault", OPT_BOOL, {.bit = false},
{ (const char **) &opt_san_nodefault},
"Do not take default SANs from reference certificate (see -oldcert)"},
{ "policies", OPT_TXT, {.txt = NULL}, { &opt_policies},
"Name of config file section defining policies request extension"},
{ "policy_oids", OPT_TXT, {.txt = NULL}, {(const char **) &opt_policy_oids},
"Policy OID(s) to add as certificate policies request extension"},
{ "policy_oids_critical", OPT_BOOL, {.bit = false},
{ (const char **) &opt_policy_oids_critical},
"Flag the policy OID(s) given with -policies_ as critical"},
{ "popo", OPT_NUM, {.num = OSSL_CRMF_POPO_NONE - 1},
{ (const char **) &opt_popo },
"Proof-of-Possession (POPO) method to use for ir/cr/kur where"},
OPT_MORE("-1 = NONE, 0 = RAVERIFIED, 1 = SIGNATURE (default), 2 = KEYENC"),
{ "csr", OPT_TXT, {.txt = NULL}, { &opt_csr },
"CSR file in PKCS#10 format to convert or to use in p10cr"},
{ "out_trusted", OPT_TXT, {.txt = NULL}, { &opt_out_trusted },
"Certs to trust when validating newly enrolled certs; defaults to -srvcert"},
{ "implicit_confirm", OPT_BOOL, {.bit = false},
{ (const char **) &opt_implicit_confirm },
"Request implicit confirmation of newly enrolled certificates"},
{ "disable_confirm", OPT_BOOL, {.bit = false},
{ (const char **) &opt_disable_confirm },
"Do not confirm newly enrolled certificates w/o requesting implicit confirm"},
{ "certout", OPT_TXT, {.txt = NULL}, { &opt_certout },
"File to save newly enrolled certificate, possibly with chain and key"},
{ "chainout", OPT_TXT, {.txt = NULL}, { &opt_chainout },
"File to save the chain of the newly enrolled certificate"},
OPT_HEADER("Certificate enrollment and revocation"),
{ "oldcert", OPT_TXT, {.txt = NULL}, { &opt_oldcert },
"Certificate to be updated (defaulting to -cert) or to be revoked in rr;"},
OPT_MORE("also used as reference (defaulting to -cert) for subject DN and SANs."),
OPT_MORE("Its issuer used as recipient unless -srvcert, -recipient or -issuer given"),
OPT_MORE("It is also used for CRLSource data in genm of type crlStatusList"),
{ "revreason", OPT_NUM, {.num = CRL_REASON_NONE},
{ (const char **) &opt_revreason },
"Reason code to include in revocation request (rr)."},
OPT_MORE("Values: 0..6, 8..10 (see RFC5280, 5.3.1) or -1. Default -1 = none included"),
{ "issuer", OPT_TXT, {.txt = NULL}, { &opt_issuer },
"DN of the issuer to place in the certificate template of ir/cr/kur"
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
"/rr"
#else
""
#endif
";"},
OPT_MORE("also used as recipient if neither -recipient nor -srvcert are given"),
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
{ "serial", OPT_TXT, {.txt = NULL}, {(const char **) &opt_serial},
"Serial number of certificate to be revoked in revocation request (rr)"},
#endif
/* Note: Lightweight CMP Profile SimpleLra does not allow CRL_REASON_NONE */
/* TODO? OPT_HEADER("Credentials format"), */
/* TODO add opt_engine */
OPT_HEADER("Message transfer"),
{ "server", OPT_TXT, {.txt = NULL}, { &opt_server },
"[http[s]://]host[:port][/path] of CMP server. Default port 80 or 443."},
OPT_MORE("host may be a DNS name or an IP address; path can be overridden by -path"),
{ "proxy", OPT_TXT, {.txt = NULL}, { &opt_proxy },
"[http[s]://]host[:port][/p] of proxy. Default port 80 or 443; p ignored."},
OPT_MORE("Default from environment variable 'http_proxy', else 'HTTP_PROXY'"),
{ "no_proxy", OPT_TXT, {.txt = NULL}, { &opt_no_proxy },
"List of addresses of servers not use HTTP(S) proxy for."},
OPT_MORE("Default from environment variable 'no_proxy', else 'NO_PROXY', else none"),
{ "recipient", OPT_TXT, {.txt = NULL}, { &opt_recipient },
"DN of CA. Default: -srvcert subject, -issuer, issuer of -oldcert or -cert,"},
OPT_MORE("subject of the first -untrusted cert if any, or else the NULL-DN"),
{ "path", OPT_TXT, {.txt = NULL}, { &opt_path },
"HTTP path (aka CMP alias) at the CMP server. Default from -server, else \"/\""},
{"keep_alive", OPT_NUM, {.num = 1 }, { (const char **)&opt_keep_alive },
"Persistent HTTP connections. 0: no, 1 (the default): request, 2: require"},
{ "msg_timeout", OPT_NUM, {.num = 120}, { (const char **)&opt_msg_timeout },
"Timeout per CMP message round trip (or 0 for none). Default 120 seconds"},
{ "total_timeout", OPT_NUM, {.num = 0}, {(const char **)&opt_total_timeout},
"Overall time an enrollment incl. polling may take. Default: 0 = infinite"},
OPT_HEADER("Server authentication"),
{ "trusted", OPT_TXT, {.txt = NULL}, { &opt_trusted },
"Certificates to use as trust anchors when validating signed CMP responses"},
{ "untrusted", OPT_TXT, {.txt = NULL}, { &opt_untrusted },
"Intermediate CA certs for chain construction for CMP/TLS/enrolled certs"},
{ "srvcert", OPT_TXT, {.txt = NULL}, { &opt_srvcert },
"Server cert to pin and trust directly when validating signed CMP responses"},
{ "expect_sender", OPT_TXT, {.txt = NULL}, { &opt_expect_sender },
"DN of expected sender of responses. Defaults to subject of -srvcert, if any"},
{ "ignore_keyusage", OPT_BOOL, {.bit = false},
{ (const char **)&opt_ignore_keyusage },
"Ignore CMP signer cert key usage, else 'digitalSignature' must be allowed"},
{ "unprotected_errors", OPT_BOOL, {.bit = false},
{ (const char **) &opt_unprotected_errors },
"Accept missing or invalid protection of regular error messages and negative"},
OPT_MORE("certificate responses (ip/cp/kup), revocation responses (rp), and PKIConf"),
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
{ "no_cache_extracerts", OPT_BOOL, {.bit = false},
{ (const char **) &opt_no_cache_extracerts },
"Do not keep certificates received in the extraCerts CMP message field"},
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30200000L || defined USE_LIBCMP
{ "srvcertout", OPT_TXT, {.txt = NULL}, { &opt_srvcertout },
"File to save server cert used and validated for CMP response protection"},
#endif
{ "extracertsout", OPT_TXT, {.txt = NULL}, { &opt_extracertsout },
"File to save extra certificates received in the extraCerts field"},
{ "extracerts_dir", OPT_TXT, {.txt = NULL}, { &opt_extracerts_dir },
"Path to save not self-issued extra certs received in the extraCerts field"},
{ "extracerts_dir_format", OPT_TXT, {.txt = "pem"},
{ &opt_extracerts_dir_format },
"Format to use for saving those certs. Default \"pem\""},
{ "cacertsout", OPT_TXT, {.txt = NULL}, { &opt_cacertsout },
"File to save certificates received in caPubs field or genp of type caCerts"},
{ "cacerts_dir", OPT_TXT, {.txt = NULL}, { &opt_cacerts_dir },
"Path to save self-issued CA certs received in the caPubs field"},
{ "cacerts_dir_format", OPT_TXT, {.txt = "pem"},
{ &opt_cacerts_dir_format },
"Format to use for saving those certs. Default \"pem\""},
#if OPENSSL_VERSION_NUMBER > 0x30200000L || defined USE_LIBCMP
{ "oldwithold", OPT_TXT, {.txt = NULL}, { &opt_oldwithold },
"Root CA certificate to request update for in genm of type rootCaCert"},
{ "newwithnew", OPT_TXT, {.txt = NULL}, { &opt_newwithnew },
"File to save NewWithNew cert received in genp of type rootCaKeyUpdate"},
{ "newwithold", OPT_TXT, {.txt = NULL}, { &opt_newwithold },
"File to save NewWithOld cert received in genp of type rootCaKeyUpdate"},
{ "oldwithnew", OPT_TXT, {.txt = NULL}, { &opt_oldwithnew },
"File to save OldWithNew cert received in genp of type rootCaKeyUpdate"},
{ "oldcrl", OPT_TXT, {.txt = NULL}, { &opt_oldcrl },
"CRL to request update for in genm of type crlStatusList"},
{ "crlout", OPT_TXT, {.txt = NULL}, { &opt_crlout },
"File to save new CRL received in genp of type 'crls'"},
#endif
OPT_HEADER("Client authentication and protection"),
{ "ref", OPT_TXT, {.txt = NULL}, { &opt_ref },
"Reference value to use as senderKID in case no -cert is given"},
{ "secret", OPT_TXT, {.txt = NULL}, { &opt_secret },
"Source of secret value for authentication with a pre-shared key (PBM)"},
{ "cert", OPT_TXT, {.txt = NULL}, { &opt_cert },
"Client cert (plus any extra one), needed unless using -secret for PBM."},
OPT_MORE("This also used as default reference for subject DN and SANs."),
OPT_MORE("Any further certs included are appended to the untrusted certs"),
{ "own_trusted", OPT_TXT, {.txt = NULL}, { &opt_own_trusted },
"Optional certs to validate chain building for own CMP signer cert"},
{ "key", OPT_TXT, {.txt = NULL}, { &opt_key },
"Key for the client certificate to use for protecting requests"},
{ "keypass", OPT_TXT, {.txt = NULL}, { &opt_keypass },
"Pass phrase source for the client -key, -cert, and -oldcert"},
{ "digest", OPT_TXT, {.txt = NULL}, { &opt_digest },
"Digest alg to use in msg protection and POPO signatures. Default \"sha256\""},
{ "mac", OPT_TXT, {.txt = NULL}, { &opt_mac},
"MAC algorithm to use in PBM-based message protection. Default \"hmac-sha1\""},
{ "extracerts", OPT_TXT, {.txt = NULL}, { &opt_extracerts },
"File(s) with certificates to append in extraCerts field of outgoing messages."},
OPT_MORE("This can be used as the default CMP signer cert chain to include"),
{ "unprotected_requests", OPT_BOOL, {.bit = false},
{ (const char **) &opt_unprotected_requests },
"Send request messages without CMP-level protection"},
OPT_HEADER("TLS connection"),
{ "tls_used", OPT_BOOL, {.bit = false}, { (const char **) &opt_tls_used },
"Enable using TLS (also when other TLS options are not set)"},
{ "tls_cert", OPT_TXT, {.txt = NULL}, { &opt_tls_cert },
"Client certificate (plus any extra certs) for TLS connection"},
{ "tls_key", OPT_TXT, {.txt = NULL}, { &opt_tls_key },
"Client private key for TLS connection"},
{ "tls_keypass", OPT_TXT, {.txt = NULL}, { &opt_tls_keypass },
"Client key and cert pass phrase source for TLS connection"},
{ "tls_extra", OPT_TXT, {.txt = NULL}, { &opt_tls_extra },
"Extra certificates to provide to TLS server during TLS handshake"},
{ "tls_trusted", OPT_TXT, {.txt = NULL}, { &opt_tls_trusted },
"File(s) with certs to trust for TLS server verification (TLS trust anchor)"},
{ "tls_host", OPT_TXT, {.txt = NULL}, { &opt_tls_host },
"Address (rather than -server) to be checked during TLS hostname validation"},
OPT_HEADER("Debugging"),
{"reqin", OPT_TXT, {.txt = NULL}, { (const char **) &opt_reqin},
"Take sequence of CMP requests to send to server from file(s)"},
{"reqin_new_tid", OPT_BOOL, {.bit = false},
{ (const char **) &opt_reqin_new_tid},
"Use fresh transactionID for CMP requests read from -reqin"},
{"reqout", OPT_TXT, {.txt = NULL}, { (const char **) &opt_reqout},
"Save sequence of CMP requests to file(s)"},
{"rspin", OPT_TXT, {.txt = NULL}, { (const char **) &opt_rspin},
"Process sequence of CMP responses provided in file(s), skipping server"},
{"rspout", OPT_TXT, {.txt = NULL}, { (const char **) &opt_rspout},
"Save sequence of CMP responses to file(s)"},
OPT_HEADER("CMP and TLS certificate status checking"),
/* TODO extend verification options and align with OpenSSL:apps/cmp.c */
{ "check_all", OPT_BOOL, {.bit = false}, { (const char **) &opt_check_all},
"Check status not only for leaf certs but for all certs (except root)"},
{ "check_any", OPT_BOOL, {.bit = false}, { (const char **) &opt_check_any},
"Check status for those certs (except root) that contain a CDP or AIA entry"},
{ "crls", OPT_TXT, {.txt = NULL}, {&opt_crls},
"Enable CRL-based status checking and first use CRLs from given file/URL(s)"},
{ "use_cdp", OPT_BOOL, {.bit = false}, { (const char **) &opt_use_cdp },
"Enable CRL-based status checking and enable using any CDP entries in certs"},
{ "cdps", OPT_TXT, {.txt = NULL}, {&opt_cdps},
"Enable CRL-based status checking and use given URL(s) as fallback CDP"},
{ "cdp_proxy", OPT_TXT, {.txt = NULL}, { &opt_cdp_proxy },
"URL of the proxy server to send CDP URLs or cert isser names to"},
{ "crl_cache_dir", OPT_TXT, {.txt = NULL}, { &opt_crl_cache_dir },
"Directory where to cache CRLs downloaded during verification."},
{ "crls_timeout", OPT_NUM, {.num = -1}, {(const char **)&opt_crls_timeout },
"Timeout for CRL fetching, or 0 for none, -1 for default: 10 seconds"},
{ "crl_maxdownload_size", OPT_NUM, {.num = 0},
{ (const char **)&opt_crl_maxdownload_size},
"Maximum size of a CRL to be downloaded. Default: 0 = OpenSSL default = 100 kiB"},
{ "use_aia", OPT_BOOL, {.bit = false}, { (const char **) &opt_use_aia },
"Enable OCSP-based status checking and enable using any AIA entries in certs"},
{ "ocsp", OPT_TXT, {.txt = NULL}, {&opt_ocsp},
"Enable OCSP-based status checking and use given OCSP responder(s) as fallback"},
{ "ocsp_timeout", OPT_NUM, {.num = -1}, {(const char **)&opt_ocsp_timeout },
"Timeout for getting OCSP responses, or 0 for none, -1 for default: 10 seconds"},
{ "ocsp_last", OPT_BOOL, {.bit = false}, { (const char **) &opt_ocsp_last },
"Do OCSP-based status checks last (else before using CRLs downloaded from CDPs)"},
{ "stapling", OPT_BOOL, {.bit = false}, { (const char **) &opt_stapling },
"Enable OCSP stapling for TLS; is tried before any other cert status checks"},
OPT_V_OPTIONS, /* excludes "crl_check" and "crl_check_all" */
OPT_END
};
#ifndef SECUTILS_NO_TLS
static int SSL_CTX_add_extra_chain_free(SSL_CTX *ssl_ctx, STACK_OF(X509) *certs)
{
int i;
int res = 1;
for (i = 0; i < sk_X509_num(certs); i++) {
if (res != 0)
res = SSL_CTX_add_extra_chain_cert(ssl_ctx,
sk_X509_value(certs, i)) != 0;
}
sk_X509_free(certs); /* must not free the stack elements */
if (res == 0)
LOG_err("Unable to use TLS extra certs");
return res;
}
#endif
static int set_gennames(OSSL_CMP_CTX *ctx, char *names, const char *desc)
{
char *next;
GENERAL_NAME *n;
for (; names != NULL; names = next) {
next = UTIL_next_item(names);
if (strcmp(names, "critical") == 0) {
(void)OSSL_CMP_CTX_set_option(ctx,
OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL,
1);
continue;
}
/* try IP address first, then email/URI/domain name */
(void)ERR_set_mark();
n = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_IPADD, names, 0);
if (n == NULL)
n = a2i_GENERAL_NAME(NULL, NULL, NULL,
strchr(names, '@') != NULL ? GEN_EMAIL :
strchr(names, ':') != NULL ? GEN_URI : GEN_DNS,
names, 0);
(void)ERR_pop_to_mark();
if (n == NULL) {
LOG(FL_ERR, "bad syntax of %s '%s'", desc, names);
return 0;
}
if (!OSSL_CMP_CTX_push1_subjectAltName(ctx, n)) {
GENERAL_NAME_free(n);
LOG_err("Out of memory");
return 0;
}
GENERAL_NAME_free(n);
}
return 1;
}
static SSL_CTX *setup_TLS(STACK_OF(X509) *untrusted_certs)
{
#ifdef SECUTILS_NO_TLS
(void)untrusted_certs;
LOG_err("TLS is not enabled in this build");
return NULL;
#else
CREDENTIALS *tls_creds = NULL;
SSL_CTX *tls = NULL;
X509_STORE *tls_trust = NULL;
static const char *tls_ciphers = NULL;
/* or, e.g., "HIGH:!ADH:!LOW:!EXP:!MD5:@STRENGTH"; */
const int security_level = -1;
if (opt_tls_trusted != NULL) {
tls_trust = STORE_load(opt_tls_trusted, "trusted certs for TLS level",
NULL /* no vpm: prevent strict checking */);
if (tls_trust == NULL)
goto err;
if (!STORE_set_parameters(tls_trust, vpm,
opt_check_all, opt_stapling, crls,
opt_use_cdp, opt_cdps, (int)opt_crls_timeout,
opt_use_aia, opt_ocsp, (int)opt_ocsp_timeout))
goto err;
if (!STORE_set_crl_callback(tls_trust, CRLMGMT_load_crl_cb, cmdata))
goto err;
} else {
LOG_warn("-tls_used given without -tls_trusted; will not authenticate the TLS server");
}
if (opt_tls_cert != NULL || opt_tls_key != NULL
|| opt_tls_keypass != NULL) {
if (opt_tls_key == NULL) {
LOG_err("missing -tls_key option");
goto err;
} else if (opt_tls_cert == NULL) {
LOG_err("missing -tls_cert option");
goto err;
}
}
if (opt_tls_key != NULL) {
tls_creds = CREDENTIALS_load(opt_tls_cert, opt_tls_key, opt_tls_keypass,
"credentials for TLS level");
if (tls_creds == NULL)
goto err;
} else {
LOG_warn("-tls_used given without -tls_key; cannot authenticate to the TLS server");
}
tls = TLS_new(tls_trust, untrusted_certs, tls_creds, tls_ciphers,
security_level);
if (tls == NULL)
goto err;
/*
* Enable and parameterize server hostname/IP address check.
* If we did this before checking our own TLS cert in TLS_new(),
* the expected hostname would mislead the check.
*/
if (tls_trust != NULL) {
const char *host = opt_tls_host != NULL ? opt_tls_host : opt_server;
if (!STORE_set1_host_ip(tls_trust, host, host))
goto err;
}
/* If present we append to the list also the certs from opt_tls_extra */
if (opt_tls_extra != NULL) {
STACK_OF(X509) *tls_extra = CERTS_load(opt_tls_extra,
"extra certificates for TLS",
1 /* CA */, vpm);
if (tls_extra == NULL
|| !SSL_CTX_add_extra_chain_free(tls, tls_extra)) {
SSL_CTX_free(tls);
tls = NULL;
goto err;
}
}
err:
STORE_free(tls_trust);
CREDENTIALS_free(tls_creds);
return tls;
#endif
}
static X509_STORE *setup_CMP_truststore(const char *trusted_cert_files)
{
if (trusted_cert_files == NULL)
return NULL;
X509_STORE *cmp_truststore =
STORE_load(trusted_cert_files, "trusted certs for CMP level",
NULL /* no vpm: prevent strict checking */);
if (cmp_truststore == NULL)
goto err;
if (!STORE_set_parameters(cmp_truststore, vpm,
opt_check_all, false /* stapling */, crls,
opt_use_cdp, opt_cdps, (int)opt_crls_timeout,
opt_use_aia, opt_ocsp, (int)opt_ocsp_timeout) ||
!STORE_set_crl_callback(cmp_truststore, CRLMGMT_load_crl_cb, cmdata) ||
/* clear any expected host/ip/email address; use opt_expect_sender: */
!STORE_set1_host_ip(cmp_truststore, NULL, NULL)) {
STORE_free(cmp_truststore);
cmp_truststore = NULL;
}
err:
return cmp_truststore;
}
static X509_EXTENSIONS *setup_X509_extensions(CMP_CTX *ctx)
{
X509_EXTENSIONS *exts = sk_X509_EXTENSION_new_null();
X509V3_CTX ext_ctx;
if (exts == NULL)
return NULL;
if (opt_reqexts != NULL || opt_policies != NULL) {
X509V3_set_ctx(&ext_ctx, NULL, NULL, NULL, NULL, 0);
X509V3_set_nconf(&ext_ctx, config);
}
if (opt_reqexts != NULL) {
if (!X509V3_EXT_add_nconf_sk(config, &ext_ctx, opt_reqexts, &exts)) {
LOG(FL_ERR, "cannot load extension section '%s'", opt_reqexts);
goto err;
}
}
if (opt_policies != NULL) {
if (!X509V3_EXT_add_nconf_sk(config, &ext_ctx, opt_policies, &exts)) {
LOG(FL_ERR, "cannot load policy section '%s'", opt_policies);
goto err;
}
}
if (opt_policies != NULL && opt_policy_oids != NULL) {
LOG_err("Cannot have policies both via -policies and via -policy_oids");
goto err;
}
if (opt_policy_oids_critical) {
if (opt_policy_oids == NULL)
LOG_warn("-policy_oids_critical has no effect unless -policy_oids is given");
if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POLICIES_CRITICAL, 1)) {
LOG_err("Failed to set 'setPoliciesCritical' field of CMP context");
goto err;
}
}
while (opt_policy_oids != NULL) {
ASN1_OBJECT *policy;
POLICYINFO *pinfo;
char *next = UTIL_next_item(opt_policy_oids);
if ((policy = OBJ_txt2obj(opt_policy_oids, 0)) == NULL) {
LOG(FL_ERR, "unknown policy OID '%s'", opt_policy_oids);
goto err;
}
if ((pinfo = POLICYINFO_new()) == NULL) {
LOG_err("Out of memory");
ASN1_OBJECT_free(policy);
goto err;
}
pinfo->policyid = policy;
if (!OSSL_CMP_CTX_push0_policy(ctx, pinfo)) {
LOG(FL_ERR, "cannot add policy with OID '%s'", opt_policy_oids);
POLICYINFO_free(pinfo);
goto err;
}
opt_policy_oids = next;
}
return exts;
err:
EXTENSIONS_free(exts);
return NULL;
}
/*
* write OSSL_CMP_MSG DER-encoded in turn to the next element in
* the string of file names, which is consumed step by step
*/
static int write_PKIMESSAGE(const OSSL_CMP_MSG *msg, char **filenames)
{
char *file;
if (msg == NULL || filenames == NULL) {
LOG_err("NULL arg to write_PKIMESSAGE");
return 0;
}
if (*filenames == NULL) {
LOG_err("Not enough file names provided for writing PKIMessage");
return 0;
}
file = *filenames;
*filenames = UTIL_next_item(file);
if (OSSL_CMP_MSG_write(file, msg) < 0) {
LOG(FL_ERR, "Cannot write PKIMessage to file '%s'", file);
return 0;
}
return 1;
}
/* read DER-encoded OSSL_CMP_MSG from the specified file name item */
static OSSL_CMP_MSG *read_PKIMESSAGE(OSSL_CMP_CTX *ctx,
const char *desc, char **filenames)
{
char *file;
OSSL_CMP_MSG *ret;
if (filenames == NULL || desc == NULL) {
LOG_err("NULL arg to read_PKIMESSAGE");
return NULL;
}
if (*filenames == NULL) {
LOG_err("Not enough file names provided for reading PKIMessage");
return NULL;
}
file = *filenames;
*filenames = UTIL_next_item(file);
ret = OSSL_CMP_MSG_read(file, OSSL_CMP_CTX_get0_libctx(ctx),
OSSL_CMP_CTX_get0_propq(ctx));
if (ret == NULL)
LOG(FL_ERR, "Cannot read PKIMessage from file '%s'", file);
else
LOG(FL_INFO, "%s %s", desc, file);
return ret;
}
/*-
* Sends the PKIMessage req and on success place the response in *res
* basically like OSSL_CMP_MSG_http_perform(), but in addition allows
* to dump the sequence of requests and responses to files and/or
* to take the sequence of requests and responses from files.
*/
static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
const OSSL_CMP_MSG *req)
{
OSSL_CMP_MSG *req_new = NULL;
OSSL_CMP_MSG *res = NULL;
OSSL_CMP_PKIHEADER *hdr;
const char *prev_opt_rspin = opt_rspin;
if (opt_reqout != NULL && !write_PKIMESSAGE(req, &opt_reqout))
goto err;
if (opt_reqin != NULL && opt_rspin == NULL) {
if ((req_new = read_PKIMESSAGE(ctx,
"actually sending", &opt_reqin)) == NULL)
goto err;
/*-
* The transaction ID in req_new read from opt_reqin may not be fresh.
* In this case the server may complain "Transaction id already in use."
* The following workaround unfortunately requires re-protection.
*/
if (opt_reqin_new_tid
&& !OSSL_CMP_MSG_update_transactionID(ctx, req_new))
goto err;
/*
* Except for first request, need to satisfy recipNonce check by server.
* Unfortunately requires re-protection if the request was protected.
*/
#if OPENSSL_VERSION_NUMBER >= 0x30000090L || defined USE_LIBCMP
if (!OSSL_CMP_MSG_update_recipNonce(ctx, req_new))
goto err;
#endif
}
if (opt_rspin != NULL) {
res = read_PKIMESSAGE(ctx, "actually using", &opt_rspin);
} else {
const OSSL_CMP_MSG *actual_req = req_new != NULL ? req_new : req;
res =
#if 0 /* TODO add in case mock server functionality is included */
opt_use_mock_srv ? OSSL_CMP_CTX_server_perform(ctx, actual_req) :
#endif
OSSL_CMP_MSG_http_perform(ctx, actual_req);
}
if (res == NULL)
goto err;
if (req_new != NULL || prev_opt_rspin != NULL) {
/* need to satisfy nonce and transactionID checks by client */
ASN1_OCTET_STRING *nonce;
ASN1_OCTET_STRING *tid;
hdr = OSSL_CMP_MSG_get0_header(res);
nonce = OSSL_CMP_HDR_get0_recipNonce(hdr);
tid = OSSL_CMP_HDR_get0_transactionID(hdr);
if (!OSSL_CMP_CTX_set1_senderNonce(ctx, nonce)
|| !OSSL_CMP_CTX_set1_transactionID(ctx, tid)) {
OSSL_CMP_MSG_free(res);
res = NULL;
goto err;
}
}
if (opt_rspout != NULL && !write_PKIMESSAGE(res, &opt_rspout)) {
OSSL_CMP_MSG_free(res);
res = NULL;
}
err:
OSSL_CMP_MSG_free(req_new);
return res;
}
static int set_name(OPTIONAL const char *str,
int (*set_fn) (OSSL_CMP_CTX *ctx, const X509_NAME *name),
OSSL_CMP_CTX *ctx, const char *desc)
{
if (str != NULL) {
X509_NAME *n = UTIL_parse_name(str, MBSTRING_ASC, false);
if (n == NULL) {
LOG(FL_ERR, "cannot parse %s DN '%s'", desc, str);
return -03;
}
if (!(*set_fn) (ctx, n)) {
X509_NAME_free(n);
LOG_err("Out of memory");
return -04;
}
X509_NAME_free(n);
}
return CMP_OK;
}
static int setup_cert_template(CMP_CTX *ctx)
{
int err;
err = set_name(opt_issuer, OSSL_CMP_CTX_set1_issuer, ctx, "issuer");
if (err != CMP_OK)
goto err;
if (opt_san_nodefault) {
if (opt_sans != NULL)
LOG_warn("-san_nodefault has no effect when -sans is used");
if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT,
1)) {
LOG_err("Failed to set 'SubjectAltName_nodefault' field of CMP context");
goto err;
}
}
if (!set_gennames(ctx, opt_sans, "Subject Alternative Name")) {
LOG_err("Failed to set 'Subject Alternative Name' of CMP context");
err = -39;
goto err;
}
err:
return err;
}
static int handle_opt_geninfo(OSSL_CMP_CTX *ctx)
{
ASN1_OBJECT *obj = NULL;
ASN1_TYPE *type = NULL;
long value;
ASN1_INTEGER *aint = NULL;
ASN1_UTF8STRING *text = NULL;
OSSL_CMP_ITAV *itav;
char *ptr = opt_geninfo, *oid, *end;
int ret = -28;
do {
while (isspace(*ptr))
ptr++;
oid = ptr;
if ((ptr = strchr(oid, ':')) == NULL) {
LOG(FL_ERR, "Missing ':' in -geninfo arg %.40s", oid);
return CMP_R_INVALID_ARGS;
}
*ptr++ = '\0';
if ((obj = OBJ_txt2obj(oid, 0)) == NULL) {
LOG(FL_ERR, "Cannot parse OID in -geninfo arg %.40s", oid);
return CMP_R_INVALID_ARGS;
}
if ((type = ASN1_TYPE_new()) == NULL) {
LOG_err("Out of memory");
goto err;
}
if (strncmp(ptr, "int:", 4) == 0) {
value = strtol(ptr += 4, &end, 10);
if (end == ptr) {
LOG(FL_ERR, "Cannot parse int in -geninfo arg %.40s", ptr);
ret = CMP_R_INVALID_ARGS;
goto err;
}
ptr = end;
if (*ptr != '\0') {
if (*ptr != ',') {
LOG(FL_ERR, "Missing ',' or end of -geninfo arg after int at %.40s",
ptr);
ret = CMP_R_INVALID_ARGS;
goto err;
}
ptr++;
}
if ((aint = ASN1_INTEGER_new()) == NULL
|| !ASN1_INTEGER_set(aint, value)) {
LOG_err("Out of memory");
goto err;
}
ASN1_TYPE_set(type, V_ASN1_INTEGER, aint);
aint = NULL;
} else if (strncmp(ptr, "str:", 4) == 0) {
end = strchr(ptr += 4, ',');
if (end == NULL)
end = ptr + strlen(ptr);
else
*end++ = '\0';
if ((text = ASN1_UTF8STRING_new()) == NULL
|| !ASN1_STRING_set(text, ptr, -1)) {
LOG_err("Out of memory");
goto err;
}
ptr = end;
ASN1_TYPE_set(type, V_ASN1_UTF8STRING, text);
text = NULL;
} else {
LOG(FL_ERR, "Missing 'int:' or 'str:' in -geninfo arg %.40s", ptr);
ret = CMP_R_INVALID_ARGS;
goto err;
}
if ((itav = OSSL_CMP_ITAV_create(obj, type)) == NULL) {
LOG_err("Unable to create 'OSSL_CMP_ITAV' structure");
goto err;
}
obj = NULL;
type = NULL;
if (!OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav)) {
LOG_err("Failed to add ITAV for geninfo of the PKI message header");
OSSL_CMP_ITAV_free(itav);
return -10;
}
} while (*ptr != '\0');
return CMP_OK;
err:
ASN1_OBJECT_free(obj);
ASN1_TYPE_free(type);
ASN1_INTEGER_free(aint);
ASN1_UTF8STRING_free(text);
return ret;
}
static int setup_ctx(CMP_CTX *ctx)
{
CMP_err err = set_name(opt_expect_sender, OSSL_CMP_CTX_set1_expected_sender,
ctx, "expected sender");
if (err != CMP_OK)
return err;
err = CMP_R_INVALID_ARGS;
if (!OSSL_CMP_CTX_set_log_verbosity(ctx, (int)opt_verbosity))
return err;
if (opt_extracerts != NULL) {
STACK_OF(X509) *certs =