wtls_pdu.h
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:7k
源码类别:

手机WAP编程

开发平台:

WINDOWS

  1. /* wtls_pdu.h - definitions for unpacked WTLS protocol data units
  2.  *
  3.  */
  4. #ifndef PDU_H
  5. #define PDU_H
  6. #include "gwlib/list.h"
  7. #include "gwlib/octstr.h"
  8. #include "wtls.h"
  9. typedef enum wtls_pdu_modes {
  10. ChangeCipher_PDU = 1,
  11. Alert_PDU,
  12. Handshake_PDU,
  13. Application_PDU
  14. } wtls_pdu_modes;
  15. typedef enum handshake_type{
  16. hello_request = 0,
  17. client_hello = 1,
  18. server_hello = 2,
  19. certificate = 11,
  20. server_key_exchange = 12,
  21. certificate_request = 13,
  22. server_hello_done = 14,
  23. certificate_verify = 15,
  24. client_key_exchange = 16,
  25. finished = 20
  26. } HandshakeType;
  27. typedef enum compmethod {
  28. null_comp = 0
  29. } CompressionMethod;
  30. typedef enum certificateformat {
  31. WTLSCert = 1,
  32. X509Cert,
  33. X968Cert
  34. } CertificateFormat;
  35. typedef enum sig_algo {
  36. anonymous,
  37. ecdsa_sha,
  38. rsa_sha,
  39. } SignatureAlgorithm;
  40. /*typedef enum keyex_algo {
  41. rsa,
  42. rsa_anon,
  43. dh_anon,
  44. ecdh_anon,
  45. ecdh_ecdsa,
  46. } KeyExchangeAlgorithm;*/
  47. typedef enum keyex_suite {
  48. null_k,
  49. shared_secret,
  50. dh_anon,
  51. dh_anon_512,
  52. dh_anon_768,
  53. rsa_anon,
  54. rsa_anon_512,
  55. rsa_anon_768,
  56. rsa,
  57. rsa_512,
  58. rsa_768,
  59. ecdh_anon,
  60. ecdh_anon_113,
  61. ecdh_anon_131,
  62. ecdh_ecdsa,
  63. } KeyExchangeSuite;
  64. typedef enum pubkey_algo {
  65. rsa_pubkey,
  66. diffie_hellman_pubkey,
  67. elliptic_curve_pubkey,
  68. } PublicKeyAlgorithm;
  69. typedef enum identifier_type {
  70. null = 0,
  71. text,
  72. binary,
  73. key_hash_sha = 254,
  74. x509_name = 255
  75. } IdentifierType;
  76. typedef enum public_key_type {
  77. rsa_key = 2,
  78. ecdh_key = 3,
  79. ecdsa_key = 4
  80. } PublicKeyType;
  81. typedef enum ecbasistype {
  82. ec_basis_onb = 1,
  83. ec_basis_trinomial,
  84. ec_basis_pentanomial,
  85. ec_basis_polynomial
  86. } ECBasisType;
  87. typedef enum ecfield {
  88. ec_prime_p,
  89. ec_characteristic_two
  90. } ECField;
  91. typedef struct random {
  92. long gmt_unix_time;
  93. Octstr *random_bytes;
  94. } Random;
  95. typedef struct ecpoint {
  96. Octstr *point;
  97. } ECPoint;
  98. typedef ECPoint ECPublicKey;
  99. typedef struct dhpublickey {
  100. Octstr *dh_Y;
  101. } DHPublicKey;
  102. typedef struct rsa_public_key {
  103. Octstr *rsa_exponent;
  104. Octstr *rsa_modulus;
  105. } RSAPublicKey;
  106. typedef struct public_key {
  107. /* ecdh */
  108. ECPublicKey *ecdh_pubkey;
  109. /* ecdsa */
  110. ECPublicKey *ecdsa_pubkey;
  111. /* rsa */
  112. RSAPublicKey *rsa_pubkey;
  113. } PublicKey;
  114. typedef struct identifier {
  115. IdentifierType id_type;
  116. /* text */
  117. int charset;
  118. Octstr *name;
  119. /* binary */
  120. Octstr *identifier;
  121. /* key_hash_sha */
  122. Octstr *key_hash;
  123. /* x509 */
  124. Octstr *distinguished_name;
  125. } Identifier;
  126. typedef struct eccurve {
  127. Octstr *a;
  128. Octstr *b;
  129. Octstr *seed;
  130. } ECCurve;
  131. typedef struct dh_parameters{
  132. int dh_e;
  133. Octstr *dh_p;
  134. Octstr *dh_g;
  135. } DHParameters;
  136. typedef struct ec_parameters{
  137. ECField field;
  138. /* case ec_prime_p */
  139. Octstr *prime_p;
  140. /* case ec_characteristic_two */
  141. int m;
  142. ECBasisType basis;
  143. /* case ec_basis_onb : nothing*/
  144. /* case ec_trinomial */
  145. int k;
  146. /* case ec_pentanomial */
  147. int k1;
  148. int k2;
  149. int k3;
  150. /* case ec_basis_polynomial */
  151. Octstr *irreducible;
  152. ECCurve *curve;
  153. ECPoint *base;
  154. Octstr *order;
  155. Octstr *cofactor;
  156. } ECParameters;
  157. typedef struct parameter_set {
  158. long length;
  159. /* rsa: empty */
  160. /* diffie-hellman */
  161. DHParameters *dhparams;
  162. /* eliptic curve */
  163. ECParameters *ecparams;
  164. } ParameterSet;
  165. typedef struct parameter_specifier {
  166. int param_index;
  167. ParameterSet *param_set;
  168. } ParameterSpecifier;
  169. typedef struct key_exchange_id {
  170. int key_exchange_suite;
  171. ParameterSpecifier *param_specif;
  172. Identifier *identifier;
  173. } KeyExchangeId;
  174. typedef struct signature {
  175. /* case anonymous */
  176. /* nothing */
  177. /* case ecdsa_sha and rsa_sha */
  178. List *sha_hash;
  179. } Signature;
  180. typedef struct to_be_signed_cert {
  181. int certificate_version;
  182. SignatureAlgorithm signature_algo;
  183. Identifier *issuer;
  184. long valid_not_before;
  185. long valid_not_after;
  186. Identifier *subject;
  187. PublicKeyType pubkey_type;
  188. ParameterSpecifier *param_spec;
  189. PublicKey *pubkey;
  190. } ToBeSignedCertificate;
  191. typedef struct wtls_cert {
  192. ToBeSignedCertificate *tobesigned_cert;
  193. Signature *signature;
  194. } WTLSCertificate;
  195. typedef struct rsa_secret{
  196. int client_version;
  197. List *random;
  198. } RSASecret;
  199. typedef struct rsa_encrypted_secret {
  200. Octstr *encrypted_secret;
  201. } RSAEncryptedSecret;
  202. typedef struct cipher_suite {
  203. int bulk_cipher_algo;
  204. int mac_algo;
  205. } CipherSuite;
  206. typedef struct cert_request {
  207. List *trusted_authorities; // List of KeyExchangeIds
  208. } CertificateRequest;
  209. typedef struct cert_verify {
  210. Signature *signature;
  211. } CertificateVerify;
  212. typedef struct hello_request
  213. {
  214. /* nothing here */
  215. } HelloRequest;
  216. typedef struct client_hello
  217. {
  218. int clientversion;
  219. Random *random;
  220. Octstr *session_id;
  221. List *client_key_ids;
  222. List *trusted_key_ids;
  223. List *ciphersuites; // list of CipherSuites
  224. List *comp_methods;
  225. int snmode;
  226. int krefresh;
  227. } ClientHello;
  228. typedef struct server_hello
  229. {
  230. int serverversion;
  231. Random *random;
  232. Octstr *session_id;
  233. int client_key_id;
  234. CipherSuite *ciphersuite;
  235. CompressionMethod comp_method;
  236. int snmode;
  237. int krefresh;
  238. } ServerHello;
  239. typedef struct certificate {
  240. CertificateFormat certificateformat;
  241. /* case WTLS */
  242. WTLSCertificate *wtls_certificate;
  243. /* case X509 */
  244. Octstr *x509_certificate;
  245. /* X968 */
  246. Octstr *x968_certificate;
  247. } Certificate;
  248. typedef struct server_key_exchange
  249. {
  250. ParameterSpecifier *param_spec;
  251. /* case rsa_anon */
  252. RSAPublicKey *rsa_params;
  253. /* case dh_anon */
  254. DHPublicKey *dh_params;
  255. /* case ecdh_anon */
  256. ECPublicKey *ecdh_params;
  257. } ServerKeyExchange;
  258. typedef struct client_key_exchange
  259. {
  260. /* case rsa and rsa_anon*/
  261. RSAEncryptedSecret *rsa_params;
  262. /* case dh_anon */
  263. DHPublicKey *dh_anon_params;
  264. /* case ecdh_anon and ecdh_ecdsa*/
  265. ECPublicKey *ecdh_params;
  266. } ClientKeyExchange;
  267. typedef struct finished {
  268. Octstr *verify_data;
  269. } Finished;
  270. typedef struct server_hello_done
  271. {
  272. /* nothing here */
  273. } ServerHelloDone;
  274. typedef struct cc
  275. {
  276. int change;
  277. } ChangeCipher;
  278. typedef struct alert
  279. {
  280. int level;
  281. int desc;
  282. Octstr *chksum;
  283. } Alert;
  284. typedef struct handshake
  285. {
  286. HandshakeType msg_type;
  287. int length;
  288. /* case hello_request */
  289. /* case client_hello */
  290. ClientHello *client_hello;
  291. /* case server_hello */
  292. ServerHello *server_hello;
  293. /* case certificate */
  294. Certificate *certificate;
  295. /* case server_key_exchange */
  296. ServerKeyExchange *server_key_exchange;
  297. /* case certificate_request */
  298. CertificateRequest *certificate_request;
  299. /* case server_hello_done */
  300. ServerHelloDone *server_hello_done;
  301. /* case certificate_verify */
  302. CertificateVerify *cert_verify;
  303. /* case client_key_exchange */
  304. ClientKeyExchange *client_key_exchange;
  305. /* case finished */
  306. Finished *finished;
  307. } Handshake;
  308. typedef struct application
  309. {
  310. Octstr *data;
  311. } Application;
  312. typedef struct wtls_pdu {
  313. int type;
  314. int reserved;
  315. int cipher;
  316. int seqnum;
  317. int rlen;
  318.         
  319. union {
  320. ChangeCipher cc;
  321. Alert alert;
  322. Handshake handshake;
  323. Application application;
  324. } u;
  325. } wtls_PDU;
  326. typedef struct wtls_payload {
  327. int type;
  328. int reserved;
  329. int cipher;
  330. int seqnum;
  331. int rlen;
  332. Octstr *data;
  333. } wtls_Payload;
  334. /* Prototypes */
  335. wtls_PDU *wtls_pdu_create(int type);
  336. void wtls_pdu_destroy(wtls_PDU *msg);
  337. void wtls_pdu_dump(wtls_PDU *msg, int level);
  338. wtls_PDU *wtls_pdu_unpack(wtls_Payload *payload, WTLSMachine* wtls_machine);
  339. wtls_Payload *wtls_pdu_pack(wtls_PDU *pdu, WTLSMachine* wtls_machine);
  340. wtls_Payload *wtls_payload_unpack(Octstr *data);
  341. Octstr *wtls_payload_pack(wtls_Payload *payload);
  342. void wtls_payload_destroy(wtls_Payload *payload);
  343. List* wtls_unpack_payloadlist (Octstr *data);
  344. Octstr* wtls_pack_payloadlist (List* payloadlist);
  345. #endif