You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssl.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "ssl.h"
  2. #include "ssl_constructs.h"
  3. #include <string.h> // for bcopy()
  4. #include <time.h> // for time()
  5. #include <stdlib.h> // for rand(), htons?, htonl?
  6. // note net byte order is big-endian
  7. // Need to set error codes
  8. int CreateSSLHello(SSL_t *ssl)
  9. {
  10. printf("In CreateSSLHello()\n",ssl);
  11. // Initalize the structure
  12. bzero(ssl,sizeof(SSL_t));
  13. //ssl->max_size = sizeof(ssl->buffer);
  14. ssl->max_size = 18456;
  15. // Declare variables
  16. int i; void *ptr;
  17. // Set pointers into buffer
  18. SSLPlaintext *record = (SSLPlaintext *)ssl->buffer;
  19. Handshake *handshake = (Handshake *)record->fragment;
  20. // the body starts right after the handshake
  21. printf("sizeof(Handshake) = %d\n",sizeof(Handshake));
  22. ClientHello *hello = (ClientHello *)(handshake + 1);
  23. printf("record->%#x, handshake->%#x, hello->%#x\n",record,handshake,hello);
  24. // Construct ClientHello Message
  25. hello->client_version = version;
  26. i = htonl(time(NULL));
  27. bcopy(&i,hello->random.gmt_unix_time,4);
  28. for(i=0;i<28;i++){ hello->random.random_bytes[i] = (uint8)rand(); }
  29. hello->session_id_length = 0;
  30. hello->session_id = &hello->session_id_length;
  31. hello->session_id_end = hello->session_id;
  32. hello->cipher_suites_length = (CipherSuiteLength *)(hello->session_id_end + 1);
  33. hello->cipher_suites = (hello->cipher_suites_length + 1);
  34. hello->cipher_suites_end = hello->cipher_suites;
  35. i = htons(2*5); // 2 bytes per Suite * 5 Suites
  36. bcopy(&i,hello->cipher_suites_length,2);
  37. bcopy(SSL_NULL_WITH_NULL_NULL,hello->cipher_suites_end,sizeof(CipherSuite));
  38. *hello->cipher_suites_end++;
  39. bcopy(SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA,hello->cipher_suites_end,sizeof(CipherSuite));
  40. *hello->cipher_suites_end++;
  41. bcopy(SSL_DH_DSS_WITH_DES_CBC_SHA,hello->cipher_suites_end,sizeof(CipherSuite));
  42. *hello->cipher_suites_end++;
  43. bcopy(SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA,hello->cipher_suites_end,sizeof(CipherSuite));
  44. *hello->cipher_suites_end++;
  45. bcopy(SSL_DH_anon_WITH_RC4_128_MD5,hello->cipher_suites_end,sizeof(CipherSuite));
  46. hello->compression_methods_length = (CompressionMethodLength *)(hello->cipher_suites_end + 1);
  47. hello->compression_methods = (hello->compression_methods_length + 1);
  48. hello->compression_methods_end = hello->compression_methods;
  49. *hello->compression_methods_length = 1;
  50. *hello->compression_methods_end = compression_method_null;
  51. // Construct Handshake Message
  52. handshake->msg_type = handshake_type_client_hello;
  53. i = (void *)(hello->compression_methods_end + 1) - (void *)hello;
  54. printf("Handshake.length = %d\n", i);
  55. handshake->length[0] = (char)*(&i+8);
  56. handshake->length[1] = (char)*(&i+8);
  57. handshake->length[2] = (char)i;
  58. //bcopy((&i+1),handshake->length,3); // +1 so we copy 3 bytes
  59. // Construct SSL Record
  60. printf("sizeof(ContentType)=%d\n",sizeof(ContentType));
  61. printf("sizeof(uint8)=%d\n",sizeof(uint8));
  62. record->type = content_type_handshake;
  63. record->version = version;
  64. i += sizeof(Handshake);
  65. printf("SSLPlaintext.length = %d\n",i);
  66. record->length[0] = (char)*(&i+8);
  67. record->length[1] = (char)i;
  68. //bcopy(&i,record->length,4); // length of handshake
  69. // Set total size of message
  70. i += sizeof(ContentType) + sizeof(ProtocolVersion) + sizeof(uint16);
  71. ssl->length = i;
  72. printf("End of CreateSSLHello\n");
  73. return 0;
  74. }
  75. void PrintSSLPacket(SSL_t *ssl)
  76. {
  77. printf("Printing packet with length:%d\n", ssl->length);
  78. char *ptr = ssl->buffer;
  79. char *begin = ptr;
  80. char *tmp;
  81. char *end = ssl->buffer + ssl->length;
  82. printf("Record Layer:\n");
  83. printf("\tContentType: %2hhX\n",(char)*ptr++);
  84. printf("\tVersion: %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++);
  85. printf("\tLength: %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++);
  86. printf("Handshake:\n");
  87. printf("\tType: %2hhX\n", (char)*ptr++);
  88. printf("\tLength: %2hhX %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++, (char)*ptr++);
  89. printf("\tVersion: %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++);
  90. printf("\tgmt_unix_time: %2hhX %2hhX %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++, (char)*ptr++, (char)*ptr++);
  91. printf("\trandom: ");
  92. tmp = ptr + 28;
  93. for(;ptr<tmp;ptr++){printf("%2hhX ", (char)*ptr);}
  94. printf("\n\nHexDump:\n");
  95. int ctr = 0;
  96. for(;begin<end;begin++){printf("%2hhX ",(char)*begin);if(++ctr%10==0){printf("\n");}}
  97. printf("\n\n");
  98. }
  99. int ReadSSLHello(SSL_t *ssl)
  100. {
  101. SSLCiphertext *ct = (SSLCiphertext *)ssl->buffer;
  102. if(ct->type == content_type_alert){
  103. // assuming text is still plaintext
  104. Alert *a = (Alert *)&ct->fragment;
  105. if(a->level == alert_level_fatal){
  106. printf("Fatal Alert %d, connection terminated\n",a->description);
  107. return (1);
  108. }else if(a->level == alert_level_warning){
  109. printf("Warning Alert %d\n", a->description);
  110. }else{
  111. printf("Unknown alert level %d\n", a->level);
  112. }
  113. }else{
  114. printf("SSL type %d\n",ct->type);
  115. }
  116. return (0);
  117. }