tlstest.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/oid.h>
  13. #include <gmssl/x509.h>
  14. #include <gmssl/rand.h>
  15. #include <gmssl/error.h>
  16. #include <gmssl/tls.h>
  17. #include <gmssl/sm3.h>
  18. #include <gmssl/sm4.h>
  19. static int test_tls_encode(void)
  20. {
  21. uint8_t a1 = 200;
  22. uint16_t a2 = 30000;
  23. uint24_t a3 = 4000000;
  24. uint32_t a4 = 4000000000;
  25. uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
  26. uint8_t r1;
  27. uint16_t r2;
  28. uint24_t r3;
  29. uint32_t r4;
  30. const uint8_t *pdata;
  31. size_t datalen;
  32. uint8_t buf[256];
  33. uint8_t *p = buf;
  34. const uint8_t *cp = buf;
  35. size_t len = 0;
  36. tls_uint8_to_bytes(a1, &p, &len);
  37. tls_uint16_to_bytes(a2, &p, &len);
  38. tls_uint24_to_bytes(a3, &p, &len);
  39. tls_uint32_to_bytes(a4, &p, &len);
  40. tls_uint8array_to_bytes(data, 5, &p, &len);
  41. tls_uint16array_to_bytes(data, 6, &p, &len);
  42. tls_uint24array_to_bytes(data, 7, &p, &len);
  43. if (tls_uint8_from_bytes(&r1, &cp, &len) != 1 || r1 != a1
  44. || tls_uint16_from_bytes(&r2, &cp, &len) != 1 || r2 != a2
  45. || tls_uint24_from_bytes(&r3, &cp, &len) != 1 || r3 != a3
  46. || tls_uint32_from_bytes(&r4, &cp, &len) != 1 || r4 != a4
  47. || tls_uint8array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 5 || memcmp(pdata, data, 5) != 0
  48. || tls_uint16array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 6 || memcmp(pdata, data, 6) != 0
  49. || tls_uint24array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 7 || memcmp(pdata, data, 7) != 0
  50. || len > 0) {
  51. error_print();
  52. return -1;
  53. }
  54. printf("%s() ok\n", __FUNCTION__);
  55. return 1;
  56. }
  57. static int test_tls_cbc(void)
  58. {
  59. uint8_t key[32] = {0};
  60. SM3_HMAC_CTX hmac_ctx;
  61. SM4_KEY sm4_key;
  62. uint8_t seq_num[8] = { 0,0,0,0,0,0,0,1 };
  63. uint8_t header[5];
  64. uint8_t in[] = "hello world";
  65. uint8_t out[256];
  66. uint8_t buf[256] = {0};
  67. size_t len;
  68. size_t buflen;
  69. header[0] = TLS_record_handshake;
  70. header[1] = TLS_protocol_tls12 >> 8;
  71. header[2] = TLS_protocol_tls12 & 0xff;
  72. header[3] = sizeof(in) >> 8;
  73. header[4] = sizeof(in) & 0xff;
  74. sm3_hmac_init(&hmac_ctx, key, 32);
  75. sm4_set_encrypt_key(&sm4_key, key);
  76. tls_cbc_encrypt(&hmac_ctx, &sm4_key, seq_num, header, in, sizeof(in), out, &len);
  77. sm3_hmac_init(&hmac_ctx, key, 32);
  78. sm4_set_decrypt_key(&sm4_key, key);
  79. tls_cbc_decrypt(&hmac_ctx, &sm4_key, seq_num, header, out, len, buf, &buflen);
  80. printf("%s() ok\n", __FUNCTION__);
  81. return 1;
  82. }
  83. static int test_tls_random(void)
  84. {
  85. uint8_t random[32];
  86. tls_random_generate(random);
  87. tls_random_print(stdout, random, 0, 0);
  88. printf("%s() ok\n", __FUNCTION__);
  89. return 1;
  90. }
  91. static int test_tls_client_hello(void)
  92. {
  93. uint8_t record[512];
  94. size_t recordlen = 0;
  95. int version = TLS_protocol_tlcp;
  96. uint8_t random[32];
  97. int cipher_suites[] = {
  98. TLS_cipher_ecc_sm4_cbc_sm3,
  99. TLS_cipher_ecc_sm4_gcm_sm3,
  100. TLS_cipher_ecdhe_sm4_cbc_sm3,
  101. TLS_cipher_ecdhe_sm4_gcm_sm3,
  102. TLS_cipher_ibsdh_sm4_cbc_sm3,
  103. TLS_cipher_ibsdh_sm4_gcm_sm3,
  104. TLS_cipher_ibc_sm4_cbc_sm3,
  105. TLS_cipher_ibc_sm4_gcm_sm3,
  106. TLS_cipher_rsa_sm4_cbc_sm3,
  107. TLS_cipher_rsa_sm4_gcm_sm3,
  108. TLS_cipher_rsa_sm4_cbc_sha256,
  109. TLS_cipher_rsa_sm4_gcm_sha256,
  110. };
  111. int comp_meths[] = {0};
  112. tls_record_set_protocol(record, TLS_protocol_tlcp);
  113. if (tls_record_set_handshake_client_hello(record, &recordlen,
  114. version,
  115. random,
  116. NULL, 0,
  117. cipher_suites, sizeof(cipher_suites)/sizeof(cipher_suites[0]),
  118. NULL, 0) != 1) {
  119. error_print();
  120. return -1;
  121. }
  122. tls_client_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 4);
  123. printf("%s() ok\n", __FUNCTION__);
  124. return 1;
  125. }
  126. static int test_tls_server_hello(void)
  127. {
  128. uint8_t record[512];
  129. size_t recordlen = 0;
  130. uint8_t random[32];
  131. uint16_t cipher_suite = TLS_cipher_ecdhe_sm4_cbc_sm3;
  132. tls_record_set_protocol(record, TLS_protocol_tlcp);
  133. if (tls_record_set_handshake_server_hello(record, &recordlen,
  134. TLS_protocol_tlcp,
  135. random,
  136. NULL, 0,
  137. cipher_suite,
  138. NULL, 0) != 1) {
  139. error_print();
  140. return -1;
  141. }
  142. tls_server_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 0);
  143. printf("%s() ok\n", __FUNCTION__);
  144. return 1;
  145. }
  146. static int test_tls_certificate(void)
  147. {
  148. size_t recordlen = 0;
  149. FILE *fp = NULL;
  150. // 测试函数不要有外部的依赖
  151. // TODO: 输出一些握手过程的record字节数组和handshake字节数组,作为后续测试的测试数据
  152. /*
  153. if (!(fp = fopen("cacert.pem", "r"))) {
  154. error_print();
  155. return -1;
  156. }
  157. if (tls_record_set_handshake_certificate_from_pem(record, &recordlen, fp) != 1) {
  158. error_print();
  159. return -1;
  160. }
  161. tls_certificate_print(stdout, record + 9, recordlen - 9, 0, 0);
  162. */
  163. printf("%s() ok\n", __FUNCTION__);
  164. return 1;
  165. }
  166. static int test_tls_server_key_exchange(void)
  167. {
  168. uint8_t record[1024];
  169. size_t recordlen = 0;
  170. uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0xAA, 0xBB};
  171. const uint8_t *psig;
  172. size_t siglen;
  173. tls_record_set_protocol(record, TLS_protocol_tlcp);
  174. if (tlcp_record_set_handshake_server_key_exchange_pke(record, &recordlen, sig, sizeof(sig)) != 1) {
  175. error_print();
  176. return -1;
  177. }
  178. if (tlcp_record_get_handshake_server_key_exchange_pke(record, &psig, &siglen) != 1) {
  179. error_print();
  180. return -1;
  181. }
  182. format_bytes(stdout, 0, 0, "server_key_exchange siganture", psig, siglen);
  183. printf("%s() ok\n", __FUNCTION__);
  184. return 1;
  185. }
  186. static int test_tls_certificate_verify(void)
  187. {
  188. uint8_t record[1024];
  189. size_t recordlen = 0;
  190. uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
  191. const uint8_t *psig;
  192. size_t siglen;
  193. tls_record_set_protocol(record, TLS_protocol_tls12);
  194. if (tls_record_set_handshake_certificate_verify(record, &recordlen, sig, sizeof(sig)) != 1) {
  195. error_print();
  196. return -1;
  197. }
  198. if (tls_record_get_handshake_certificate_verify(record, &psig, &siglen) != 1) {
  199. error_print();
  200. return -1;
  201. }
  202. tls_certificate_verify_print(stdout, psig, siglen, 0, 0);
  203. printf("%s() ok\n", __FUNCTION__);
  204. return 1;
  205. }
  206. static int test_tls_finished(void)
  207. {
  208. uint8_t record[1024];
  209. size_t recordlen = 0;
  210. uint8_t verify_data[12];
  211. const uint8_t *verify_data_ptr;
  212. size_t verify_data_len;
  213. if (tls_record_set_handshake_finished(record, &recordlen, verify_data, sizeof(verify_data)) != 1) {
  214. error_print();
  215. return -1;
  216. }
  217. if (tls_record_get_handshake_finished(record, &verify_data_ptr, &verify_data_len) != 1) {
  218. error_print();
  219. return -1;
  220. }
  221. tls_finished_print(stdout, verify_data_ptr, verify_data_len, 0, 0);
  222. printf("%s() ok\n", __FUNCTION__);
  223. return 1;
  224. }
  225. static int test_tls_alert(void)
  226. {
  227. uint8_t record[1024];
  228. size_t recordlen = 0;
  229. int level;
  230. int reason;
  231. if (tls_record_set_alert(record, &recordlen, TLS_alert_level_fatal, TLS_alert_close_notify) != 1) {
  232. error_print();
  233. return -1;
  234. }
  235. if (tls_record_get_alert(record, &level, &reason) != 1) {
  236. error_print();
  237. return -1;
  238. }
  239. tls_alert_print(stdout, record + 5, recordlen - 5, 0, 0);
  240. printf("%s() ok\n", __FUNCTION__);
  241. return 1;
  242. }
  243. static int test_tls_change_cipher_spec(void)
  244. {
  245. uint8_t record[1024];
  246. size_t recordlen = 0;
  247. if (tls_record_set_change_cipher_spec(record, &recordlen) != 1) {
  248. error_print();
  249. return -1;
  250. }
  251. if (tls_record_get_change_cipher_spec(record) != 1) {
  252. error_print();
  253. return -1;
  254. }
  255. tls_change_cipher_spec_print(stdout, record + 5, recordlen - 5, 0, 0);
  256. printf("%s() ok\n", __FUNCTION__);
  257. return 1;
  258. }
  259. static int test_tls_application_data(void)
  260. {
  261. uint8_t record[1024];
  262. size_t recordlen = 0;
  263. uint8_t data[88];
  264. const uint8_t *p;
  265. size_t len;
  266. if (tls_record_set_application_data(record, &recordlen, data, sizeof(data)) != 1) {
  267. error_print();
  268. return -1;
  269. }
  270. if (tls_record_get_application_data(record, &p, &len) != 1) {
  271. error_print();
  272. return -1;
  273. }
  274. tls_application_data_print(stdout, p, len, 0, 0);
  275. printf("%s() ok\n", __FUNCTION__);
  276. return 1;
  277. }
  278. int main(void)
  279. {
  280. if (test_tls_encode() != 1) goto err;
  281. if (test_tls_cbc() != 1) goto err;
  282. if (test_tls_random() != 1) goto err;
  283. if (test_tls_client_hello() != 1) goto err;
  284. if (test_tls_server_hello() != 1) goto err;
  285. if (test_tls_certificate() != 1) goto err;
  286. if (test_tls_server_key_exchange() != 1) goto err;
  287. if (test_tls_certificate_verify() != 1) goto err;
  288. //if (test_tls_finished() != 1) goto err; //FIXME
  289. if (test_tls_alert() != 1) goto err;
  290. if (test_tls_change_cipher_spec() != 1) goto err;
  291. if (test_tls_application_data() != 1) goto err;
  292. printf("%s all tests passed\n", __FILE__);
  293. return 0;
  294. err:
  295. error_print();
  296. return -1;
  297. }