louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#91 · main · 15 min 44 s
0Étoiles
0Forks
40Issues
| 1 | /// TLS clients and servers (ADR 0038; native programs only): HTTPS, |
| 2 | /// SMTPS, PostgreSQL and anything else over TLS 1.2 or 1.3. |
| 3 | /// |
| 4 | /// use config <- result.try(tls.config(tls.defaults())) // once, and share it |
| 5 | /// use conn <- result.try(tls.dial("example.com", 443, config)) |
| 6 | /// use _ <- result.try(tls.write_string(conn, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")) |
| 7 | /// tls.read(conn, 4096) |
| 8 | /// |
| 9 | /// The server's certificate must be valid for the host name given and come |
| 10 | /// from a trusted authority: Mozilla's list (built in), or the ones you |
| 11 | /// add (`with_ca_file`, `with_ca_pem`). Nothing turns this off by accident: |
| 12 | /// only `insecure_skip_verification` does, with a warning. |
| 13 | /// |
| 14 | /// A server makes a `server_config` from its certificate and key, and |
| 15 | /// `accept`s TLS over each connection `net.accept` gives, in that |
| 16 | /// connection's own actor (a client that never finishes its handshake |
| 17 | /// then holds up only its actor). With `with_client_ca_file` or |
| 18 | /// `with_client_ca_pem`, every client must present a certificate from |
| 19 | /// those authorities (mutual TLS); `peer_names` says whom it was issued to. |
| 20 | /// |
| 21 | /// A connection that ends without TLS's `close_notify` is `Truncated`, not |
| 22 | /// an empty read: what was read may not be all that was sent. |
| 23 | /// |
| 24 | /// There are no timeouts here: an actor that must not wait forever closes |
| 25 | /// the TCP connection from another actor (`net.close`), or is killed. A |
| 26 | /// stream is meant to be used by one actor at a time. |
| 27 | /// |
| 28 | /// `Stream` (std/io) is implemented for TLS streams and TCP connections, so |
| 29 | /// code can take either. |
| 30 | /// |
| 31 | /// **Security review needed before production use** (ADR 0038). |
| 32 | |
| 33 | import std/ffi.{type Resource} |
| 34 | import std/io.{type Stream} |
| 35 | import std/list |
| 36 | import std/net.{type TcpStream} |
| 37 | import std/result |
| 38 | |
| 39 | /// Marks TLS streams (`Resource<TlsStream>`). |
| 40 | @thread_safe |
| 41 | pub type TlsStream { TlsStream } |
| 42 | |
| 43 | /// Marks TLS configurations (`Resource<TlsConfig>`): make one, and use it |
| 44 | /// for every connection (it remembers sessions, to resume them). |
| 45 | @thread_safe |
| 46 | pub type TlsConfig { TlsConfig } |
| 47 | |
| 48 | /// Marks TLS server configurations (`Resource<TlsServerConfig>`): make |
| 49 | /// one, and use it for every connection. |
| 50 | @thread_safe |
| 51 | pub type TlsServerConfig { TlsServerConfig } |
| 52 | |
| 53 | /// Why TLS failed. |
| 54 | @derive Eq Show Debug |
| 55 | pub type TlsError |
| 56 | { /// The certificate isn't from a trusted authority. |
| 57 | UntrustedIssuer(String) |
| 58 | /// The certificate has expired. |
| 59 | , Expired |
| 60 | /// The certificate isn't valid yet. |
| 61 | , NotYetValid |
| 62 | /// The certificate is for another name than the one connected to. |
| 63 | , WrongHostName(String) |
| 64 | /// Something else is wrong with the certificate. |
| 65 | , BadCertificate(String) |
| 66 | /// No TLS version both sides accept (TLS 1.2 is the oldest here). |
| 67 | , ProtocolVersion |
| 68 | /// The handshake failed otherwise. |
| 69 | , HandshakeFailure(String) |
| 70 | /// The connection ended without TLS's `close_notify`. |
| 71 | , Truncated |
| 72 | /// The configuration can't work: a certificate, CA or key file missing |
| 73 | /// or invalid, a key that isn't the certificate's, no roots, a bad host |
| 74 | /// name. |
| 75 | , InvalidConfig(String) |
| 76 | /// The socket failed. |
| 77 | , Io(String) |
| 78 | } |
| 79 | |
| 80 | /// What a configuration is made of: start from `defaults()`. |
| 81 | pub opaque type Options |
| 82 | { Options( |
| 83 | default_roots: Bool |
| 84 | , ca_pems: List<String> |
| 85 | , ca_files: List<String> |
| 86 | , alpn: List<String> |
| 87 | , tls13_only: Bool |
| 88 | , insecure: Bool |
| 89 | , cert_file: String |
| 90 | , key_file: String |
| 91 | ) |
| 92 | } |
| 93 | |
| 94 | /// Mozilla's trusted authorities, TLS 1.2 or 1.3, no client certificate. |
| 95 | pub fn defaults :: -> Options |
| 96 | { |
| 97 | Options( default_roots: True |
| 98 | , ca_pems: [] |
| 99 | , ca_files: [] |
| 100 | , alpn: [] |
| 101 | , tls13_only: False |
| 102 | , insecure: False |
| 103 | , cert_file: "" |
| 104 | , key_file: "" |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | /// Also trust the authorities of a PEM file. |
| 109 | pub fn with_ca_file :: options: Options path: String -> Options |
| 110 | { |
| 111 | Options(..options, ca_files: [path, ..options.ca_files]) |
| 112 | } |
| 113 | |
| 114 | /// Also trust the authorities of PEM text. |
| 115 | pub fn with_ca_pem :: options: Options pem: String -> Options |
| 116 | { |
| 117 | Options(..options, ca_pems: [pem, ..options.ca_pems]) |
| 118 | } |
| 119 | |
| 120 | /// Trust only the authorities added (a private PKI). |
| 121 | pub fn without_default_roots :: options: Options -> Options |
| 122 | { |
| 123 | Options(..options, default_roots: False) |
| 124 | } |
| 125 | |
| 126 | /// Present a client certificate (mutual TLS): PEM files, read when the |
| 127 | /// configuration is made. The key is read by path only. |
| 128 | pub fn with_client_cert :: |
| 129 | options: Options |
| 130 | cert_path: String |
| 131 | key_path: String |
| 132 | -> Options |
| 133 | { |
| 134 | Options(..options, cert_file: cert_path, key_file: key_path) |
| 135 | } |
| 136 | |
| 137 | /// Offer these application protocols (ALPN), preferred first: `["h2", |
| 138 | /// "http/1.1"]`. |
| 139 | pub fn with_alpn :: options: Options protocols: List<String> -> Options |
| 140 | { |
| 141 | Options(..options, alpn: protocols) |
| 142 | } |
| 143 | |
| 144 | /// Refuse TLS 1.2. |
| 145 | pub fn tls13_only :: options: Options -> Options |
| 146 | { |
| 147 | Options(..options, tls13_only: True) |
| 148 | } |
| 149 | |
| 150 | /// Accept any certificate: the connection is encrypted but not |
| 151 | /// authenticated, so anyone in between can read it. For tests against a |
| 152 | /// server you can't give a certificate. Warns on stderr. |
| 153 | pub fn insecure_skip_verification :: options: Options -> Options |
| 154 | { |
| 155 | Options(..options, insecure: True) |
| 156 | } |
| 157 | |
| 158 | /// A configuration (reads the files on the `io` blocking pool). |
| 159 | pub fn config :: |
| 160 | options: Options |
| 161 | -> Result<Resource<TlsConfig>, TlsError> / <io> |
| 162 | { |
| 163 | match options |
| 164 | { Options(d, pems, files, alpn, only13, insecure, cert, key) -> |
| 165 | // Added files and texts in the order given. |
| 166 | make_config |
| 167 | d |
| 168 | (list.reverse pems) |
| 169 | (list.reverse files) |
| 170 | alpn |
| 171 | only13 |
| 172 | insecure |
| 173 | cert |
| 174 | key |
| 175 | |> result.map_error error_of |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | @extern c "tl_tls_config" (blocking io) |
| 180 | fn make_config :: |
| 181 | default_roots: Bool |
| 182 | ca_pems: List<String> |
| 183 | ca_files: List<String> |
| 184 | alpn: List<String> |
| 185 | tls13_only: Bool |
| 186 | insecure: Bool |
| 187 | cert_file: String |
| 188 | key_file: String |
| 189 | -> Result<Resource<TlsConfig>, (Int, String)> / <io> |
| 190 | |
| 191 | /// What a server configuration is made of: start from `server_options`. |
| 192 | pub opaque type ServerOptions |
| 193 | { ServerOptions( |
| 194 | cert_file: String |
| 195 | , key_file: String |
| 196 | , client_ca_pems: List<String> |
| 197 | , client_ca_files: List<String> |
| 198 | , alpn: List<String> |
| 199 | , tls13_only: Bool |
| 200 | ) |
| 201 | } |
| 202 | |
| 203 | /// A server's certificate chain (the server's certificate first) and its |
| 204 | /// private key: PEM files, read when the configuration is made, the key by |
| 205 | /// path only. TLS 1.2 or 1.3, no client certificates asked for. |
| 206 | pub fn server_options :: cert_path: String key_path: String -> ServerOptions |
| 207 | { |
| 208 | ServerOptions( cert_file: cert_path |
| 209 | , key_file: key_path |
| 210 | , client_ca_pems: [] |
| 211 | , client_ca_files: [] |
| 212 | , alpn: [] |
| 213 | , tls13_only: False |
| 214 | ) |
| 215 | } |
| 216 | |
| 217 | /// Mutual TLS: refuse clients without a certificate from the authorities |
| 218 | /// of a PEM file (or of the others added). |
| 219 | pub fn with_client_ca_file :: |
| 220 | options: ServerOptions |
| 221 | path: String |
| 222 | -> ServerOptions |
| 223 | { |
| 224 | ServerOptions(..options, client_ca_files: [path, ..options.client_ca_files]) |
| 225 | } |
| 226 | |
| 227 | /// Mutual TLS: refuse clients without a certificate from the authorities |
| 228 | /// of PEM text (or of the others added). |
| 229 | pub fn with_client_ca_pem :: options: ServerOptions pem: String -> ServerOptions |
| 230 | { |
| 231 | ServerOptions(..options, client_ca_pems: [pem, ..options.client_ca_pems]) |
| 232 | } |
| 233 | |
| 234 | /// Accept these application protocols (ALPN), preferred first: the first |
| 235 | /// of them the client offers is chosen. |
| 236 | pub fn server_alpn :: |
| 237 | options: ServerOptions |
| 238 | protocols: List<String> |
| 239 | -> ServerOptions |
| 240 | { |
| 241 | ServerOptions(..options, alpn: protocols) |
| 242 | } |
| 243 | |
| 244 | /// Refuse TLS 1.2. |
| 245 | pub fn server_tls13_only :: options: ServerOptions -> ServerOptions |
| 246 | { |
| 247 | ServerOptions(..options, tls13_only: True) |
| 248 | } |
| 249 | |
| 250 | /// A server configuration (reads the files on the `io` blocking pool). |
| 251 | pub fn server_config :: |
| 252 | options: ServerOptions |
| 253 | -> Result<Resource<TlsServerConfig>, TlsError> / <io> |
| 254 | { |
| 255 | match options |
| 256 | { ServerOptions(cert, key, pems, files, alpn, only13) -> |
| 257 | // Added files and texts in the order given. |
| 258 | make_server_config |
| 259 | cert |
| 260 | key |
| 261 | (list.reverse pems) |
| 262 | (list.reverse files) |
| 263 | alpn |
| 264 | only13 |
| 265 | |> result.map_error error_of |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @extern c "tl_tls_server_config" (blocking io) |
| 270 | fn make_server_config :: |
| 271 | cert_file: String |
| 272 | key_file: String |
| 273 | client_ca_pems: List<String> |
| 274 | client_ca_files: List<String> |
| 275 | alpn: List<String> |
| 276 | tls13_only: Bool |
| 277 | -> Result<Resource<TlsServerConfig>, (Int, String)> / <io> |
| 278 | |
| 279 | /// TLS over `conn` to `host` (the name the certificate must have), after |
| 280 | /// the handshake. `conn` is the TLS stream's from then on: reading or |
| 281 | /// writing it is an error, but `net.close(conn)` still closes it (from |
| 282 | /// another actor too, to stop a handshake that hangs). Nothing read from |
| 283 | /// `conn` before is lost or replayed: use this after a protocol's own |
| 284 | /// "start TLS" exchange (PostgreSQL's `SSLRequest`, SMTP's `STARTTLS`). |
| 285 | pub fn connect :: |
| 286 | conn: Resource<TcpStream> |
| 287 | host: String |
| 288 | config: Resource<TlsConfig> |
| 289 | -> Result<Resource<TlsStream>, TlsError> / <io> |
| 290 | { |
| 291 | connect_raw conn host config |> result.map_error error_of |
| 292 | } |
| 293 | |
| 294 | @extern c "tl_tls_connect" |
| 295 | fn connect_raw :: |
| 296 | conn: Resource<TcpStream> |
| 297 | host: String |
| 298 | config: Resource<TlsConfig> |
| 299 | -> Result<Resource<TlsStream>, (Int, String)> / <io> |
| 300 | |
| 301 | /// Connect to `host:port`, then TLS to `host`. |
| 302 | pub fn dial :: |
| 303 | host: String |
| 304 | port: Int |
| 305 | config: Resource<TlsConfig> |
| 306 | -> Result<Resource<TlsStream>, TlsError> / <io> |
| 307 | { |
| 308 | match net.connect host port |
| 309 | { Ok(conn) -> connect conn host config, Error(e) -> Error(Io(e)) } |
| 310 | } |
| 311 | |
| 312 | /// The server side of TLS over `conn` (from `net.accept`), after the |
| 313 | /// handshake. As with `connect`, `conn` is the TLS stream's from then on, |
| 314 | /// and `net.close(conn)` from another actor stops a handshake that hangs. |
| 315 | /// Under mutual TLS, a client without a certificate from the authorities |
| 316 | /// added is refused here (with TLS 1.3, it learns so on its first read). |
| 317 | pub fn accept :: |
| 318 | conn: Resource<TcpStream> |
| 319 | config: Resource<TlsServerConfig> |
| 320 | -> Result<Resource<TlsStream>, TlsError> / <io> |
| 321 | { |
| 322 | accept_raw conn config |> result.map_error error_of |
| 323 | } |
| 324 | |
| 325 | @extern c "tl_tls_accept" |
| 326 | fn accept_raw :: |
| 327 | conn: Resource<TcpStream> |
| 328 | config: Resource<TlsServerConfig> |
| 329 | -> Result<Resource<TlsStream>, (Int, String)> / <io> |
| 330 | |
| 331 | /// Up to `max` bytes; empty once the other end ended the stream properly. |
| 332 | pub fn read :: |
| 333 | conn: Resource<TlsStream> |
| 334 | max: Int |
| 335 | -> Result<Bytes, TlsError> / <io> |
| 336 | { |
| 337 | read_raw conn max |> result.map_error error_of |
| 338 | } |
| 339 | |
| 340 | @extern c "tl_tls_read" |
| 341 | fn read_raw :: |
| 342 | conn: Resource<TlsStream> |
| 343 | max: Int |
| 344 | -> Result<Bytes, (Int, String)> / <io> |
| 345 | |
| 346 | /// Send all of `data`. |
| 347 | pub fn write :: |
| 348 | conn: Resource<TlsStream> |
| 349 | data: Bytes |
| 350 | -> Result<(), TlsError> / <io> |
| 351 | { |
| 352 | write_raw conn data |> result.map_error error_of |
| 353 | } |
| 354 | |
| 355 | @extern c "tl_tls_write" |
| 356 | fn write_raw :: |
| 357 | conn: Resource<TlsStream> |
| 358 | data: Bytes |
| 359 | -> Result<(), (Int, String)> / <io> |
| 360 | |
| 361 | /// Send all of `text`, encoded as UTF-8. |
| 362 | pub fn write_string :: |
| 363 | conn: Resource<TlsStream> |
| 364 | text: String |
| 365 | -> Result<(), TlsError> / <io> |
| 366 | { |
| 367 | write_string_raw conn text |> result.map_error error_of |
| 368 | } |
| 369 | |
| 370 | @extern c "tl_tls_write_string" |
| 371 | fn write_string_raw :: |
| 372 | conn: Resource<TlsStream> |
| 373 | text: String |
| 374 | -> Result<(), (Int, String)> / <io> |
| 375 | |
| 376 | /// End the stream properly (`close_notify`) and close the socket. |
| 377 | @extern c "tl_tls_close" |
| 378 | pub fn close :: conn: Resource<TlsStream> -> () / <io> |
| 379 | |
| 380 | /// The application protocol the server chose (ALPN), if any. |
| 381 | pub fn alpn_protocol :: conn: Resource<TlsStream> -> Option<String> / <io> |
| 382 | { |
| 383 | match alpn_raw conn { (True, p) -> Some(p), _ -> None } |
| 384 | } |
| 385 | |
| 386 | @extern c "tl_tls_alpn" |
| 387 | fn alpn_raw :: conn: Resource<TlsStream> -> (Bool, String) / <io> |
| 388 | |
| 389 | /// The protocol version: `"TLS 1.3"` or `"TLS 1.2"`. |
| 390 | @extern c "tl_tls_version" |
| 391 | pub fn version :: conn: Resource<TlsStream> -> String / <io> |
| 392 | |
| 393 | /// The DNS names of the other end's certificate (its subject alternative |
| 394 | /// names), once verified: the server's for a client; the client's for a |
| 395 | /// server under mutual TLS. Empty when there is no verified certificate: |
| 396 | /// a server that asks for none, or `insecure_skip_verification`. Compare |
| 397 | /// names exactly: a wildcard (`*.example.com`) is listed as written. |
| 398 | @extern c "tl_tls_peer_names" |
| 399 | pub fn peer_names :: conn: Resource<TlsStream> -> List<String> / <io> |
| 400 | |
| 401 | /// The runtime's error codes (`louise_rt/src/tls.rs`). |
| 402 | fn error_of :: e: (Int, String) -> TlsError |
| 403 | { |
| 404 | match e |
| 405 | { (1, m) -> UntrustedIssuer(m) |
| 406 | , (2, _) -> Expired |
| 407 | , (3, _) -> NotYetValid |
| 408 | , (4, m) -> WrongHostName(m) |
| 409 | , (5, m) -> BadCertificate(m) |
| 410 | , (6, _) -> ProtocolVersion |
| 411 | , (7, m) -> HandshakeFailure(m) |
| 412 | , (8, _) -> Truncated |
| 413 | , (9, m) -> InvalidConfig(m) |
| 414 | , (_, m) -> Io(m) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | impl Stream<TlsStream> |
| 419 | { |
| 420 | fn read :: conn max |
| 421 | { |
| 422 | read conn max |> result.map_error show |
| 423 | } |
| 424 | fn write :: conn data |
| 425 | { |
| 426 | write conn data |> result.map_error show |
| 427 | } |
| 428 | fn close :: conn |
| 429 | { |
| 430 | close conn |
| 431 | } |
| 432 | } |