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