2 * sslSession.cpp - basic 2-thread SSL server/client session
4 #include <Security/SecureTransport.h>
5 #include <Security/Security.h>
6 #include <clAppUtils/sslAppUtils.h>
7 #include <clAppUtils/ioSock.h>
8 #include <clAppUtils/sslThreading.h>
9 #include <security_cdsa_utils/cuFileIo.h>
10 #include <utilLib/common.h>
11 #include <security_cdsa_utils/cuPrintCert.h>
12 #include <security_utilities/threading.h>
13 #include <security_utilities/devrandom.h>
15 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
22 #include <sys/param.h>
25 #define HOST_DEF "localhost"
26 #define DH_PARAMS "dhParams_512.der"
28 static void usage(char **argv
)
30 printf("Usage: %s server_kc [options]\n", argv
[0]);
32 printf(" P=port (default = %d)\n", PORT_DEF
);
33 printf(" c=client_kc (default is none)\n");
34 printf(" d (DSA, default is RSA)\n");
35 printf(" f (D-H, default is RSA)\n");
36 printf(" a anchor File for client side (typically, the server's cert)\n");
37 printf(" A anchor file for server side (typically, the client's cert)\n");
38 printf(" h hostname (default is %s)\n", HOST_DEF
);
39 printf(" k (skip hostname check)\n");
40 printf(" b (non blocking I/O)\n");
41 printf(" u Require client authentication\n");
42 printf(" x Expect policy verify error on client side\n");
43 printf(" X Expect policy verify error on server side\n");
44 printf(" z=kc_pwd\n");
45 printf(" R (ringBuffer I/O)\n");
46 printf(" l=loops (default 1)\n");
48 printf(" v(erbose)\n");
52 #define IGNORE_SIGPIPE 1
59 #endif /* IGNORE_SIGPIPE */
61 static SSLCipherSuite ciphers
[] = {
62 SSL_RSA_WITH_RC4_128_SHA
, SSL_NO_SUCH_CIPHERSUITE
66 * Default params for each test. Main() adjust this per cmd line
69 SslAppTestParams serverDefaults
=
72 false, // skipHostNameCHeck
74 NULL
, NULL
, // RingBuffers
77 NULL
, // acceptedProts - not used in this test
78 NULL
, // myCerts - const
80 true, // idIsTrustedRoot
81 false, // disableCertVerify
83 false, // replaceAnchors
85 false, // resumeEnable
91 kTLSProtocol1
, // expectVersion
101 false, // serverAbort
104 SSL_NULL_WITH_NULL_NULL
,
110 SslAppTestParams clientDefaults
=
113 false, // skipHostNameCHeck
115 NULL
, NULL
, // RingBuffers
118 NULL
, // acceptedProts - not used in this test
119 NULL
, // myCerts - const
121 true, // idIsTrustedRoot
122 false, // disableCertVerify
124 false, // replaceAnchors
126 false, // resumeEnable
128 false, // nonBlocking
132 kTLSProtocol1
, // expectVersion
140 false, // serverReady
142 false, // serverAbort
145 SSL_NULL_WITH_NULL_NULL
,
151 int main(int argc
, char **argv
)
155 bool dhEnable
= false;
158 bool ringBufferIo
= false;
159 RingBuffer serverToClientRing
;
160 RingBuffer clientToServerRing
;
165 serverDefaults
.myCertKcName
= argv
[1];
166 for(int arg
=2; arg
<argc
; arg
++) {
170 clientDefaults
.myCertKcName
= &argp
[2];
172 serverDefaults
.quiet
= clientDefaults
.quiet
= true;
175 serverDefaults
.verbose
= clientDefaults
.verbose
= true;
178 serverDefaults
.port
= clientDefaults
.port
= atoi(&argp
[2]);
181 serverDefaults
.nonBlocking
= clientDefaults
.nonBlocking
=
185 ciphers
[0] = SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
;
189 ciphers
[0] = SSL_DH_anon_WITH_RC4_128_MD5
;
196 clientDefaults
.hostName
=argv
[arg
];
199 clientDefaults
.skipHostNameCheck
= true;
205 clientDefaults
.anchorFile
= serverDefaults
.anchorFile
= argv
[arg
];
211 serverDefaults
.anchorFile
= argv
[arg
];
214 serverDefaults
.password
= &argp
[2];
217 serverDefaults
.port
= clientDefaults
.port
= atoi(&argp
[2]);
220 serverDefaults
.authenticate
= kAlwaysAuthenticate
;
221 if(serverDefaults
.expectCertState
== kSSLClientCertNone
) {
222 serverDefaults
.expectCertState
= kSSLClientCertSent
;
224 /* else it was set by 'X' option */
225 if(clientDefaults
.expectCertState
== kSSLClientCertNone
) {
226 clientDefaults
.expectCertState
= kSSLClientCertSent
;
231 /* server side has bad cert */
232 clientDefaults
.expectRtn
= errSSLXCertChainInvalid
;
233 serverDefaults
.expectRtn
= errSSLPeerCertUnknown
;
236 /* client side has bad cert */
237 serverDefaults
.expectRtn
= errSSLXCertChainInvalid
;
238 clientDefaults
.expectRtn
= errSSLPeerCertUnknown
;
239 serverDefaults
.expectCertState
= kSSLClientCertRejected
;
240 clientDefaults
.expectCertState
= kSSLClientCertRejected
;
246 loops
= atoi(&argp
[2]);
254 signal(SIGPIPE
, sigpipe
);
258 /* set up ring buffers */
259 ringBufSetup(&serverToClientRing
, "serveToClient", DEFAULT_NUM_RB_BUFS
, DEFAULT_BUF_RB_SIZE
);
260 ringBufSetup(&clientToServerRing
, "clientToServe", DEFAULT_NUM_RB_BUFS
, DEFAULT_BUF_RB_SIZE
);
261 serverDefaults
.serverToClientRing
= &serverToClientRing
;
262 serverDefaults
.clientToServerRing
= &clientToServerRing
;
263 clientDefaults
.serverToClientRing
= &serverToClientRing
;
264 clientDefaults
.clientToServerRing
= &clientToServerRing
;
267 /* snag D-H params */
268 if(readFile(DH_PARAMS
, (unsigned char **)&serverDefaults
.dhParams
,
269 &serverDefaults
.dhParamsLen
)) {
270 printf("***Error reading Diffie-Hellman params."
271 " Patience, grasshopper.\n");
274 testStartBanner("sslSession", argc
, argv
);
275 for(loop
=0; loop
<loops
; loop
++) {
276 ourRtn
= sslRunSession(&serverDefaults
, &clientDefaults
, NULL
);
281 if(!clientDefaults
.quiet
) {
283 if(!serverDefaults
.quiet
) {
284 printf("===== %s test PASSED =====\n", argv
[0]);
288 printf("****FAIL: %d errors detected\n", ourRtn
);