]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/clAppUtils/sslClient.cpp
2 * sslClient.cpp : perform one SSL client side sesssion
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 <clAppUtils/ringBufferIo.h>
10 #include <utilLib/common.h>
11 #include <security_cdsa_utils/cuPrintCert.h>
13 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
19 #include <sys/param.h>
21 /* when true, keep listening until server disconnects */
22 #define KEEP_CONNECTED 1
24 #define CLIENT_GETMSG "GET / HTTP/1.0\r\n\r\n"
26 #define READBUF_LEN 256
28 /* relies on SSLSetProtocolVersionEnabled */
29 OSStatus
sslAppClient(
30 SslAppTestParams
*params
)
35 SSLContextRef ctx
= NULL
;
36 SecKeychainRef clientKc
= nil
;
37 CFArrayRef clientCerts
= nil
;
38 RingBuffers ringBufs
= {params
->serverToClientRing
, params
->clientToServerRing
};
40 sslThrDebug("Client", "starting");
41 params
->negVersion
= kSSLProtocolUnknown
;
42 params
->negCipher
= SSL_NULL_WITH_NULL_NULL
;
43 params
->ortn
= noHardwareErr
;
45 if(params
->serverToClientRing
== NULL
) {
46 /* first make sure requested server is there */
47 ortn
= MakeServerConnection(params
->hostName
, params
->port
,
48 params
->nonBlocking
, &sock
, &peerId
);
50 printf("MakeServerConnection returned %d; aborting\n", (int)ortn
);
56 * Set up a SecureTransport session.
58 ortn
= SSLNewContext(false, &ctx
);
60 printSslErrStr("SSLNewContext", ortn
);
63 if(params
->serverToClientRing
) {
64 ortn
= SSLSetIOFuncs(ctx
, ringReadFunc
, ringWriteFunc
);
66 printSslErrStr("SSLSetIOFuncs", ortn
);
69 ortn
= SSLSetConnection(ctx
, (SSLConnectionRef
)&ringBufs
);
71 printSslErrStr("SSLSetConnection", ortn
);
76 ortn
= SSLSetIOFuncs(ctx
, SocketRead
, SocketWrite
);
78 printSslErrStr("SSLSetIOFuncs", ortn
);
81 ortn
= SSLSetConnection(ctx
, (SSLConnectionRef
)sock
);
83 printSslErrStr("SSLSetConnection", ortn
);
87 if(!params
->skipHostNameCheck
) {
88 ortn
= SSLSetPeerDomainName(ctx
, params
->hostName
,
89 strlen(params
->hostName
) + 1);
91 printSslErrStr("SSLSetPeerDomainName", ortn
);
96 /* remainder of setup is optional */
97 if(params
->anchorFile
) {
98 ortn
= sslAddTrustedRoot(ctx
, params
->anchorFile
, params
->replaceAnchors
);
103 if(!params
->noProtSpec
) {
104 ortn
= sslSetProtocols(ctx
, params
->acceptedProts
, params
->tryVersion
);
109 if(params
->resumeEnable
) {
110 ortn
= SSLSetPeerID(ctx
, &peerId
, sizeof(PeerSpec
));
112 printSslErrStr("SSLSetPeerID", ortn
);
116 if(params
->disableCertVerify
) {
117 ortn
= SSLSetEnableCertVerify(ctx
, false);
119 printSslErrStr("SSLSetEnableCertVerify", ortn
);
123 if(params
->ciphers
!= NULL
) {
124 ortn
= sslSetEnabledCiphers(ctx
, params
->ciphers
);
129 if(params
->myCertKcName
) {
130 clientCerts
= getSslCerts(params
->myCertKcName
, CSSM_FALSE
, CSSM_FALSE
, NULL
, &clientKc
);
131 if(clientCerts
== nil
) {
134 if(params
->password
) {
135 ortn
= SecKeychainUnlock(clientKc
, strlen(params
->password
),
136 (void *)params
->password
, true);
138 printf("SecKeychainUnlock returned %d\n", (int)ortn
);
142 if(params
->idIsTrustedRoot
) {
143 /* assume this is a root we want to implicitly trust */
144 ortn
= addIdentityAsTrustedRoot(ctx
, clientCerts
);
149 ortn
= SSLSetCertificate(ctx
, clientCerts
);
151 printSslErrStr("SSLSetCertificate", ortn
);
156 ortn
= SSLHandshake(ctx
);
157 if((ortn
== errSSLWouldBlock
) && !params
->silent
) {
158 /* keep UI responsive */
161 } while (ortn
== errSSLWouldBlock
);
163 SSLGetClientCertificateState(ctx
, ¶ms
->certState
);
164 SSLGetNegotiatedCipher(ctx
, ¶ms
->negCipher
);
165 SSLGetNegotiatedProtocolVersion(ctx
, ¶ms
->negVersion
);
173 ortn
= SSLWrite(ctx
, CLIENT_GETMSG
, strlen(CLIENT_GETMSG
), &actLen
);
175 printSslErrStr("SSLWrite", ortn
);
182 * Consume any server data and wait for server to disconnect
184 char readBuf
[READBUF_LEN
];
186 ortn
= SSLRead(ctx
, readBuf
, READBUF_LEN
, &actLen
);
187 } while (ortn
== errSSLWouldBlock
);
189 /* convert normal "shutdown" into zero err rtn */
190 if(ortn
== errSSLClosedGraceful
) {
193 #endif /* KEEP_CONNECTED */
197 OSStatus cerr
= SSLClose(ctx
);
203 endpointShutdown(sock
);
205 ringBuffersClose(&ringBufs
); /* tolerates NULLs */
207 SSLDisposeContext(ctx
);
210 sslThrDebug("Client", "done");