Skip to content

Commit

Permalink
Remove interactive status updates from write_cert() (#75)
Browse files Browse the repository at this point in the history
* remove status update from write_cert() when interactive

* document that key and cert may take time to generate
  • Loading branch information
shikokuchuo authored Jan 27, 2025
1 parent 63792bd commit 0f38f4d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: nanonext
Type: Package
Title: NNG (Nanomsg Next Gen) Lightweight Messaging Library
Version: 1.4.0.9002
Version: 1.4.0.9003
Description: R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. NNG is
a socket library implementing 'Scalability Protocols', a reliable,
high-performance standard for common communications patterns including
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# nanonext 1.4.0.9002 (development)
# nanonext 1.4.0.9003 (development)

#### Updates

* `write_cert()` no longer displays a status message when interactive (thanks @wlandau, #74).
* Removes partial matching when using `$`, `[[` or `[` on an object inheriting from class 'nano'.

# nanonext 1.4.0
Expand Down
6 changes: 3 additions & 3 deletions R/tls.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ tls_config <- function(client = NULL, server = NULL, pass = NULL, auth = is.null
#' Generate self-signed x509 certificate and 4096 bit RSA private/public key
#' pair for use with authenticated, encrypted TLS communications.
#'
#' For interactive sessions only, a status message is printed at the start of
#' key / certificate generation and also when complete.
#' Note that it can take a second or two for the key and certificate to be
#' generated.
#'
#' @param cn [default 'localhost'] character issuer common name (CN) for the
#' certificate. This can be either a hostname or an IP address, but must match
Expand Down Expand Up @@ -115,4 +115,4 @@ tls_config <- function(client = NULL, server = NULL, pass = NULL, auth = is.null
#' @export
#'
write_cert <- function(cn = "localhost", valid = "20301231235959")
.Call(rnng_write_cert, cn, valid, interactive())
.Call(rnng_write_cert, cn, valid)
4 changes: 2 additions & 2 deletions man/write_cert.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static const R_CallMethodDef callMethods[] = {
{"rnng_url_parse", (DL_FUNC) &rnng_url_parse, 1},
{"rnng_version", (DL_FUNC) &rnng_version, 0},
{"rnng_wait_thread_create", (DL_FUNC) &rnng_wait_thread_create, 1},
{"rnng_write_cert", (DL_FUNC) &rnng_write_cert, 3},
{"rnng_write_cert", (DL_FUNC) &rnng_write_cert, 2},
{NULL, NULL, 0}
};

Expand Down
2 changes: 1 addition & 1 deletion src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,6 @@ SEXP rnng_unresolved2(SEXP);
SEXP rnng_url_parse(SEXP);
SEXP rnng_version(void);
SEXP rnng_wait_thread_create(SEXP);
SEXP rnng_write_cert(SEXP, SEXP, SEXP);
SEXP rnng_write_cert(SEXP, SEXP);

#endif
21 changes: 4 additions & 17 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,10 @@ SEXP rnng_random(SEXP n, SEXP convert) {

// nanonext - Key Generation and Certificates ----------------------------------

SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
SEXP rnng_write_cert(SEXP cn, SEXP valid) {

const char *common = CHAR(STRING_ELT(cn, 0));
const char *not_after = CHAR(STRING_ELT(valid, 0)); /* validity period not after */
const int interactive = NANO_INTEGER(inter);
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_pk_context key;
Expand All @@ -155,7 +154,6 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
snprintf(issuer_name, clen, "CN=%s,O=Nanonext,C=JP", common);

int xc, exit = 1;
if (interactive) REprintf("Generating key + certificate [ ]");
mbedtls_x509_crt issuer_crt;
mbedtls_pk_context loaded_issuer_key;
mbedtls_pk_context *issuer_key = &loaded_issuer_key;
Expand All @@ -181,20 +179,10 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
mbedtls_mpi_init(&serial);
#endif

if (interactive) REprintf("\rGenerating key + certificate [. ]");

if ((xc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers))) ||
(xc = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type((mbedtls_pk_type_t) MBEDTLS_PK_RSA))))
goto exitlevel1;

if (interactive) REprintf("\rGenerating key + certificate [.. ]");

if ((xc = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg, 4096, 65537)))
goto exitlevel1;

if (interactive) REprintf("\rGenerating key + certificate [... ]");

if ((xc = mbedtls_pk_write_key_pem(&key, key_buf, 16000)))
(xc = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type((mbedtls_pk_type_t) MBEDTLS_PK_RSA))) ||
(xc = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg, 4096, 65537)) ||
(xc = mbedtls_pk_write_key_pem(&key, key_buf, 16000)))
goto exitlevel1;

size_t klen = strlen((char *) key_buf);
Expand Down Expand Up @@ -246,7 +234,6 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
SET_STRING_ELT(cstr, 0, Rf_mkChar((char *) &output_buf));
SET_STRING_ELT(cstr, 1, R_BlankString);

if (interactive) REprintf("\rGenerating key + certificate [done]\n");
exit = 0;

exitlevel1:
Expand Down

0 comments on commit 0f38f4d

Please sign in to comment.