-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mojo
152 lines (133 loc) · 5.69 KB
/
main.mojo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from src.rustls import *
from src.libc import *
fn main():
var cert_path = to_char_ptr("/etc/ssl/cert.pem")
var config = RustlsClientConfig()
var root_cert_store_builder = new_root_cert_store_builder()
var result = load_roots_from_file(root_cert_store_builder, cert_path, False)
print("Result: ", result)
# const char *hostname = argv[1];
# const char *port = argv[2];
# const char *path = argv[3];
# /* Set this global variable for logging purposes. */
# programname = "client";
# const struct rustls_crypto_provider *custom_provider = NULL;
# struct rustls_client_config_builder *config_builder = NULL;
# struct rustls_root_cert_store_builder *server_cert_root_store_builder = NULL;
# const struct rustls_root_cert_store *server_cert_root_store = NULL;
# const struct rustls_client_config *client_config = NULL;
# struct rustls_web_pki_server_cert_verifier_builder
# *server_cert_verifier_builder = NULL;
# struct rustls_server_cert_verifier *server_cert_verifier = NULL;
# struct rustls_slice_bytes alpn_http11;
# const struct rustls_certified_key *certified_key = NULL;
# alpn_http11.data = (unsigned char *)"http/1.1";
# alpn_http11.len = 8;
# #ifdef _WIN32
# WSADATA wsa;
# WSAStartup(MAKEWORD(1, 1), &wsa);
# setmode(STDOUT_FILENO, O_BINARY);
# #endif
# const char *custom_ciphersuite_name = getenv("RUSTLS_CIPHERSUITE");
# if(custom_ciphersuite_name != NULL) {
# custom_provider =
# default_provider_with_custom_ciphersuite(custom_ciphersuite_name);
# if(custom_provider == NULL) {
# goto cleanup;
# }
# printf("customized to use ciphersuite: %s\n", custom_ciphersuite_name);
# result = rustls_client_config_builder_new_custom(custom_provider,
# default_tls_versions,
# default_tls_versions_len,
# &config_builder);
# if(result != RUSTLS_RESULT_OK) {
# print_error("creating client config builder", result);
# goto cleanup;
# }
# }
# else {
# config_builder = rustls_client_config_builder_new();
# }
# if(getenv("RUSTLS_PLATFORM_VERIFIER")) {
# result = rustls_platform_server_cert_verifier(&server_cert_verifier);
# if(result != RUSTLS_RESULT_OK) {
# fprintf(stderr, "client: failed to construct platform verifier\n");
# goto cleanup;
# }
# rustls_client_config_builder_set_server_verifier(config_builder,
# server_cert_verifier);
# }
# else if(getenv("CA_FILE")) {
# server_cert_root_store_builder = rustls_root_cert_store_builder_new();
# result = rustls_root_cert_store_builder_load_roots_from_file(
# server_cert_root_store_builder, getenv("CA_FILE"), true);
# if(result != RUSTLS_RESULT_OK) {
# print_error("loading trusted certificates", result);
# goto cleanup;
# }
# result = rustls_root_cert_store_builder_build(
# server_cert_root_store_builder, &server_cert_root_store);
# if(result != RUSTLS_RESULT_OK) {
# goto cleanup;
# }
# server_cert_verifier_builder =
# rustls_web_pki_server_cert_verifier_builder_new(server_cert_root_store);
# result = rustls_web_pki_server_cert_verifier_builder_build(
# server_cert_verifier_builder, &server_cert_verifier);
# if(result != RUSTLS_RESULT_OK) {
# goto cleanup;
# }
# rustls_client_config_builder_set_server_verifier(config_builder,
# server_cert_verifier);
# }
# else if(getenv("NO_CHECK_CERTIFICATE")) {
# rustls_client_config_builder_dangerous_set_certificate_verifier(
# config_builder, verify);
# }
# else {
# fprintf(stderr,
# "client: must set either RUSTLS_PLATFORM_VERIFIER or CA_FILE or "
# "NO_CHECK_CERTIFICATE env var\n");
# goto cleanup;
# }
# char *auth_cert = getenv("AUTH_CERT");
# char *auth_key = getenv("AUTH_KEY");
# if((auth_cert && !auth_key) || (!auth_cert && auth_key)) {
# fprintf(
# stderr,
# "client: must set both AUTH_CERT and AUTH_KEY env vars, or neither\n");
# goto cleanup;
# }
# else if(auth_cert && auth_key) {
# certified_key = load_cert_and_key(auth_cert, auth_key);
# if(certified_key == NULL) {
# goto cleanup;
# }
# rustls_client_config_builder_set_certified_key(
# config_builder, &certified_key, 1);
# }
# rustls_client_config_builder_set_alpn_protocols(
# config_builder, &alpn_http11, 1);
# result = rustls_client_config_builder_build(config_builder, &client_config);
# if(result != RUSTLS_RESULT_OK) {
# print_error("building client config", result);
# goto cleanup;
# }
# int i;
# for(i = 0; i < 3; i++) {
# result = do_request(client_config, hostname, port, path);
# if(result != 0) {
# goto cleanup;
# }
# }
# // Success!
# ret = 0;
# cleanup:
# rustls_root_cert_store_builder_free(server_cert_root_store_builder);
# rustls_root_cert_store_free(server_cert_root_store);
# rustls_web_pki_server_cert_verifier_builder_free(
# server_cert_verifier_builder);
# rustls_server_cert_verifier_free(server_cert_verifier);
# rustls_certified_key_free(certified_key);
# rustls_client_config_free(client_config);
# rustls_crypto_provider_free(custom_provider);