Debian Codegraph @

src/tls.c

raw symbols SCIP
1 // nanonext - C level - Mbed TLS Functions -------------------------------------
2
3 #define NANONEXT_TLS
4 #include "nanonext.h"
5
6 // internals -------------------------------------------------------------------
7
8 static SEXP nano_hash_char(unsigned char *buf, const size_t sz) {
9
10 static const char hex[] = "0123456789abcdef";
11 SEXP out;
12 char cbuf[sz + sz + 1];
13
14 for (size_t i = 0; i < sz; i++) {
15 cbuf[2 * i] = hex[buf[i] >> 4];
16 cbuf[2 * i + 1] = hex[buf[i] & 0x0f];
17 }
18 cbuf[sz + sz] = '\0';
19
20 PROTECT(out = Rf_allocVector(STRSXP, 1));
21 SET_STRING_ELT(out, 0, Rf_mkCharLenCE(cbuf, (int) (sz + sz), CE_NATIVE));
22
23 UNPROTECT(1);
24 return out;
25
26 }
27
28 #if MBEDTLS_VERSION_MAJOR == 3 && MBEDTLS_VERSION_MINOR >= 4 || MBEDTLS_VERSION_MAJOR >= 4
29 static int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
30 const char *ibuf, size_t *len) {
31
32 unsigned long long dec;
33 unsigned int remaining_bytes = sizeof(dec);
34 unsigned char *p = obuf;
35 unsigned char val;
36 char *end_ptr = NULL;
37
38 errno = 0;
39 dec = strtoull(ibuf, &end_ptr, 10);
40
41 if ((errno != 0) || (end_ptr == ibuf))
42 return -1;
43
44 *len = 0;
45
46 while (remaining_bytes > 0) {
47 if (obufmax < (*len + 1))
48 return -1;
49
50 val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
51
52 if ((val != 0) || (*len != 0)) {
53 *p = val;
54 (*len)++;
55 p++;
56 }
57
58 remaining_bytes--;
59 }
60
61 return 0;
62
63 }
64 #endif
65
66 // Mbed TLS Random Data Generator ----------------------------------------------
67
68 SEXP rnng_random(SEXP n, SEXP convert) {
69
70 #ifdef MBEDTLS_PSA_CRYPTO_C
71 psa_crypto_init();
72 #endif
73
74 int sz, xc;
75 switch (TYPEOF(n)) {
76 case INTSXP:
77 case LGLSXP:
78 sz = NANO_INTEGER(n);
79 if (sz > 0 && sz <= 1024) break;
80 case REALSXP:
81 sz = Rf_asInteger(n);
82 if (sz > 0 && sz <= 1024) break;
83 default:
84 Rf_error("`n` must be an integer value between 1 and 1024");
85 }
86
87 SEXP out;
88 unsigned char buf[sz];
89 mbedtls_entropy_context entropy;
90 mbedtls_ctr_drbg_context ctr_drbg;
91 const char *pers = "r-nanonext-rng";
92
93 mbedtls_entropy_init(&entropy);
94 mbedtls_ctr_drbg_init(&ctr_drbg);
95
96 xc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers));
97 if (xc == 0) {
98 xc = mbedtls_ctr_drbg_random(&ctr_drbg, buf, sz);
99 }
100
101 mbedtls_ctr_drbg_free(&ctr_drbg);
102 mbedtls_entropy_free(&entropy);
103
104 if (xc)
105 Rf_error("error generating random bytes");
106
107 if (NANO_INTEGER(convert)) {
108 out = nano_hash_char(buf, sz);
109 } else {
110 out = Rf_allocVector(RAWSXP, sz);
111 if (sz)
112 memcpy(NANO_DATAPTR(out), buf, sz);
113 }
114
115 return out;
116
117 }
118
119 // nanonext - Key Generation and Certificates ----------------------------------
120
121 SEXP rnng_write_cert(SEXP cn, SEXP valid) {
122
123 #ifdef MBEDTLS_PSA_CRYPTO_C
124 psa_crypto_init();
125 #endif
126
127 const char *common = CHAR(STRING_ELT(cn, 0));
128 const char *not_after = CHAR(STRING_ELT(valid, 0)); /* validity period not after */
129 mbedtls_entropy_context entropy = {0};
130 mbedtls_ctr_drbg_context ctr_drbg = {0};
131 mbedtls_pk_context key = {0};
132 const char *pers = "r-nanonext-key";
133
134 unsigned char key_buf[16000] = {0};
135
136 mbedtls_entropy_init(&entropy);
137 mbedtls_ctr_drbg_init(&ctr_drbg);
138 mbedtls_pk_init(&key);
139
140 const char *serialvalue = "1"; /* serial number string (decimal) */
141 const char *not_before = "20010101000000"; /* validity period not before */
142 const int is_ca = 1; /* is a CA certificate */
143 const int max_pathlen = 0; /* maximum CA path length */
144 const int version = 2; /* CRT version */
145 const mbedtls_md_type_t md = MBEDTLS_MD_SHA256; /* Hash used for signing */
146
147 size_t clen = strlen(common) + 20;
148 char issuer_name[clen]; /* issuer name for certificate */
149 snprintf(issuer_name, clen, "CN=%s,O=Nanonext,C=JP", common);
150
151 int xc, error = 1;
152 mbedtls_x509_crt issuer_crt = {0};
153 mbedtls_pk_context loaded_issuer_key = {0};
154 mbedtls_pk_context *issuer_key = &loaded_issuer_key;
155 char buf[1024] = {0};
156 mbedtls_x509_csr csr; // #if defined(MBEDTLS_X509_CSR_PARSE_C)
157 mbedtls_x509write_cert crt;
158 const char *persn = "certificate";
159
160 mbedtls_x509write_crt_init(&crt);
161 mbedtls_pk_init(&loaded_issuer_key);
162 mbedtls_x509_csr_init(&csr); // #if defined(MBEDTLS_X509_CSR_PARSE_C)
163 mbedtls_x509_crt_init(&issuer_crt);
164 unsigned char output_buf[4096] = {0};
165
166 #if MBEDTLS_VERSION_MAJOR == 3 && MBEDTLS_VERSION_MINOR >= 4 || MBEDTLS_VERSION_MAJOR >= 4
167 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN] = {0};
168 size_t serial_len;
169 #else
170 mbedtls_mpi serial = {0};
171 mbedtls_mpi_init(&serial);
172 #endif
173
174 if ((xc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers))) ||
175 (xc = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type((mbedtls_pk_type_t) MBEDTLS_PK_RSA))) ||
176 (xc = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg, 4096, 65537)) ||
177 (xc = mbedtls_pk_write_key_pem(&key, key_buf, 16000)))
178 goto cleanup;
179
180 size_t klen = strlen((char *) key_buf);
181
182 if ((xc = mbedtls_ctr_drbg_reseed(&ctr_drbg, (const unsigned char *) persn, strlen(persn))) ||
183 #if MBEDTLS_VERSION_MAJOR == 3 && MBEDTLS_VERSION_MINOR >= 4 || MBEDTLS_VERSION_MAJOR >= 4
184 (xc = parse_serial_decimal_format(serial, sizeof(serial), serialvalue, &serial_len)) ||
185 #else
186 (xc = mbedtls_mpi_read_string(&serial, 10, serialvalue)) ||
187 #endif
188 #if MBEDTLS_VERSION_MAJOR >= 3
189 (xc = mbedtls_pk_parse_key(&loaded_issuer_key, key_buf, klen + 1, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg)))
190 #else
191 (xc = mbedtls_pk_parse_key(&loaded_issuer_key, key_buf, klen + 1, NULL, 0)))
192 #endif
193 goto cleanup;
194
195 mbedtls_x509write_crt_set_subject_key(&crt, issuer_key);
196 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
197
198 if ((xc = mbedtls_x509write_crt_set_subject_name(&crt, issuer_name)) ||
199 (xc = mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name)))
200 goto cleanup;
201
202 mbedtls_x509write_crt_set_version(&crt, version);
203 mbedtls_x509write_crt_set_md_alg(&crt, md);
204
205 #if MBEDTLS_VERSION_MAJOR == 3 && MBEDTLS_VERSION_MINOR >= 4 || MBEDTLS_VERSION_MAJOR >= 4
206 if ((xc = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len)) ||
207 #else
208 if ((xc = mbedtls_x509write_crt_set_serial(&crt, &serial)) ||
209 #endif
210 (xc = mbedtls_x509write_crt_set_validity(&crt, not_before, not_after)) ||
211 (xc = mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca, max_pathlen)) ||
212 (xc = mbedtls_x509write_crt_set_subject_key_identifier(&crt)) ||
213 (xc = mbedtls_x509write_crt_set_authority_key_identifier(&crt)) ||
214 (xc = mbedtls_x509write_crt_pem(&crt, output_buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg)))
215 goto cleanup;
216
217 SEXP vec, kcstr, cstr;
218 const char *names[] = {"server", "client", ""};
219 PROTECT(vec = Rf_mkNamed(VECSXP, names));
220 kcstr = Rf_allocVector(STRSXP, 2);
221 SET_VECTOR_ELT(vec, 0, kcstr);
222 SET_STRING_ELT(kcstr, 0, Rf_mkChar((char *) &output_buf));
223 SET_STRING_ELT(kcstr, 1, Rf_mkChar((char *) key_buf));
224 cstr = Rf_allocVector(STRSXP, 2);
225 SET_VECTOR_ELT(vec, 1, cstr);
226 SET_STRING_ELT(cstr, 0, Rf_mkChar((char *) &output_buf));
227 SET_STRING_ELT(cstr, 1, R_BlankString);
228
229 error = 0;
230
231 cleanup:
232
233 mbedtls_x509_csr_free(&csr); // #if defined(MBEDTLS_X509_CSR_PARSE_C)
234 mbedtls_x509_crt_free(&issuer_crt);
235 mbedtls_x509write_crt_free(&crt);
236 mbedtls_pk_free(&loaded_issuer_key);
237 #if MBEDTLS_VERSION_MAJOR == 3 && MBEDTLS_VERSION_MINOR < 4 || MBEDTLS_VERSION_MAJOR < 3
238 mbedtls_mpi_free(&serial);
239 #endif
240 mbedtls_pk_free(&key);
241 mbedtls_ctr_drbg_free(&ctr_drbg);
242 mbedtls_entropy_free(&entropy);
243
244 if (error) {
245 mbedtls_strerror(xc, buf, sizeof(buf));
246 Rf_error("%d | %s", xc, buf);
247 }
248
249 UNPROTECT(1);
250 return vec;
251
252 }
253
254 // TLS Config ------------------------------------------------------------------
255
256 SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
257
258 const nng_tls_auth_mode mod = NANO_INTEGER(auth) ? NNG_TLS_AUTH_MODE_REQUIRED : NNG_TLS_AUTH_MODE_OPTIONAL;
259 R_xlen_t usefile;
260 nng_tls_config *cfg = NULL;
261 int xc;
262 const char *crl, *file, *key, *pss;
263 SEXP xp;
264
265 if (client != R_NilValue) {
266 file = CHAR(STRING_ELT(client, 0));
267 usefile = XLENGTH(client);
268 if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)) ||
269 (xc = nng_tls_config_auth_mode(cfg, mod)))
270 goto fail;
271
272 if (usefile > 1) {
273 crl = CHAR(STRING_ELT(client, 1));
274 if ((xc = nng_tls_config_ca_chain(cfg, file, strncmp(crl, "", 1) ? crl : NULL)))
275 goto fail;
276 } else {
277 file = R_ExpandFileName(file);
278 if ((xc = nng_tls_config_ca_file(cfg, file)))
279 goto fail;
280 }
281
282 } else if (server != R_NilValue) {
283 file = CHAR(STRING_ELT(server, 0));
284 usefile = XLENGTH(server);
285 pss = pass != R_NilValue ? CHAR(STRING_ELT(pass, 0)) : NULL;
286 if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_SERVER)) ||
287 (xc = nng_tls_config_auth_mode(cfg, mod)))
288 goto fail;
289
290 if (usefile > 1) {
291 key = CHAR(STRING_ELT(server, 1));
292 if ((xc = nng_tls_config_own_cert(cfg, file, key, pss)))
293 goto fail;
294 } else {
295 file = R_ExpandFileName(file);
296 if ((xc = nng_tls_config_cert_key_file(cfg, file, pss)))
297 goto fail;
298 }
299
300 } else {
301 if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)) ||
302 (xc = nng_tls_config_auth_mode(cfg, NNG_TLS_AUTH_MODE_NONE)))
303 goto fail;
304 }
305
306 PROTECT(xp = R_MakeExternalPtr(cfg, nano_TlsSymbol, R_NilValue));
307 R_RegisterCFinalizerEx(xp, tls_finalizer, TRUE);
308 Rf_classgets(xp, Rf_mkString("tlsConfig"));
309 if (client != R_NilValue) {
310 Rf_setAttrib(xp, R_SpecSymbol, Rf_mkString("client"));
311 Rf_setAttrib(xp, R_ModeSymbol, Rf_mkString(mod == NNG_TLS_AUTH_MODE_REQUIRED ? "required" : "optional"));
312 } else if (server != R_NilValue) {
313 Rf_setAttrib(xp, R_SpecSymbol, Rf_mkString("server"));
314 Rf_setAttrib(xp, R_ModeSymbol, Rf_mkString(mod == NNG_TLS_AUTH_MODE_REQUIRED ? "required" : "optional"));
315 } else {
316 Rf_setAttrib(xp, R_SpecSymbol, Rf_mkString("client"));
317 Rf_setAttrib(xp, R_ModeSymbol, Rf_mkString("none"));
318 }
319
320 UNPROTECT(1);
321 return xp;
322
323 fail:
324 if (cfg != NULL)
325 nng_tls_config_free(cfg);
326 ERROR_OUT(xc);
327
328 }
329
330 // utils -----------------------------------------------------------------------
331
332 SEXP rnng_version(void) {
333
334 char mbed_version_string[18];
335 mbedtls_version_get_string_full(mbed_version_string);
336 SEXP version;
337 PROTECT(version = Rf_allocVector(STRSXP, 2));
338 SET_STRING_ELT(version, 0, Rf_mkChar(nng_version()));
339 SET_STRING_ELT(version, 1, Rf_mkChar(mbed_version_string));
340 UNPROTECT(1);
341
342 return version;
343
344 }