Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ssl_constructs.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Note: This file still needs some work.
  2. // Note: I had to redefine the enums to a set of const values,
  3. // so that the size of the variable would be correct.
  4. // Typedefs
  5. // (As defined by the SSL v3.0 RFC Draft)
  6. // URL: http://wp.netscape.com/eng/ssl3/draft302.txt
  7. typedef unsigned char uint8;
  8. typedef uint8 uint16[2];
  9. typedef uint8 uint24[3];
  10. typedef uint8 uint32[4];
  11. typedef uint8 uint64[8];
  12. // Record layers
  13. typedef struct _ProtocolVersion{
  14. uint8 major, minor;
  15. } ProtocolVersion;
  16. const ProtocolVersion version = { 3, 0 };
  17. typedef uint8 ContentType;
  18. const ContentType content_type_change_cipher_spec_type = 20;
  19. const ContentType content_type_alert = 21;
  20. const ContentType content_type_handshake = 22;
  21. const ContentType content_type_application_data = 23;
  22. typedef struct _SSLPlaintext{
  23. ContentType type;
  24. ProtocolVersion version;
  25. uint16 length; // can not exceed 2^14 bytes
  26. uint8 fragment[16384]; // 2^14 = 16,384 bytes
  27. } SSLPlaintext;
  28. typedef struct _SSLCompressed{
  29. ContentType type;
  30. ProtocolVersion version;
  31. uint16 length; // can not exceed 2^14 + 1024
  32. uint8 fragment[17408]; // SSLCompressed.length
  33. } SSLCompressed;
  34. typedef struct _SSLCiphertext{
  35. ContentType type;
  36. ProtocolVersion version;
  37. uint16 length;
  38. uint8 fragment; // so we have a pointer to the data, and don't have to do math
  39. // fragment; type GenericStreamCipher or GenericBlockCipher
  40. } SSLCiphertext; // recast to get fragment
  41. typedef struct _GenericStreamCipher{
  42. uint8 content[17408]; // SSLCompressed.length
  43. uint8 MAC[]; // CipherSpec.hash_size
  44. } GenericStreamCipher;
  45. typedef struct _SSLStreamCiphertext{
  46. ContentType type;
  47. ProtocolVersion version;
  48. uint16 length; // can not exceed 2^14 + 2048 = 18,456
  49. GenericStreamCipher fragment;
  50. } SSLStreamCiphertext;
  51. typedef struct _GenericBlockCipher{
  52. uint8 content[17408]; // SSLConpressed.length
  53. uint8 MAC[0]; // CipherSpec.hash_size
  54. // padding is used to bring the plaintext to
  55. // a multiple of the block cipher's block length.
  56. uint8 padding[0]; // GenericBlockCipher.padding_length
  57. uint8 padding_length;
  58. } GenericBlockCipher;
  59. typedef struct _SSLBlockCiphertext{
  60. ContentType type;
  61. ProtocolVersion version;
  62. uint16 length; // can not exceed 2^14 + 2048 = 18,456
  63. GenericBlockCipher fragment;
  64. } SSLBlockCiphertext;
  65. // Change cipher specs message
  66. typedef struct _ChangeCipherSpec{
  67. enum { type_change_cipher_spec=1, type_size=255 } type;
  68. } ChangeCipherSpec;
  69. // Alert messages
  70. typedef uint8 AlertLevel;
  71. const AlertLevel alert_level_warning = 1;
  72. const AlertLevel alert_level_fatal=2;
  73. typedef uint8 AlertDescription;
  74. const AlertDescription alert_description_close_notify = 0;
  75. const AlertDescription alert_description_unexpected_message = 10;
  76. const AlertDescription alert_description_bad_record_mac = 20;
  77. const AlertDescription alert_description_decompression_failure = 30;
  78. const AlertDescription alert_description_handshake_failure = 40;
  79. const AlertDescription alert_description_no_certificate = 41;
  80. const AlertDescription alert_description_bad_certificate = 42;
  81. const AlertDescription alert_description_unsupported_certificate = 43;
  82. const AlertDescription alert_description_certificate_revoked = 44;
  83. const AlertDescription alert_description_certificate_expired = 45;
  84. const AlertDescription alert_description_certificate_unknown = 46;
  85. const AlertDescription alert_description_illegal_parameter = 47;
  86. typedef struct _Alert{
  87. AlertLevel level;
  88. AlertDescription description;
  89. } Alert;
  90. // Handshake protocol
  91. // What is the best way to have a generic pointer to the body struct??
  92. typedef uint8 HandshakeType;
  93. const HandshakeType handshake_type_hello_request = 0;
  94. const HandshakeType handshake_type_client_hello = 1;
  95. const HandshakeType handshake_type_server_hello = 2;
  96. const HandshakeType handshake_type_certificate = 11;
  97. const HandshakeType handshake_type_server_key_exchange = 12;
  98. const HandshakeType handshake_type_certificate_request = 13;
  99. const HandshakeType handshake_type_server_done = 14;
  100. const HandshakeType handshake_type_certificate_verify = 15;
  101. const HandshakeType handshake_type_client_key_exchange = 16;
  102. const HandshakeType handshake_type_finished = 20;
  103. typedef struct _Handshake{
  104. HandshakeType msg_type;
  105. uint24 length;
  106. // body; // one of HandshakeType structs
  107. } Handshake; // generic Handshake, need to recast to get body
  108. // Hello messages
  109. typedef struct _HelloRequest{} HelloRequest;
  110. typedef struct _HelloRequestHandshake{
  111. HandshakeType msg_type;
  112. uint24 length;
  113. HelloRequest body;
  114. } HelloRequestHandshake;
  115. typedef struct _Random{
  116. uint32 gmt_unix_time;
  117. uint8 random_bytes[28];
  118. } Random;
  119. //typedef uint8 SessionID[32]; // <0..32>
  120. typedef uint8 SessionIDLength;
  121. typedef uint8 SessionID;
  122. typedef uint16 CipherSuiteLength;
  123. typedef uint8 CipherSuite[2];
  124. typedef uint8 CompressionMethodLength;
  125. typedef uint8 CompressionMethod;
  126. const CompressionMethod compression_method_null = 0;
  127. typedef struct _ClientHello{
  128. ProtocolVersion client_version;
  129. Random random;
  130. SessionIDLength session_id_length;
  131. SessionID *session_id;
  132. SessionID *session_id_end;
  133. CipherSuiteLength *cipher_suites_length;
  134. CipherSuite *cipher_suites; // min size is one entry
  135. CipherSuite *cipher_suites_end;
  136. //CipherSuite cipher_suites[32768]; // <2..2^16-1> = 65,536 bytes and CipherSuite is 2 bytes
  137. CompressionMethodLength *compression_methods_length;
  138. CompressionMethod *compression_methods;
  139. CompressionMethod *compression_methods_end;
  140. //CompressionMethod *compression_methods; // min size is zero
  141. //CompressionMethod compression_methods[256]; // <0..2^8-1> = 256 bytes and CompressionMethod is 1 byte
  142. } ClientHello;
  143. typedef struct _ClientHelloHandshake{
  144. //HandshakeType msg_type;
  145. uint8 msg_type;
  146. uint24 length;
  147. ClientHello body;
  148. } ClientHelloHandshake;
  149. typedef struct _ServerHello{
  150. ProtocolVersion server_version;
  151. Random random;
  152. SessionID session_id;
  153. CipherSuite cipher_suite;
  154. CompressionMethod compression_method;
  155. } ServerHello;
  156. typedef struct _ServerHelloHandshake{
  157. HandshakeType msg_type;
  158. uint24 length;
  159. ServerHello body;
  160. } ServerHelloHandshake;
  161. // Server authentication and key exchange messages
  162. typedef uint8 ASN1Cert[16777216]; // <1..2^24-1> = 16,777,216 bytes
  163. typedef struct _Certificate{
  164. ASN1Cert certificate_list[1]; // <1..2^24-1> / ANS1Cert = 1
  165. // for some reason the size of certificate_list and ASN1Cert is the same, so only one certificate in the list
  166. } Certificate;
  167. typedef uint8 KeyExchangeAlgorithm;
  168. const KeyExchangeAlgorithm key_exchange_algorithm_rsa = 0;
  169. const KeyExchangeAlgorithm key_exchange_algorithm_diffie_hellman = 1;
  170. const KeyExchangeAlgorithm key_exchange_algorithm_fortezza_kea = 2;
  171. typedef struct _AnonSignature{
  172. struct {};
  173. } AnonSignature;
  174. typedef struct _RSASignature{
  175. uint8 md5_hash[16];
  176. uint8 sha_hash[20];
  177. } RSASignature;
  178. typedef struct _DSASignature{
  179. uint8 sha_hash[20];
  180. } DSASignature;
  181. // use union??, make a mess to reference, but easy to make Signature type.
  182. typedef union _Signature{ AnonSignature anon; RSASignature rsa; DSASignature dsa; } Signature;
  183. typedef struct _ServerRSAParams{
  184. uint8 RSA_modulus[65536]; // <1..2^16-1> = 65,536
  185. uint8 RSA_exponent[65536]; // <1..2^16-1> = 65,536
  186. } ServerRSAParams;
  187. typedef struct _ServerDHParams{
  188. uint8 DH_p[65536]; // <1..2^16-1>
  189. uint8 DH_g[65536]; // <1..2^16-1>
  190. uint8 DH_Ys[65536]; // <1..2^16-1>
  191. } ServerDHParams;
  192. typedef struct _ServerDHKeyExchange{
  193. ServerDHParams params;
  194. Signature signed_params;
  195. } ServerDHKeyExchange;
  196. typedef struct _ServerRSAKeyExchange{
  197. ServerRSAParams params;
  198. Signature signed_params;
  199. } ServerRSAKeyExchange;
  200. typedef uint8 SignatureAlgorithm;
  201. const SignatureAlgorithm signature_algorithm_anonymous = 0;
  202. const SignatureAlgorithm signature_algorithm_rsa = 1;
  203. const SignatureAlgorithm signature_algorithm_dsa = 2;
  204. typedef uint8 CertificateType;
  205. const CertificateType certificate_type_RSA_sign = 1;
  206. const CertificateType certificate_type_DSS_sign = 2;
  207. const CertificateType certificate_type_RSA_fixed_DH = 3;
  208. const CertificateType certificate_type_DSS_fixed_DH = 4;
  209. const CertificateType certificate_type_RSA_ephemeral_DH = 5;
  210. const CertificateType certificate_type_DSS_ephemeral_DH = 6;
  211. const CertificateType certificate_type_FORTEZZA_MISSI = 20;
  212. typedef uint8 DistinguishedName[65536]; // <1..2^16-1> = 65,536
  213. typedef struct _CertificateRequest{
  214. CertificateType certificate_types[256]; // <1..2^8-1>
  215. DistinguishedName certificate_authorities[1]; // <3...2^16-1> / DistinguishedName
  216. // this is another one that is odd with a list size of 1
  217. } CertificateRequest;
  218. typedef struct _ServerHelloDone{} ServerHelloDone;
  219. // Client authentication and key exchange messages
  220. typedef struct _PreMasterSecret{
  221. ProtocolVersion client_version;
  222. uint8 random[46];
  223. } PreMasterSecret;
  224. typedef struct _EncryptedPreMasterSecret{
  225. PreMasterSecret pre_master_secret;
  226. } EncryptedPreMasterSecret;
  227. typedef struct _RSAClientKeyExchange{
  228. EncryptedPreMasterSecret exchange_keys;
  229. } RSAClientKeyExchange;
  230. typedef uint8 PublicValueEncoding;
  231. const PublicValueEncoding public_value_encoding_implicit = 0;
  232. const PublicValueEncoding public_value_encoding_explicit = 1;
  233. typedef struct _ClientDiffieHellmanPublic{
  234. // This is a select on PublicValueEncoding, and I chose the larger size
  235. uint8 dh_public[65536]; // DH_Yc<1..2^16-1>, the dh public value
  236. } ClientDiffieHellmanPublic;
  237. typedef struct _DHClientKeyExhange{
  238. ClientDiffieHellmanPublic exchange_keys;
  239. } DHClientKeyExchange;
  240. typedef struct _CertificateVerify{
  241. Signature signature;
  242. } CertificateVerify;
  243. // Handshake finalization message
  244. typedef struct _Finished{
  245. uint8 md5_hash[16];
  246. uint8 sha_hash[20];
  247. } Finished;
  248. // The CipherSuite
  249. CipherSuite SSL_NULL_WITH_NULL_NULL = { 0x00, 0x13 };
  250. CipherSuite SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA = { 0x00, 0x0B };
  251. CipherSuite SSL_DH_DSS_WITH_DES_CBC_SHA = { 0x00, 0x0C };
  252. CipherSuite SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA = { 0x00, 0x11 };
  253. CipherSuite SSL_DH_anon_EXPORT_WITH_RC4_40_MD5 = { 0x00, 0x17 };
  254. CipherSuite SSL_DH_anon_WITH_RC4_128_MD5 = { 0x00, 0x18 };
  255. // The CipherSpec
  256. typedef uint8 CipherType;
  257. const CipherType cipher_type_stream = 0;
  258. const CipherType cipher_type_block = 1;
  259. typedef uint8 IsExportable;
  260. const IsExportable is_exportable_true = 0;
  261. const IsExportable is_exportable_false = 1;
  262. typedef uint8 BulkCipherAlgorithm;
  263. const BulkCipherAlgorithm bulk_cipher_algorithm_null = 0;
  264. const BulkCipherAlgorithm bulk_cipher_algorithm_rc4 = 1;
  265. const BulkCipherAlgorithm bulk_cipher_algorithm_rc2 = 2;
  266. const BulkCipherAlgorithm bulk_cipher_algorithm_des = 3;
  267. const BulkCipherAlgorithm bulk_cipher_algorithm_3des = 4;
  268. const BulkCipherAlgorithm bulk_cipher_algorithm_des40 = 5;
  269. const BulkCipherAlgorithm bulk_cipher_algorithm_fortezza = 6;
  270. typedef uint8 MACAlgorithm;
  271. const MACAlgorithm mac_algorithm_null = 0;
  272. const MACAlgorithm mac_algorithm_md5 = 1;
  273. const MACAlgorithm mac_algorithm_sha = 2;
  274. typedef struct _CipherSpec{
  275. BulkCipherAlgorithm bulk_cipher_algorithm;
  276. MACAlgorithm mac_algorithm;
  277. CipherType cipher_type;
  278. IsExportable is_exportable;
  279. uint8 hash_size;
  280. uint8 key_material;
  281. uint8 IV_size;
  282. } CipherSpec;