/
tstclnt.c
2233 lines (1998 loc) · 72.9 KB
/
tstclnt.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
**
** Sample client side test program that uses SSL and NSS
**
*/
#include "secutil.h"
#include "basicutil.h"
#if defined(XP_UNIX)
#include <unistd.h>
#else
#include <ctype.h> /* for isalpha() */
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include "nspr.h"
#include "prio.h"
#include "prnetdb.h"
#include "nss.h"
#include "nssb64.h"
#include "ocsp.h"
#include "ssl.h"
#include "sslproto.h"
#include "sslexp.h"
#include "pk11func.h"
#include "secmod.h"
#include "plgetopt.h"
#include "plstr.h"
#if defined(WIN32)
#include <fcntl.h>
#include <io.h>
#endif
#define PRINTF \
if (verbose) \
printf
#define FPRINTF \
if (verbose) \
fprintf
#define MAX_WAIT_FOR_SERVER 600
#define WAIT_INTERVAL 100
#define ZERO_RTT_MAX (2 << 16)
#define EXIT_CODE_HANDSHAKE_FAILED 254
#define EXIT_CODE_SIDECHANNELTEST_GOOD 0
#define EXIT_CODE_SIDECHANNELTEST_BADCERT 1
#define EXIT_CODE_SIDECHANNELTEST_NODATA 2
#define EXIT_CODE_SIDECHANNELTEST_REVOKED 3
PRIntervalTime maxInterval = PR_INTERVAL_NO_TIMEOUT;
int ssl3CipherSuites[] = {
-1, /* SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA* a */
-1, /* SSL_FORTEZZA_DMS_WITH_RC4_128_SHA * b */
TLS_RSA_WITH_RC4_128_MD5, /* c */
TLS_RSA_WITH_3DES_EDE_CBC_SHA, /* d */
TLS_RSA_WITH_DES_CBC_SHA, /* e */
-1, /* TLS_RSA_EXPORT_WITH_RC4_40_MD5 * f */
-1, /* TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 * g */
-1, /* SSL_FORTEZZA_DMS_WITH_NULL_SHA * h */
TLS_RSA_WITH_NULL_MD5, /* i */
-1, /* SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA * j */
-1, /* SSL_RSA_FIPS_WITH_DES_CBC_SHA * k */
-1, /* TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA * l */
-1, /* TLS_RSA_EXPORT1024_WITH_RC4_56_SHA * m */
TLS_RSA_WITH_RC4_128_SHA, /* n */
TLS_DHE_DSS_WITH_RC4_128_SHA, /* o */
TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, /* p */
TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA, /* q */
TLS_DHE_RSA_WITH_DES_CBC_SHA, /* r */
TLS_DHE_DSS_WITH_DES_CBC_SHA, /* s */
TLS_DHE_DSS_WITH_AES_128_CBC_SHA, /* t */
TLS_DHE_RSA_WITH_AES_128_CBC_SHA, /* u */
TLS_RSA_WITH_AES_128_CBC_SHA, /* v */
TLS_DHE_DSS_WITH_AES_256_CBC_SHA, /* w */
TLS_DHE_RSA_WITH_AES_256_CBC_SHA, /* x */
TLS_RSA_WITH_AES_256_CBC_SHA, /* y */
TLS_RSA_WITH_NULL_SHA, /* z */
0
};
unsigned long __cmp_umuls;
PRBool verbose;
int dumpServerChain = 0;
int renegotiationsToDo = 0;
int renegotiationsDone = 0;
PRBool initializedServerSessionCache = PR_FALSE;
static char *progName;
static const char *requestFile;
secuPWData pwdata = { PW_NONE, 0 };
SSLNamedGroup *enabledGroups = NULL;
unsigned int enabledGroupsCount = 0;
const SSLSignatureScheme *enabledSigSchemes = NULL;
unsigned int enabledSigSchemeCount = 0;
const char *
signatureSchemeName(SSLSignatureScheme scheme)
{
switch (scheme) {
#define strcase(x) \
case ssl_sig_##x: \
return #x
strcase(none);
strcase(rsa_pkcs1_sha1);
strcase(rsa_pkcs1_sha256);
strcase(rsa_pkcs1_sha384);
strcase(rsa_pkcs1_sha512);
strcase(ecdsa_sha1);
strcase(ecdsa_secp256r1_sha256);
strcase(ecdsa_secp384r1_sha384);
strcase(ecdsa_secp521r1_sha512);
strcase(rsa_pss_rsae_sha256);
strcase(rsa_pss_rsae_sha384);
strcase(rsa_pss_rsae_sha512);
strcase(ed25519);
strcase(ed448);
strcase(rsa_pss_pss_sha256);
strcase(rsa_pss_pss_sha384);
strcase(rsa_pss_pss_sha512);
strcase(dsa_sha1);
strcase(dsa_sha256);
strcase(dsa_sha384);
strcase(dsa_sha512);
#undef strcase
case ssl_sig_rsa_pkcs1_sha1md5:
return "RSA PKCS#1 SHA1+MD5";
default:
break;
}
return "Unknown Scheme";
}
void
printSecurityInfo(PRFileDesc *fd)
{
CERTCertificate *cert;
const SECItemArray *csa;
const SECItem *scts;
SSL3Statistics *ssl3stats = SSL_GetStatistics();
SECStatus result;
SSLChannelInfo channel;
SSLCipherSuiteInfo suite;
result = SSL_GetChannelInfo(fd, &channel, sizeof channel);
if (result == SECSuccess &&
channel.length == sizeof channel &&
channel.cipherSuite) {
result = SSL_GetCipherSuiteInfo(channel.cipherSuite,
&suite, sizeof suite);
if (result == SECSuccess) {
FPRINTF(stderr,
"tstclnt: SSL version %d.%d using %d-bit %s with %d-bit %s MAC\n",
channel.protocolVersion >> 8, channel.protocolVersion & 0xff,
suite.effectiveKeyBits, suite.symCipherName,
suite.macBits, suite.macAlgorithmName);
FPRINTF(stderr,
"tstclnt: Server Auth: %d-bit %s, Key Exchange: %d-bit %s\n"
" Compression: %s, Extended Master Secret: %s\n"
" Signature Scheme: %s\n",
channel.authKeyBits, suite.authAlgorithmName,
channel.keaKeyBits, suite.keaTypeName,
channel.compressionMethodName,
channel.extendedMasterSecretUsed ? "Yes" : "No",
signatureSchemeName(channel.signatureScheme));
}
}
cert = SSL_RevealCert(fd);
if (cert) {
char *ip = CERT_NameToAscii(&cert->issuer);
char *sp = CERT_NameToAscii(&cert->subject);
if (sp) {
fprintf(stderr, "subject DN: %s\n", sp);
PORT_Free(sp);
}
if (ip) {
fprintf(stderr, "issuer DN: %s\n", ip);
PORT_Free(ip);
}
CERT_DestroyCertificate(cert);
cert = NULL;
}
fprintf(stderr,
"%ld cache hits; %ld cache misses, %ld cache not reusable\n"
"%ld stateless resumes\n",
ssl3stats->hsh_sid_cache_hits, ssl3stats->hsh_sid_cache_misses,
ssl3stats->hsh_sid_cache_not_ok, ssl3stats->hsh_sid_stateless_resumes);
csa = SSL_PeerStapledOCSPResponses(fd);
if (csa) {
fprintf(stderr, "Received %d Cert Status items (OCSP stapled data)\n",
csa->len);
}
scts = SSL_PeerSignedCertTimestamps(fd);
if (scts && scts->len) {
fprintf(stderr, "Received a Signed Certificate Timestamp of length"
" %u\n",
scts->len);
}
if (channel.peerDelegCred) {
fprintf(stderr, "Received a Delegated Credential\n");
}
}
static void
PrintUsageHeader()
{
fprintf(stderr,
"Usage: %s -h host [-a 1st_hs_name ] [-a 2nd_hs_name ] [-p port]\n"
" [-D | -d certdir] [-C] [-b | -R root-module] \n"
" [-n nickname] [-Bafosvx] [-c ciphers] [-Y] [-Z] [-E]\n"
" [-V [min-version]:[max-version]] [-K] [-T] [-U]\n"
" [-r N] [-w passwd] [-W pwfile] [-q [-t seconds]]\n"
" [-I groups] [-J signatureschemes]\n"
" [-A requestfile] [-L totalconnections] [-P {client,server}]\n"
" [-N encryptedSniKeys] [-Q]\n"
"\n",
progName);
}
static void
PrintParameterUsage()
{
fprintf(stderr, "%-20s Send different SNI name. 1st_hs_name - at first\n"
"%-20s handshake, 2nd_hs_name - at second handshake.\n"
"%-20s Default is host from the -h argument.\n",
"-a name",
"", "");
fprintf(stderr, "%-20s Hostname to connect with\n", "-h host");
fprintf(stderr, "%-20s Port number for SSL server\n", "-p port");
fprintf(stderr,
"%-20s Directory with cert database (default is ~/.netscape)\n",
"-d certdir");
fprintf(stderr, "%-20s Run without a cert database\n", "-D");
fprintf(stderr, "%-20s Load the default \"builtins\" root CA module\n", "-b");
fprintf(stderr, "%-20s Load the given root CA module\n", "-R");
fprintf(stderr, "%-20s Print certificate chain information\n", "-C");
fprintf(stderr, "%-20s (use -C twice to print more certificate details)\n", "");
fprintf(stderr, "%-20s (use -C three times to include PEM format certificate dumps)\n", "");
fprintf(stderr, "%-20s Nickname of key and cert\n",
"-n nickname");
fprintf(stderr,
"%-20s Restricts the set of enabled SSL/TLS protocols versions.\n"
"%-20s All versions are enabled by default.\n"
"%-20s Possible values for min/max: ssl3 tls1.0 tls1.1 tls1.2 tls1.3\n"
"%-20s Example: \"-V ssl3:\" enables SSL 3 and newer.\n",
"-V [min]:[max]", "", "", "");
fprintf(stderr, "%-20s Send TLS_FALLBACK_SCSV\n", "-K");
fprintf(stderr, "%-20s Prints only payload data. Skips HTTP header.\n", "-S");
fprintf(stderr, "%-20s Client speaks first. \n", "-f");
fprintf(stderr, "%-20s Use synchronous certificate validation\n", "-O");
fprintf(stderr, "%-20s Override bad server cert. Make it OK.\n", "-o");
fprintf(stderr, "%-20s Disable SSL socket locking.\n", "-s");
fprintf(stderr, "%-20s Verbose progress reporting.\n", "-v");
fprintf(stderr, "%-20s Ping the server and then exit.\n", "-q");
fprintf(stderr, "%-20s Timeout for server ping (default: no timeout).\n", "-t seconds");
fprintf(stderr, "%-20s Renegotiate N times (resuming session if N>1).\n", "-r N");
fprintf(stderr, "%-20s Enable the session ticket extension.\n", "-u");
fprintf(stderr, "%-20s Enable false start.\n", "-g");
fprintf(stderr, "%-20s Enable the cert_status extension (OCSP stapling).\n", "-T");
fprintf(stderr, "%-20s Enable the signed_certificate_timestamp extension.\n", "-U");
fprintf(stderr, "%-20s Enable the delegated credentials extension.\n", "-B");
fprintf(stderr, "%-20s Require fresh revocation info from side channel.\n"
"%-20s -F once means: require for server cert only\n"
"%-20s -F twice means: require for intermediates, too\n"
"%-20s (Connect, handshake with server, disable dynamic download\n"
"%-20s of OCSP/CRL, verify cert using CERT_PKIXVerifyCert.)\n"
"%-20s Exit code:\n"
"%-20s 0: have fresh and valid revocation data, status good\n"
"%-20s 1: cert failed to verify, prior to revocation checking\n"
"%-20s 2: missing, old or invalid revocation data\n"
"%-20s 3: have fresh and valid revocation data, status revoked\n",
"-F", "", "", "", "", "", "", "", "", "");
fprintf(stderr, "%-20s Test -F allows 0=any (default), 1=only OCSP, 2=only CRL\n", "-M");
fprintf(stderr, "%-20s Restrict ciphers\n", "-c ciphers");
fprintf(stderr, "%-20s Print cipher values allowed for parameter -c and exit\n", "-Y");
fprintf(stderr, "%-20s Enforce using an IPv4 destination address\n", "-4");
fprintf(stderr, "%-20s Enforce using an IPv6 destination address\n", "-6");
fprintf(stderr, "%-20s (Options -4 and -6 cannot be combined.)\n", "");
fprintf(stderr, "%-20s Enable the extended master secret extension [RFC7627]\n", "-G");
fprintf(stderr, "%-20s Require the use of FFDHE supported groups [RFC7919]\n", "-H");
fprintf(stderr, "%-20s Read from a file instead of stdin\n", "-A");
fprintf(stderr, "%-20s Allow 0-RTT data (TLS 1.3 only)\n", "-Z");
fprintf(stderr, "%-20s Disconnect and reconnect up to N times total\n", "-L");
fprintf(stderr, "%-20s Comma separated list of enabled groups for TLS key exchange.\n"
"%-20s The following values are valid:\n"
"%-20s P256, P384, P521, x25519, FF2048, FF3072, FF4096, FF6144, FF8192\n",
"-I", "", "");
fprintf(stderr, "%-20s Comma separated list of signature schemes in preference order.\n"
"%-20s The following values are valid:\n"
"%-20s rsa_pkcs1_sha1, rsa_pkcs1_sha256, rsa_pkcs1_sha384, rsa_pkcs1_sha512,\n"
"%-20s ecdsa_sha1, ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384,\n"
"%-20s ecdsa_secp521r1_sha512,\n"
"%-20s rsa_pss_rsae_sha256, rsa_pss_rsae_sha384, rsa_pss_rsae_sha512,\n"
"%-20s rsa_pss_pss_sha256, rsa_pss_pss_sha384, rsa_pss_pss_sha512,\n"
"%-20s dsa_sha1, dsa_sha256, dsa_sha384, dsa_sha512\n",
"-J", "", "", "", "", "", "", "");
fprintf(stderr, "%-20s Enable alternative TLS 1.3 handshake\n", "-X alt-server-hello");
fprintf(stderr, "%-20s Use DTLS\n", "-P {client, server}");
fprintf(stderr, "%-20s Exit after handshake\n", "-Q");
fprintf(stderr, "%-20s Encrypted SNI Keys\n", "-N");
fprintf(stderr, "%-20s Enable post-handshake authentication\n"
"%-20s for TLS 1.3; need to specify -n\n",
"-E", "");
fprintf(stderr, "%-20s Export and print keying material after successful handshake.\n"
"%-20s The argument is a comma separated list of exporters in the form:\n"
"%-20s LABEL[:OUTPUT-LENGTH[:CONTEXT]]\n"
"%-20s where LABEL and CONTEXT can be either a free-form string or\n"
"%-20s a hex string if it is preceded by \"0x\"; OUTPUT-LENGTH\n"
"%-20s is a decimal integer.\n",
"-x", "", "", "", "", "");
}
static void
Usage()
{
PrintUsageHeader();
PrintParameterUsage();
exit(1);
}
static void
PrintCipherUsage()
{
PrintUsageHeader();
fprintf(stderr, "%-20s Letter(s) chosen from the following list\n",
"-c ciphers");
fprintf(stderr,
"c SSL3 RSA WITH RC4 128 MD5\n"
"d SSL3 RSA WITH 3DES EDE CBC SHA\n"
"e SSL3 RSA WITH DES CBC SHA\n"
"i SSL3 RSA WITH NULL MD5\n"
"n SSL3 RSA WITH RC4 128 SHA\n"
"o SSL3 DHE DSS WITH RC4 128 SHA\n"
"p SSL3 DHE RSA WITH 3DES EDE CBC SHA\n"
"q SSL3 DHE DSS WITH 3DES EDE CBC SHA\n"
"r SSL3 DHE RSA WITH DES CBC SHA\n"
"s SSL3 DHE DSS WITH DES CBC SHA\n"
"t SSL3 DHE DSS WITH AES 128 CBC SHA\n"
"u SSL3 DHE RSA WITH AES 128 CBC SHA\n"
"v SSL3 RSA WITH AES 128 CBC SHA\n"
"w SSL3 DHE DSS WITH AES 256 CBC SHA\n"
"x SSL3 DHE RSA WITH AES 256 CBC SHA\n"
"y SSL3 RSA WITH AES 256 CBC SHA\n"
"z SSL3 RSA WITH NULL SHA\n"
"\n"
":WXYZ Use cipher with hex code { 0xWX , 0xYZ } in TLS\n");
exit(1);
}
void
milliPause(PRUint32 milli)
{
PRIntervalTime ticks = PR_MillisecondsToInterval(milli);
PR_Sleep(ticks);
}
void
disableAllSSLCiphers()
{
const PRUint16 *cipherSuites = SSL_GetImplementedCiphers();
int i = SSL_GetNumImplementedCiphers();
SECStatus rv;
/* disable all the SSL3 cipher suites */
while (--i >= 0) {
PRUint16 suite = cipherSuites[i];
rv = SSL_CipherPrefSetDefault(suite, PR_FALSE);
if (rv != SECSuccess) {
PRErrorCode err = PR_GetError();
fprintf(stderr,
"SSL_CipherPrefSet didn't like value 0x%04x (i = %d): %s\n",
suite, i, SECU_Strerror(err));
exit(2);
}
}
}
typedef struct
{
PRBool shouldPause; /* PR_TRUE if we should use asynchronous peer cert
* authentication */
PRBool isPaused; /* PR_TRUE if libssl is waiting for us to validate the
* peer's certificate and restart the handshake. */
void *dbHandle; /* Certificate database handle to use while
* authenticating the peer's certificate. */
PRBool testFreshStatusFromSideChannel;
PRErrorCode sideChannelRevocationTestResultCode;
PRBool requireDataForIntermediates;
PRBool allowOCSPSideChannelData;
PRBool allowCRLSideChannelData;
} ServerCertAuth;
/*
* Callback is called when incoming certificate is not valid.
* Returns SECSuccess to accept the cert anyway, SECFailure to reject.
*/
static SECStatus
ownBadCertHandler(void *arg, PRFileDesc *socket)
{
PRErrorCode err = PR_GetError();
/* can log invalid cert here */
fprintf(stderr, "Bad server certificate: %d, %s\n", err,
SECU_Strerror(err));
return SECSuccess; /* override, say it's OK. */
}
#define EXIT_CODE_SIDECHANNELTEST_GOOD 0
#define EXIT_CODE_SIDECHANNELTEST_BADCERT 1
#define EXIT_CODE_SIDECHANNELTEST_NODATA 2
#define EXIT_CODE_SIDECHANNELTEST_REVOKED 3
static void
verifyFromSideChannel(CERTCertificate *cert, ServerCertAuth *sca)
{
PRUint64 revDoNotUse =
CERT_REV_M_DO_NOT_TEST_USING_THIS_METHOD;
PRUint64 revUseLocalOnlyAndSoftFail =
CERT_REV_M_TEST_USING_THIS_METHOD |
CERT_REV_M_FORBID_NETWORK_FETCHING |
CERT_REV_M_REQUIRE_INFO_ON_MISSING_SOURCE |
CERT_REV_M_IGNORE_MISSING_FRESH_INFO |
CERT_REV_M_STOP_TESTING_ON_FRESH_INFO;
PRUint64 revUseLocalOnlyAndHardFail =
CERT_REV_M_TEST_USING_THIS_METHOD |
CERT_REV_M_FORBID_NETWORK_FETCHING |
CERT_REV_M_REQUIRE_INFO_ON_MISSING_SOURCE |
CERT_REV_M_FAIL_ON_MISSING_FRESH_INFO |
CERT_REV_M_STOP_TESTING_ON_FRESH_INFO;
PRUint64 methodFlagsDoNotUse[2];
PRUint64 methodFlagsCheckSoftFail[2];
PRUint64 methodFlagsCheckHardFail[2];
CERTRevocationTests revTestsDoNotCheck;
CERTRevocationTests revTestsOverallSoftFail;
CERTRevocationTests revTestsOverallHardFail;
CERTRevocationFlags rev;
CERTValInParam cvin[2];
CERTValOutParam cvout[1];
SECStatus rv;
methodFlagsDoNotUse[cert_revocation_method_crl] = revDoNotUse;
methodFlagsDoNotUse[cert_revocation_method_ocsp] = revDoNotUse;
methodFlagsCheckSoftFail[cert_revocation_method_crl] =
sca->allowCRLSideChannelData ? revUseLocalOnlyAndSoftFail : revDoNotUse;
methodFlagsCheckSoftFail[cert_revocation_method_ocsp] =
sca->allowOCSPSideChannelData ? revUseLocalOnlyAndSoftFail : revDoNotUse;
methodFlagsCheckHardFail[cert_revocation_method_crl] =
sca->allowCRLSideChannelData ? revUseLocalOnlyAndHardFail : revDoNotUse;
methodFlagsCheckHardFail[cert_revocation_method_ocsp] =
sca->allowOCSPSideChannelData ? revUseLocalOnlyAndHardFail : revDoNotUse;
revTestsDoNotCheck.cert_rev_flags_per_method = methodFlagsDoNotUse;
revTestsDoNotCheck.number_of_defined_methods = 2;
revTestsDoNotCheck.number_of_preferred_methods = 0;
revTestsDoNotCheck.cert_rev_method_independent_flags =
CERT_REV_MI_TEST_ALL_LOCAL_INFORMATION_FIRST |
CERT_REV_MI_NO_OVERALL_INFO_REQUIREMENT;
revTestsOverallSoftFail.cert_rev_flags_per_method = 0; /* must define later */
revTestsOverallSoftFail.number_of_defined_methods = 2;
revTestsOverallSoftFail.number_of_preferred_methods = 0;
revTestsOverallSoftFail.cert_rev_method_independent_flags =
CERT_REV_MI_TEST_ALL_LOCAL_INFORMATION_FIRST |
CERT_REV_MI_NO_OVERALL_INFO_REQUIREMENT;
revTestsOverallHardFail.cert_rev_flags_per_method = 0; /* must define later */
revTestsOverallHardFail.number_of_defined_methods = 2;
revTestsOverallHardFail.number_of_preferred_methods = 0;
revTestsOverallHardFail.cert_rev_method_independent_flags =
CERT_REV_MI_TEST_ALL_LOCAL_INFORMATION_FIRST |
CERT_REV_MI_REQUIRE_SOME_FRESH_INFO_AVAILABLE;
rev.chainTests = revTestsDoNotCheck;
rev.leafTests = revTestsDoNotCheck;
cvin[0].type = cert_pi_revocationFlags;
cvin[0].value.pointer.revocation = &rev;
cvin[1].type = cert_pi_end;
cvout[0].type = cert_po_end;
/* Strategy:
*
* Verify with revocation checking disabled.
* On failure return 1.
*
* if result if "good", then continue testing.
*
* Verify with CERT_REV_M_FAIL_ON_MISSING_FRESH_INFO.
* If result is good, return 0.
*
* On failure continue testing, find out why it failed.
*
* Verify with CERT_REV_M_IGNORE_MISSING_FRESH_INFO
*
* If result is "good", then our previous test failed,
* because we don't have fresh revocation info, return 2.
*
* If result is still bad, we do have revocation info,
* and it says "revoked" or something equivalent, return 3.
*/
/* revocation checking disabled */
rv = CERT_PKIXVerifyCert(cert, certificateUsageSSLServer,
cvin, cvout, NULL);
if (rv != SECSuccess) {
sca->sideChannelRevocationTestResultCode =
EXIT_CODE_SIDECHANNELTEST_BADCERT;
return;
}
/* revocation checking, hard fail */
if (sca->allowOCSPSideChannelData && sca->allowCRLSideChannelData) {
/* any method is allowed. use soft fail on individual checks,
* but use hard fail on the overall check
*/
revTestsOverallHardFail.cert_rev_flags_per_method = methodFlagsCheckSoftFail;
} else {
/* only one method is allowed. use hard fail on the individual checks.
* hard/soft fail is irrelevant on overall flags.
*/
revTestsOverallHardFail.cert_rev_flags_per_method = methodFlagsCheckHardFail;
}
rev.leafTests = revTestsOverallHardFail;
rev.chainTests =
sca->requireDataForIntermediates ? revTestsOverallHardFail : revTestsDoNotCheck;
rv = CERT_PKIXVerifyCert(cert, certificateUsageSSLServer,
cvin, cvout, NULL);
if (rv == SECSuccess) {
sca->sideChannelRevocationTestResultCode =
EXIT_CODE_SIDECHANNELTEST_GOOD;
return;
}
/* revocation checking, soft fail */
revTestsOverallSoftFail.cert_rev_flags_per_method = methodFlagsCheckSoftFail;
rev.leafTests = revTestsOverallSoftFail;
rev.chainTests =
sca->requireDataForIntermediates ? revTestsOverallSoftFail : revTestsDoNotCheck;
rv = CERT_PKIXVerifyCert(cert, certificateUsageSSLServer,
cvin, cvout, NULL);
if (rv == SECSuccess) {
sca->sideChannelRevocationTestResultCode =
EXIT_CODE_SIDECHANNELTEST_NODATA;
return;
}
sca->sideChannelRevocationTestResultCode =
EXIT_CODE_SIDECHANNELTEST_REVOKED;
}
static void
dumpCertificatePEM(CERTCertificate *cert)
{
SECItem data;
data.data = cert->derCert.data;
data.len = cert->derCert.len;
fprintf(stderr, "%s\n%s\n%s\n", NS_CERT_HEADER,
BTOA_DataToAscii(data.data, data.len), NS_CERT_TRAILER);
}
static void
dumpServerCertificateChain(PRFileDesc *fd)
{
CERTCertList *peerCertChain = NULL;
CERTCertListNode *node = NULL;
CERTCertificate *peerCert = NULL;
CERTCertificateList *foundChain = NULL;
SECU_PPFunc dumpFunction = NULL;
PRBool dumpCertPEM = PR_FALSE;
if (!dumpServerChain) {
return;
} else if (dumpServerChain == 1) {
dumpFunction = (SECU_PPFunc)SECU_PrintCertificateBasicInfo;
} else {
dumpFunction = (SECU_PPFunc)SECU_PrintCertificate;
if (dumpServerChain > 2) {
dumpCertPEM = PR_TRUE;
}
}
SECU_EnableWrap(PR_FALSE);
fprintf(stderr, "==== certificate(s) sent by server: ====\n");
peerCertChain = SSL_PeerCertificateChain(fd);
if (peerCertChain) {
node = CERT_LIST_HEAD(peerCertChain);
while (!CERT_LIST_END(node, peerCertChain)) {
CERTCertificate *cert = node->cert;
SECU_PrintSignedContent(stderr, &cert->derCert, "Certificate", 0,
dumpFunction);
if (dumpCertPEM) {
dumpCertificatePEM(cert);
}
node = CERT_LIST_NEXT(node);
}
}
if (peerCertChain) {
peerCert = SSL_RevealCert(fd);
if (peerCert) {
foundChain = CERT_CertChainFromCert(peerCert, certificateUsageSSLServer,
PR_TRUE);
}
if (foundChain) {
unsigned int count = 0;
fprintf(stderr, "==== locally found issuer certificate(s): ====\n");
for (count = 0; count < (unsigned int)foundChain->len; count++) {
CERTCertificate *c;
PRBool wasSentByServer = PR_FALSE;
c = CERT_FindCertByDERCert(CERT_GetDefaultCertDB(), &foundChain->certs[count]);
node = CERT_LIST_HEAD(peerCertChain);
while (!CERT_LIST_END(node, peerCertChain)) {
CERTCertificate *cert = node->cert;
if (CERT_CompareCerts(cert, c)) {
wasSentByServer = PR_TRUE;
break;
}
node = CERT_LIST_NEXT(node);
}
if (!wasSentByServer) {
SECU_PrintSignedContent(stderr, &c->derCert, "Certificate", 0,
dumpFunction);
if (dumpCertPEM) {
dumpCertificatePEM(c);
}
}
CERT_DestroyCertificate(c);
}
CERT_DestroyCertificateList(foundChain);
}
if (peerCert) {
CERT_DestroyCertificate(peerCert);
}
CERT_DestroyCertList(peerCertChain);
peerCertChain = NULL;
}
fprintf(stderr, "==== end of certificate chain information ====\n");
fflush(stderr);
}
static SECStatus
ownAuthCertificate(void *arg, PRFileDesc *fd, PRBool checkSig,
PRBool isServer)
{
ServerCertAuth *serverCertAuth = (ServerCertAuth *)arg;
if (dumpServerChain) {
dumpServerCertificateChain(fd);
}
if (!serverCertAuth->shouldPause) {
CERTCertificate *cert;
unsigned int i;
const SECItemArray *csa;
if (!serverCertAuth->testFreshStatusFromSideChannel) {
return SSL_AuthCertificate(serverCertAuth->dbHandle,
fd, checkSig, isServer);
}
/* No verification attempt must have happened before now,
* to ensure revocation data has been actively retrieved yet,
* or our test will produce incorrect results.
*/
cert = SSL_RevealCert(fd);
if (!cert) {
exit(254);
}
csa = SSL_PeerStapledOCSPResponses(fd);
if (csa) {
for (i = 0; i < csa->len; ++i) {
PORT_SetError(0);
if (CERT_CacheOCSPResponseFromSideChannel(
serverCertAuth->dbHandle, cert, PR_Now(),
&csa->items[i], arg) != SECSuccess) {
PORT_Assert(PR_GetError() != 0);
}
}
}
verifyFromSideChannel(cert, serverCertAuth);
CERT_DestroyCertificate(cert);
/* return success to ensure our caller will continue and we will
* reach the code that handles
* serverCertAuth->sideChannelRevocationTestResultCode
*/
return SECSuccess;
}
FPRINTF(stderr, "%s: using asynchronous certificate validation\n",
progName);
PORT_Assert(!serverCertAuth->isPaused);
serverCertAuth->isPaused = PR_TRUE;
return SECWouldBlock;
}
SECStatus
own_GetClientAuthData(void *arg,
PRFileDesc *socket,
struct CERTDistNamesStr *caNames,
struct CERTCertificateStr **pRetCert,
struct SECKEYPrivateKeyStr **pRetKey)
{
if (verbose > 1) {
SECStatus rv;
fprintf(stderr, "Server requested Client Authentication\n");
if (caNames && caNames->nnames > 0) {
PLArenaPool *arena = caNames->arena;
if (!arena)
arena = PORT_NewArena(2048);
if (arena) {
int i;
for (i = 0; i < caNames->nnames; ++i) {
char *nameString;
CERTName dn;
rv = SEC_QuickDERDecodeItem(arena,
&dn,
SEC_ASN1_GET(CERT_NameTemplate),
caNames->names + i);
if (rv != SECSuccess)
continue;
nameString = CERT_NameToAscii(&dn);
if (!nameString)
continue;
fprintf(stderr, "CA[%d]: %s\n", i + 1, nameString);
PORT_Free(nameString);
}
if (!caNames->arena) {
PORT_FreeArena(arena, PR_FALSE);
}
}
}
rv = NSS_GetClientAuthData(arg, socket, caNames, pRetCert, pRetKey);
if (rv == SECSuccess && *pRetCert) {
char *nameString = CERT_NameToAscii(&((*pRetCert)->subject));
if (nameString) {
fprintf(stderr, "sent cert: %s\n", nameString);
PORT_Free(nameString);
}
} else {
fprintf(stderr, "send no cert\n");
}
return rv;
}
return NSS_GetClientAuthData(arg, socket, caNames, pRetCert, pRetKey);
}
#if defined(WIN32) || defined(OS2)
void
thread_main(void *arg)
{
PRFileDesc *ps = (PRFileDesc *)arg;
PRFileDesc *std_in;
int wc, rc;
char buf[256];
if (requestFile) {
std_in = PR_Open(requestFile, PR_RDONLY, 0);
} else {
std_in = PR_GetSpecialFD(PR_StandardInput);
}
#ifdef WIN32
if (!requestFile) {
/* Put stdin into O_BINARY mode
** or else incoming \r\n's will become \n's.
*/
int smrv = _setmode(_fileno(stdin), _O_BINARY);
if (smrv == -1) {
fprintf(stderr,
"%s: Cannot change stdin to binary mode. Use -i option instead.\n",
progName);
/* plow ahead anyway */
}
}
#endif
do {
rc = PR_Read(std_in, buf, sizeof buf);
if (rc <= 0)
break;
wc = PR_Send(ps, buf, rc, 0, maxInterval);
} while (wc == rc);
PR_Close(ps);
if (requestFile) {
PR_Close(std_in);
}
}
#endif
static void
printHostNameAndAddr(const char *host, const PRNetAddr *addr)
{
PRUint16 port = PR_NetAddrInetPort(addr);
char addrBuf[80];
PRStatus st = PR_NetAddrToString(addr, addrBuf, sizeof addrBuf);
if (st == PR_SUCCESS) {
port = PR_ntohs(port);
FPRINTF(stderr, "%s: connecting to %s:%hu (address=%s)\n",
progName, host, port, addrBuf);
}
}
/*
* Prints output according to skipProtoHeader flag. If skipProtoHeader
* is not set, prints without any changes, otherwise looking
* for \n\r\n(empty line sequence: HTTP header separator) and
* prints everything after it.
*/
static void
separateReqHeader(const PRFileDesc *outFd, const char *buf, const int nb,
PRBool *wrStarted, int *ptrnMatched)
{
/* it is sufficient to look for only "\n\r\n". Hopping that
* HTTP response format satisfies the standard */
char *ptrnStr = "\n\r\n";
char *resPtr;
if (nb == 0) {
return;
}
if (*ptrnMatched > 0) {
/* Get here only if previous separateReqHeader call found
* only a fragment of "\n\r\n" in previous buffer. */
PORT_Assert(*ptrnMatched < 3);
/* the size of fragment of "\n\r\n" what we want to find in this
* buffer is equal to *ptrnMatched */
if (*ptrnMatched <= nb) {
/* move the pointer to the beginning of the fragment */
int strSize = *ptrnMatched;
char *tmpPtrn = ptrnStr + (3 - strSize);
if (PL_strncmp(buf, tmpPtrn, strSize) == 0) {
/* print the rest of the buffer(without the fragment) */
PR_Write((void *)outFd, buf + strSize, nb - strSize);
*wrStarted = PR_TRUE;
return;
}
} else {
/* we are here only when nb == 1 && *ptrnMatched == 2 */
if (*buf == '\r') {
*ptrnMatched = 1;
} else {
*ptrnMatched = 0;
}
return;
}
}
resPtr = PL_strnstr(buf, ptrnStr, nb);
if (resPtr != NULL) {
/* if "\n\r\n" was found in the buffer, calculate offset
* and print the rest of the buffer */
int newBn = nb - (resPtr - buf + 3); /* 3 is the length of "\n\r\n" */
PR_Write((void *)outFd, resPtr + 3, newBn);
*wrStarted = PR_TRUE;
return;
} else {
/* try to find a fragment of "\n\r\n" at the end of the buffer.
* if found, set *ptrnMatched to the number of chars left to find
* in the next buffer.*/
int i;
for (i = 1; i < 3; i++) {
char *bufPrt;
int strSize = 3 - i;
if (strSize > nb) {
continue;
}
bufPrt = (char *)(buf + nb - strSize);
if (PL_strncmp(bufPrt, ptrnStr, strSize) == 0) {
*ptrnMatched = i;
return;
}
}
}
}
#define SSOCK_FD 0
#define STDIN_FD 1
#define HEXCHAR_TO_INT(c, i) \
if (((c) >= '0') && ((c) <= '9')) { \
i = (c) - '0'; \
} else if (((c) >= 'a') && ((c) <= 'f')) { \
i = (c) - 'a' + 10; \
} else if (((c) >= 'A') && ((c) <= 'F')) { \
i = (c) - 'A' + 10; \
} else { \
Usage(); \
}
static SECStatus
restartHandshakeAfterServerCertIfNeeded(PRFileDesc *fd,
ServerCertAuth *serverCertAuth,
PRBool override)
{
SECStatus rv;
PRErrorCode error = 0;
if (!serverCertAuth->isPaused)
return SECSuccess;
FPRINTF(stderr, "%s: handshake was paused by auth certificate hook\n",
progName);
serverCertAuth->isPaused = PR_FALSE;
rv = SSL_AuthCertificate(serverCertAuth->dbHandle, fd, PR_TRUE, PR_FALSE);
if (rv != SECSuccess) {
error = PR_GetError();
if (error == 0) {
PR_NOT_REACHED("SSL_AuthCertificate return SECFailure without "
"setting error code.");
error = PR_INVALID_STATE_ERROR;
} else if (override) {
rv = ownBadCertHandler(NULL, fd);
}
}
if (rv == SECSuccess) {
error = 0;
}
if (SSL_AuthCertificateComplete(fd, error) != SECSuccess) {
rv = SECFailure;
} else {
/* restore the original error code, which could be reset by
* SSL_AuthCertificateComplete */
PORT_SetError(error);
}
return rv;
}
char *host = NULL;
char *nickname = NULL;
char *cipherString = NULL;
int multiplier = 0;
SSLVersionRange enabledVersions;
int disableLocking = 0;
int enableSessionTickets = 0;
int enableFalseStart = 0;
int enableCertStatus = 0;
int enableSignedCertTimestamps = 0;
int forceFallbackSCSV = 0;
int enableExtendedMasterSecret = 0;
PRBool requireDHNamedGroups = 0;
PRSocketOptionData opt;
PRNetAddr addr;
PRBool allowIPv4 = PR_TRUE;
PRBool allowIPv6 = PR_TRUE;
PRBool pingServerFirst = PR_FALSE;
int pingTimeoutSeconds = -1;
PRBool clientSpeaksFirst = PR_FALSE;
PRBool skipProtoHeader = PR_FALSE;
ServerCertAuth serverCertAuth;
char *hs1SniHostName = NULL;
char *hs2SniHostName = NULL;
PRUint16 portno = 443;
int override = 0;
PRBool enableZeroRtt = PR_FALSE;
PRUint8 *zeroRttData;
unsigned int zeroRttLen = 0;
PRBool enableAltServerHello = PR_FALSE;
PRBool useDTLS = PR_FALSE;
PRBool actAsServer = PR_FALSE;