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