]>
git.saurik.com Git - apple/security.git/blob - sslViewer/sslClient.cpp
2 * Copyright (c) 2006-2008,2010-2011,2013 Apple Inc. All Rights Reserved.
4 * sslClient.cpp : perform one SSL client side sesssion
7 #include <Security/SecureTransport.h>
8 #include <Security/Security.h>
9 #include <clAppUtils/sslAppUtils.h>
10 #include <clAppUtils/ioSock.h>
11 #include <clAppUtils/sslThreading.h>
12 #include <utilLib/fileIo.h>
13 #include <utilLib/common.h>
14 #include <security_cdsa_utils/cuPrintCert.h>
16 #include <Security/SecBase.h>
23 #include <sys/param.h>
25 /* when true, keep listening until server disconnects */
26 #define KEEP_CONNECTED 1
28 #define CLIENT_GETMSG "GET / HTTP/1.0\r\n\r\n"
30 #define READBUF_LEN 256
32 /* relies on SSLSetProtocolVersionEnabled */
33 OSStatus
sslAppClient(
34 SslAppTestParams
*params
)
39 SSLContextRef ctx
= NULL
;
40 SecKeychainRef clientKc
= nil
;
41 CFArrayRef clientCerts
= nil
;
43 sslThrDebug("Client", "starting");
44 params
->negVersion
= kSSLProtocolUnknown
;
45 params
->negCipher
= SSL_NULL_WITH_NULL_NULL
;
46 params
->ortn
= noHardwareErr
;
48 /* first make sure requested server is there */
49 ortn
= MakeServerConnection(params
->hostName
, params
->port
,
50 params
->nonBlocking
, &sock
, &peerId
);
52 printf("MakeServerConnection returned %d; aborting\n", (int)ortn
);
57 * Set up a SecureTransport session.
59 ortn
= SSLNewContext(false, &ctx
);
61 printSslErrStr("SSLNewContext", ortn
);
64 ortn
= SSLSetIOFuncs(ctx
, SocketRead
, SocketWrite
);
66 printSslErrStr("SSLSetIOFuncs", ortn
);
69 ortn
= SSLSetConnection(ctx
, (SSLConnectionRef
)sock
);
71 printSslErrStr("SSLSetConnection", ortn
);
74 if(!params
->skipHostNameCheck
) {
75 ortn
= SSLSetPeerDomainName(ctx
, params
->hostName
,
76 strlen(params
->hostName
));
78 printSslErrStr("SSLSetPeerDomainName", ortn
);
83 /* remainder of setup is optional */
84 if(params
->anchorFile
) {
85 ortn
= sslAddTrustedRoot(ctx
, params
->anchorFile
, params
->replaceAnchors
);
90 ortn
= sslSetProtocols(ctx
, params
->acceptedProts
, params
->tryVersion
);
94 if(params
->resumeEnable
) {
95 ortn
= SSLSetPeerID(ctx
, &peerId
, sizeof(PeerSpec
));
97 printSslErrStr("SSLSetPeerID", ortn
);
101 if(params
->disableCertVerify
) {
102 ortn
= SSLSetEnableCertVerify(ctx
, false);
104 printSslErrStr("SSLSetEnableCertVerify", ortn
);
108 if(params
->ciphers
!= NULL
) {
109 ortn
= sslSetEnabledCiphers(ctx
, params
->ciphers
);
114 if(params
->myCertKcName
) {
115 clientCerts
= getSslCerts(params
->myCertKcName
, false, false, NULL
, &clientKc
);
116 if(clientCerts
== nil
) {
119 if(params
->password
) {
120 ortn
= SecKeychainUnlock(clientKc
, strlen(params
->password
),
121 (void *)params
->password
, true);
123 printf("SecKeychainUnlock returned %d\n", (int)ortn
);
127 if(params
->idIsTrustedRoot
) {
128 /* assume this is a root we want to implicitly trust */
129 ortn
= addIdentityAsTrustedRoot(ctx
, clientCerts
);
134 ortn
= SSLSetCertificate(ctx
, clientCerts
);
136 printSslErrStr("SSLSetCertificate", ortn
);
141 ortn
= SSLHandshake(ctx
);
142 if((ortn
== errSSLWouldBlock
) && !params
->silent
) {
143 /* keep UI responsive */
146 } while (ortn
== errSSLWouldBlock
);
148 SSLGetClientCertificateState(ctx
, ¶ms
->certState
);
149 SSLGetNegotiatedCipher(ctx
, ¶ms
->negCipher
);
150 SSLGetNegotiatedProtocolVersion(ctx
, ¶ms
->negVersion
);
152 if(ortn
!= errSecSuccess
) {
158 ortn
= SSLWrite(ctx
, CLIENT_GETMSG
, strlen(CLIENT_GETMSG
), &actLen
);
160 printSslErrStr("SSLWrite", ortn
);
167 * Consume any server data and wait for server to disconnect
169 char readBuf
[READBUF_LEN
];
171 ortn
= SSLRead(ctx
, readBuf
, READBUF_LEN
, &actLen
);
172 } while (ortn
== errSSLWouldBlock
);
174 /* convert normal "shutdown" into zero err rtn */
175 if(ortn
== errSSLClosedGraceful
) {
176 ortn
= errSecSuccess
;
178 #endif /* KEEP_CONNECTED */
182 OSStatus cerr
= SSLClose(ctx
);
183 if(ortn
== errSecSuccess
) {
188 endpointShutdown(sock
);
191 SSLDisposeContext(ctx
);
194 sslThrDebug("Client", "done");