1*ebfedea0SLionel Sambuc /*
2*ebfedea0SLionel Sambuc * An demo illustrating how to retrieve a URI from a secure HTTP server.
3*ebfedea0SLionel Sambuc *
4*ebfedea0SLionel Sambuc * Author: Roy Wood
5*ebfedea0SLionel Sambuc * Date: September 7, 1999
6*ebfedea0SLionel Sambuc * Comments: This relies heavily on my MacSockets library.
7*ebfedea0SLionel Sambuc * This project is also set up so that it expects the OpenSSL source folder (0.9.4 as I write this)
8*ebfedea0SLionel Sambuc * to live in a folder called "OpenSSL-0.9.4" in this project's parent folder. For example:
9*ebfedea0SLionel Sambuc *
10*ebfedea0SLionel Sambuc * Macintosh HD:
11*ebfedea0SLionel Sambuc * Development:
12*ebfedea0SLionel Sambuc * OpenSSL-0.9.4:
13*ebfedea0SLionel Sambuc * (OpenSSL sources here)
14*ebfedea0SLionel Sambuc * OpenSSL Example:
15*ebfedea0SLionel Sambuc * (OpenSSL example junk here)
16*ebfedea0SLionel Sambuc *
17*ebfedea0SLionel Sambuc *
18*ebfedea0SLionel Sambuc * Also-- before attempting to compile this, make sure the aliases in "OpenSSL-0.9.4:include:openssl"
19*ebfedea0SLionel Sambuc * are installed! Use the AppleScript applet in the "openssl-0.9.4" folder to do this!
20*ebfedea0SLionel Sambuc */
21*ebfedea0SLionel Sambuc /* modified to seed the PRNG */
22*ebfedea0SLionel Sambuc /* modified to use CRandomizer for seeding */
23*ebfedea0SLionel Sambuc
24*ebfedea0SLionel Sambuc
25*ebfedea0SLionel Sambuc // Include some funky libs I've developed over time
26*ebfedea0SLionel Sambuc
27*ebfedea0SLionel Sambuc #include "CPStringUtils.hpp"
28*ebfedea0SLionel Sambuc #include "ErrorHandling.hpp"
29*ebfedea0SLionel Sambuc #include "MacSocket.h"
30*ebfedea0SLionel Sambuc #include "Randomizer.h"
31*ebfedea0SLionel Sambuc
32*ebfedea0SLionel Sambuc // We use the OpenSSL implementation of SSL....
33*ebfedea0SLionel Sambuc // This was a lot of work to finally get going, though you wouldn't know it by the results!
34*ebfedea0SLionel Sambuc
35*ebfedea0SLionel Sambuc #include <openssl/ssl.h>
36*ebfedea0SLionel Sambuc #include <openssl/err.h>
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel Sambuc #include <timer.h>
39*ebfedea0SLionel Sambuc
40*ebfedea0SLionel Sambuc // Let's try grabbing some data from here:
41*ebfedea0SLionel Sambuc
42*ebfedea0SLionel Sambuc #define kHTTPS_DNS "www.apache-ssl.org"
43*ebfedea0SLionel Sambuc #define kHTTPS_Port 443
44*ebfedea0SLionel Sambuc #define kHTTPS_URI "/"
45*ebfedea0SLionel Sambuc
46*ebfedea0SLionel Sambuc
47*ebfedea0SLionel Sambuc // Forward-declare this
48*ebfedea0SLionel Sambuc
49*ebfedea0SLionel Sambuc OSErr MyMacSocket_IdleWaitCallback(void *inUserRefPtr);
50*ebfedea0SLionel Sambuc
51*ebfedea0SLionel Sambuc // My idle-wait callback. Doesn't do much, does it? Silly cooperative multitasking.
52*ebfedea0SLionel Sambuc
MyMacSocket_IdleWaitCallback(void * inUserRefPtr)53*ebfedea0SLionel Sambuc OSErr MyMacSocket_IdleWaitCallback(void *inUserRefPtr)
54*ebfedea0SLionel Sambuc {
55*ebfedea0SLionel Sambuc #pragma unused(inUserRefPtr)
56*ebfedea0SLionel Sambuc
57*ebfedea0SLionel Sambuc EventRecord theEvent;
58*ebfedea0SLionel Sambuc ::EventAvail(everyEvent,&theEvent);
59*ebfedea0SLionel Sambuc
60*ebfedea0SLionel Sambuc CRandomizer *randomizer = (CRandomizer*)inUserRefPtr;
61*ebfedea0SLionel Sambuc if (randomizer)
62*ebfedea0SLionel Sambuc randomizer->PeriodicAction();
63*ebfedea0SLionel Sambuc
64*ebfedea0SLionel Sambuc return(noErr);
65*ebfedea0SLionel Sambuc }
66*ebfedea0SLionel Sambuc
67*ebfedea0SLionel Sambuc
68*ebfedea0SLionel Sambuc // Finally!
69*ebfedea0SLionel Sambuc
main(void)70*ebfedea0SLionel Sambuc void main(void)
71*ebfedea0SLionel Sambuc {
72*ebfedea0SLionel Sambuc OSErr errCode;
73*ebfedea0SLionel Sambuc int theSocket = -1;
74*ebfedea0SLionel Sambuc int theTimeout = 30;
75*ebfedea0SLionel Sambuc
76*ebfedea0SLionel Sambuc SSL_CTX *ssl_ctx = nil;
77*ebfedea0SLionel Sambuc SSL *ssl = nil;
78*ebfedea0SLionel Sambuc
79*ebfedea0SLionel Sambuc char tempString[256];
80*ebfedea0SLionel Sambuc UnsignedWide microTickCount;
81*ebfedea0SLionel Sambuc
82*ebfedea0SLionel Sambuc
83*ebfedea0SLionel Sambuc CRandomizer randomizer;
84*ebfedea0SLionel Sambuc
85*ebfedea0SLionel Sambuc printf("OpenSSL Demo by Roy Wood, roy@centricsystems.ca\n\n");
86*ebfedea0SLionel Sambuc
87*ebfedea0SLionel Sambuc BailIfError(errCode = MacSocket_Startup());
88*ebfedea0SLionel Sambuc
89*ebfedea0SLionel Sambuc
90*ebfedea0SLionel Sambuc
91*ebfedea0SLionel Sambuc // Create a socket-like object
92*ebfedea0SLionel Sambuc
93*ebfedea0SLionel Sambuc BailIfError(errCode = MacSocket_socket(&theSocket,false,theTimeout * 60,MyMacSocket_IdleWaitCallback,&randomizer));
94*ebfedea0SLionel Sambuc
95*ebfedea0SLionel Sambuc
96*ebfedea0SLionel Sambuc // Set up the connect string and try to connect
97*ebfedea0SLionel Sambuc
98*ebfedea0SLionel Sambuc CopyCStrAndInsertCStrLongIntIntoCStr("%s:%ld",kHTTPS_DNS,kHTTPS_Port,tempString,sizeof(tempString));
99*ebfedea0SLionel Sambuc
100*ebfedea0SLionel Sambuc printf("Connecting to %s....\n",tempString);
101*ebfedea0SLionel Sambuc
102*ebfedea0SLionel Sambuc BailIfError(errCode = MacSocket_connect(theSocket,tempString));
103*ebfedea0SLionel Sambuc
104*ebfedea0SLionel Sambuc
105*ebfedea0SLionel Sambuc // Init SSL stuff
106*ebfedea0SLionel Sambuc
107*ebfedea0SLionel Sambuc SSL_load_error_strings();
108*ebfedea0SLionel Sambuc
109*ebfedea0SLionel Sambuc SSLeay_add_ssl_algorithms();
110*ebfedea0SLionel Sambuc
111*ebfedea0SLionel Sambuc
112*ebfedea0SLionel Sambuc // Pick the SSL method
113*ebfedea0SLionel Sambuc
114*ebfedea0SLionel Sambuc // ssl_ctx = SSL_CTX_new(SSLv2_client_method());
115*ebfedea0SLionel Sambuc ssl_ctx = SSL_CTX_new(SSLv23_client_method());
116*ebfedea0SLionel Sambuc // ssl_ctx = SSL_CTX_new(SSLv3_client_method());
117*ebfedea0SLionel Sambuc
118*ebfedea0SLionel Sambuc
119*ebfedea0SLionel Sambuc // Create an SSL thingey and try to negotiate the connection
120*ebfedea0SLionel Sambuc
121*ebfedea0SLionel Sambuc ssl = SSL_new(ssl_ctx);
122*ebfedea0SLionel Sambuc
123*ebfedea0SLionel Sambuc SSL_set_fd(ssl,theSocket);
124*ebfedea0SLionel Sambuc
125*ebfedea0SLionel Sambuc errCode = SSL_connect(ssl);
126*ebfedea0SLionel Sambuc
127*ebfedea0SLionel Sambuc if (errCode < 0)
128*ebfedea0SLionel Sambuc {
129*ebfedea0SLionel Sambuc SetErrorMessageAndLongIntAndBail("OpenSSL: Can't initiate SSL connection, SSL_connect() = ",errCode);
130*ebfedea0SLionel Sambuc }
131*ebfedea0SLionel Sambuc
132*ebfedea0SLionel Sambuc // Request the URI from the host
133*ebfedea0SLionel Sambuc
134*ebfedea0SLionel Sambuc CopyCStrToCStr("GET ",tempString,sizeof(tempString));
135*ebfedea0SLionel Sambuc ConcatCStrToCStr(kHTTPS_URI,tempString,sizeof(tempString));
136*ebfedea0SLionel Sambuc ConcatCStrToCStr(" HTTP/1.0\r\n\r\n",tempString,sizeof(tempString));
137*ebfedea0SLionel Sambuc
138*ebfedea0SLionel Sambuc
139*ebfedea0SLionel Sambuc errCode = SSL_write(ssl,tempString,CStrLength(tempString));
140*ebfedea0SLionel Sambuc
141*ebfedea0SLionel Sambuc if (errCode < 0)
142*ebfedea0SLionel Sambuc {
143*ebfedea0SLionel Sambuc SetErrorMessageAndLongIntAndBail("OpenSSL: Error writing data via ssl, SSL_write() = ",errCode);
144*ebfedea0SLionel Sambuc }
145*ebfedea0SLionel Sambuc
146*ebfedea0SLionel Sambuc
147*ebfedea0SLionel Sambuc for (;;)
148*ebfedea0SLionel Sambuc {
149*ebfedea0SLionel Sambuc char tempString[256];
150*ebfedea0SLionel Sambuc int bytesRead;
151*ebfedea0SLionel Sambuc
152*ebfedea0SLionel Sambuc
153*ebfedea0SLionel Sambuc // Read some bytes and dump them to the console
154*ebfedea0SLionel Sambuc
155*ebfedea0SLionel Sambuc bytesRead = SSL_read(ssl,tempString,sizeof(tempString) - 1);
156*ebfedea0SLionel Sambuc
157*ebfedea0SLionel Sambuc if (bytesRead == 0 && MacSocket_RemoteEndIsClosing(theSocket))
158*ebfedea0SLionel Sambuc {
159*ebfedea0SLionel Sambuc break;
160*ebfedea0SLionel Sambuc }
161*ebfedea0SLionel Sambuc
162*ebfedea0SLionel Sambuc else if (bytesRead < 0)
163*ebfedea0SLionel Sambuc {
164*ebfedea0SLionel Sambuc SetErrorMessageAndLongIntAndBail("OpenSSL: Error reading data via ssl, SSL_read() = ",bytesRead);
165*ebfedea0SLionel Sambuc }
166*ebfedea0SLionel Sambuc
167*ebfedea0SLionel Sambuc
168*ebfedea0SLionel Sambuc tempString[bytesRead] = '\0';
169*ebfedea0SLionel Sambuc
170*ebfedea0SLionel Sambuc printf("%s", tempString);
171*ebfedea0SLionel Sambuc }
172*ebfedea0SLionel Sambuc
173*ebfedea0SLionel Sambuc printf("\n\n\n");
174*ebfedea0SLionel Sambuc
175*ebfedea0SLionel Sambuc // All done!
176*ebfedea0SLionel Sambuc
177*ebfedea0SLionel Sambuc errCode = noErr;
178*ebfedea0SLionel Sambuc
179*ebfedea0SLionel Sambuc
180*ebfedea0SLionel Sambuc EXITPOINT:
181*ebfedea0SLionel Sambuc
182*ebfedea0SLionel Sambuc // Clean up and go home
183*ebfedea0SLionel Sambuc
184*ebfedea0SLionel Sambuc if (theSocket >= 0)
185*ebfedea0SLionel Sambuc {
186*ebfedea0SLionel Sambuc MacSocket_close(theSocket);
187*ebfedea0SLionel Sambuc }
188*ebfedea0SLionel Sambuc
189*ebfedea0SLionel Sambuc if (ssl != nil)
190*ebfedea0SLionel Sambuc {
191*ebfedea0SLionel Sambuc SSL_free(ssl);
192*ebfedea0SLionel Sambuc }
193*ebfedea0SLionel Sambuc
194*ebfedea0SLionel Sambuc if (ssl_ctx != nil)
195*ebfedea0SLionel Sambuc {
196*ebfedea0SLionel Sambuc SSL_CTX_free(ssl_ctx);
197*ebfedea0SLionel Sambuc }
198*ebfedea0SLionel Sambuc
199*ebfedea0SLionel Sambuc
200*ebfedea0SLionel Sambuc if (errCode != noErr)
201*ebfedea0SLionel Sambuc {
202*ebfedea0SLionel Sambuc printf("An error occurred:\n");
203*ebfedea0SLionel Sambuc
204*ebfedea0SLionel Sambuc printf("%s",GetErrorMessage());
205*ebfedea0SLionel Sambuc }
206*ebfedea0SLionel Sambuc
207*ebfedea0SLionel Sambuc
208*ebfedea0SLionel Sambuc MacSocket_Shutdown();
209*ebfedea0SLionel Sambuc }
210