| 1 |
# nanonext - TLS Configuration ------------------------------------------------- |
| 2 |
|
| 3 |
#' Create TLS Configuration |
| 4 |
#' |
| 5 |
#' Create a TLS configuration object to be used for secure connections. Specify |
| 6 |
#' `client` to create a client configuration or `server` to create a server |
| 7 |
#' configuration. |
| 8 |
#' |
| 9 |
#' Specify one of `client` or `server` only, or neither (in which case an empty |
| 10 |
#' client configuration is created), as a configuration can only be of one type. |
| 11 |
#' |
| 12 |
#' @section Public Internet HTTPS: |
| 13 |
#' |
| 14 |
#' When making HTTPS requests over the public internet, you should supply a TLS |
| 15 |
#' configuration to validate server certificates. |
| 16 |
#' |
| 17 |
#' Root CA certificates in PEM format may be found at: |
| 18 |
#' |
| 19 |
#' - Linux: \file{/etc/ssl/certs/ca-certificates.crt} or |
| 20 |
#' \file{/etc/pki/tls/certs/ca-bundle.crt} |
| 21 |
#' - macOS: \file{/etc/ssl/cert.pem} |
| 22 |
#' - Windows: download from the Common CA Database site run by Mozilla: |
| 23 |
#' <https://www.ccadb.org/resources> (select the Server Authentication SSL/TLS |
| 24 |
#' certificates text file). *This link is not endorsed; use at your own risk.* |
| 25 |
#' |
| 26 |
#' @param client **either** the character path to a file containing X.509 |
| 27 |
#' certificate(s) in PEM format, comprising the certificate authority |
| 28 |
#' certificate chain (and revocation list if present), used to validate |
| 29 |
#' certificates presented by peers,\cr |
| 30 |
#' **or** a length 2 character vector comprising \[i\] the certificate |
| 31 |
#' authority certificate chain and \[ii\] the certificate revocation list, or |
| 32 |
#' empty string `""` if not applicable. |
| 33 |
#' @param server **either** the character path to a file containing the |
| 34 |
#' PEM-encoded TLS certificate and associated private key (may contain |
| 35 |
#' additional certificates leading to a validation chain, with the leaf |
| 36 |
#' certificate first),\cr |
| 37 |
#' **or** a length 2 character vector comprising \[i\] the TLS certificate |
| 38 |
#' (optionally certificate chain) and \[ii\] the associated private key. |
| 39 |
#' @param pass (optional) required only if the secret key supplied to `server` |
| 40 |
#' is encrypted with a password. For security, consider providing through a |
| 41 |
#' function that returns this value, rather than directly. |
| 42 |
#' @param auth logical value whether to require authentication - by default TRUE |
| 43 |
#' for client and FALSE for server configurations. If TRUE, the session is |
| 44 |
#' only allowed to proceed if the peer has presented a certificate and it has |
| 45 |
#' been validated. If FALSE, authentication is optional, whereby a certificate |
| 46 |
#' is validated if presented by the peer, but the session allowed to proceed |
| 47 |
#' otherwise. If neither `client` nor `server` are supplied, then no |
| 48 |
#' authentication is performed and this argument has no effect. |
| 49 |
#' |
| 50 |
#' @return A 'tlsConfig' object. |
| 51 |
#' |
| 52 |
#' @examples |
| 53 |
#' tls <- tls_config() |
| 54 |
#' tls |
| 55 |
#' ncurl("https://postman-echo.com/get", timeout = 1000L, tls = tls) |
| 56 |
#' |
| 57 |
#' # client TLS configuration for public internet HTTPS on Linux |
| 58 |
#' # tls <- tls_config(client = "/etc/ssl/certs/ca-certificates.crt") |
| 59 |
#' |
| 60 |
#' @export |
| 61 |
#' |
| 62 |
tls_config <- function(client = NULL, server = NULL, pass = NULL, auth = is.null(server)) |
| 63 |
.Call(rnng_tls_config, client, server, pass, auth) |
| 64 |
|
| 65 |
# nanonext - Key Gen and Certificates ------------------------------------------ |
| 66 |
|
| 67 |
#' Generate Self-Signed Certificate and Key |
| 68 |
#' |
| 69 |
#' Generate self-signed x509 certificate and 4096 bit RSA private/public key |
| 70 |
#' pair for use with authenticated, encrypted TLS communications. |
| 71 |
#' |
| 72 |
#' Note that it can take a second or two for the key and certificate to be |
| 73 |
#' generated. |
| 74 |
#' |
| 75 |
#' @param cn \[default '127.0.0.1'\] character issuer common name (CN) for the |
| 76 |
#' certificate. This can be either a hostname or an IP address, but must match |
| 77 |
#' the actual server URL as client authentication will depend on it. |
| 78 |
#' @param valid \[default '20301231235959'\] character 'not after' date-time in |
| 79 |
#' 'yyyymmddhhmmss' format. The certificate is not valid after this time. |
| 80 |
#' |
| 81 |
#' @return A list of length 2, comprising `$server` and `$client`. These may be |
| 82 |
#' passed directly to the relevant argument of [tls_config()]. |
| 83 |
#' |
| 84 |
#' @examplesIf interactive() |
| 85 |
#' cert <- write_cert(cn = "127.0.0.1") |
| 86 |
#' ser <- tls_config(server = cert$server) |
| 87 |
#' cli <- tls_config(client = cert$client) |
| 88 |
#' |
| 89 |
#' s <- socket(listen = "tls+tcp://127.0.0.1:5555", tls = ser) |
| 90 |
#' s1 <- socket(dial = "tls+tcp://127.0.0.1:5555", tls = cli) |
| 91 |
#' |
| 92 |
#' # secure TLS connection established |
| 93 |
#' |
| 94 |
#' close(s1) |
| 95 |
#' close(s) |
| 96 |
#' |
| 97 |
#' cert |
| 98 |
#' |
| 99 |
#' @export |
| 100 |
#' |
| 101 |
write_cert <- function(cn = "127.0.0.1", valid = "20301231235959") |
| 102 |
.Call(rnng_write_cert, cn, valid) |