2
0

GetHTTPS.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /* modified to seed the PRNG */
  22. /* modified to use CRandomizer for seeding */
  23. // Include some funky libs I've developed over time
  24. #include "CPStringUtils.hpp"
  25. #include "ErrorHandling.hpp"
  26. #include "MacSocket.h"
  27. #include "Randomizer.h"
  28. // We use the OpenSSL implementation of SSL....
  29. // This was a lot of work to finally get going, though you wouldn't know it by the results!
  30. #include <openssl/ssl.h>
  31. #include <openssl/err.h>
  32. #include <timer.h>
  33. // Let's try grabbing some data from here:
  34. #define kHTTPS_DNS "www.apache-ssl.org"
  35. #define kHTTPS_Port 443
  36. #define kHTTPS_URI "/"
  37. // Forward-declare this
  38. OSErr MyMacSocket_IdleWaitCallback(void *inUserRefPtr);
  39. // My idle-wait callback. Doesn't do much, does it? Silly cooperative multitasking.
  40. OSErr MyMacSocket_IdleWaitCallback(void *inUserRefPtr)
  41. {
  42. #pragma unused(inUserRefPtr)
  43. EventRecord theEvent;
  44. ::EventAvail(everyEvent,&theEvent);
  45. CRandomizer *randomizer = (CRandomizer*)inUserRefPtr;
  46. if (randomizer)
  47. randomizer->PeriodicAction();
  48. return(noErr);
  49. }
  50. // Finally!
  51. void main(void)
  52. {
  53. OSErr errCode;
  54. int theSocket = -1;
  55. int theTimeout = 30;
  56. SSL_CTX *ssl_ctx = nil;
  57. SSL *ssl = nil;
  58. char tempString[256];
  59. UnsignedWide microTickCount;
  60. CRandomizer randomizer;
  61. printf("OpenSSL Demo by Roy Wood, roy@centricsystems.ca\n\n");
  62. BailIfError(errCode = MacSocket_Startup());
  63. // Create a socket-like object
  64. BailIfError(errCode = MacSocket_socket(&theSocket,false,theTimeout * 60,MyMacSocket_IdleWaitCallback,&randomizer));
  65. // Set up the connect string and try to connect
  66. CopyCStrAndInsertCStrLongIntIntoCStr("%s:%ld",kHTTPS_DNS,kHTTPS_Port,tempString,sizeof(tempString));
  67. printf("Connecting to %s....\n",tempString);
  68. BailIfError(errCode = MacSocket_connect(theSocket,tempString));
  69. // Init SSL stuff
  70. SSL_load_error_strings();
  71. SSLeay_add_ssl_algorithms();
  72. // Pick the SSL method
  73. // ssl_ctx = SSL_CTX_new(SSLv2_client_method());
  74. ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  75. // ssl_ctx = SSL_CTX_new(SSLv3_client_method());
  76. // Create an SSL thingey and try to negotiate the connection
  77. ssl = SSL_new(ssl_ctx);
  78. SSL_set_fd(ssl,theSocket);
  79. errCode = SSL_connect(ssl);
  80. if (errCode < 0)
  81. {
  82. SetErrorMessageAndLongIntAndBail("OpenSSL: Can't initiate SSL connection, SSL_connect() = ",errCode);
  83. }
  84. // Request the URI from the host
  85. CopyCStrToCStr("GET ",tempString,sizeof(tempString));
  86. ConcatCStrToCStr(kHTTPS_URI,tempString,sizeof(tempString));
  87. ConcatCStrToCStr(" HTTP/1.0\r\n\r\n",tempString,sizeof(tempString));
  88. errCode = SSL_write(ssl,tempString,CStrLength(tempString));
  89. if (errCode < 0)
  90. {
  91. SetErrorMessageAndLongIntAndBail("OpenSSL: Error writing data via ssl, SSL_write() = ",errCode);
  92. }
  93. for (;;)
  94. {
  95. char tempString[256];
  96. int bytesRead;
  97. // Read some bytes and dump them to the console
  98. bytesRead = SSL_read(ssl,tempString,sizeof(tempString) - 1);
  99. if (bytesRead == 0 && MacSocket_RemoteEndIsClosing(theSocket))
  100. {
  101. break;
  102. }
  103. else if (bytesRead < 0)
  104. {
  105. SetErrorMessageAndLongIntAndBail("OpenSSL: Error reading data via ssl, SSL_read() = ",bytesRead);
  106. }
  107. tempString[bytesRead] = '\0';
  108. printf("%s", tempString);
  109. }
  110. printf("\n\n\n");
  111. // All done!
  112. errCode = noErr;
  113. EXITPOINT:
  114. // Clean up and go home
  115. if (theSocket >= 0)
  116. {
  117. MacSocket_close(theSocket);
  118. }
  119. if (ssl != nil)
  120. {
  121. SSL_free(ssl);
  122. }
  123. if (ssl_ctx != nil)
  124. {
  125. SSL_CTX_free(ssl_ctx);
  126. }
  127. if (errCode != noErr)
  128. {
  129. printf("An error occurred:\n");
  130. printf("%s",GetErrorMessage());
  131. }
  132. MacSocket_Shutdown();
  133. }