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

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