packages feed

streamly-network-tls-0.1.0.0: README.md

# streamly-network-tls

TLS for [Streamly](https://streamly.composewell.com/), for clients and for
servers, over the
[tls](https://hackage.haskell.org/package/tls) package. A connection gives
the bytes received as a stream of arrays and takes the bytes to send
through a function or a fold. It knows nothing of the protocol carried.

```haskell
{-# LANGUAGE OverloadedStrings #-}

import Streamly.Data.Stream (toList)
import Streamly.External.ByteString (toArray)
import Streamly.External.Network.TLS (config, received, send, withConnection)

main :: IO ()
main = withConnection (config "example.org" 443) $ \connection -> do
  send connection (toArray "GET / HTTP/1.0\r\nHost: example.org\r\n\r\n")
  toList (received connection) >>= print . length
```

## Certificates

The server must present a certificate valid for the name asked and signed
by a trusted authority: those of the system (`SystemCertificates`, the
default) or those of a file or a directory in PEM (`CertificatesFrom`),
which is how a server with a certificate of its own making is reached.
There is no way to skip the check.

## Servers

`acceptConnection` does the handshake of a server on a connection it has
accepted, with the certificate and the key read by `loadCredentials`:

```haskell
credentials <- loadCredentials "certificate.pem" "key.pem"
(peer, _) <- Network.Socket.accept listener
acceptConnection credentials (Just 30000000) peer $ \connection ->
  fold (write connection) (received connection)
```

## HTTPS and secure WebSockets

[streamly-http](https://github.com/GuillaumedeVolpiano/streamly-http) and
[streamly-websockets](https://github.com/GuillaumedeVolpiano/streamly-websockets)
take a connection opened by other means, as a `Transport`: `transport`
gives one from a connection of this library, which depends on neither of
them. The packages
[streamly-http-tls](https://github.com/GuillaumedeVolpiano/streamly-http-tls)
and
[streamly-websockets-tls](https://github.com/GuillaumedeVolpiano/streamly-websockets-tls)
put them together; by hand it reads:

```haskell
import Streamly.External.Network.HTTP (defaultLimits, fetchOver, get)
import Streamly.External.Network.TLS (transport, withConnection)
import Streamly.External.Network.TLS qualified as NT (config)
import Streamly.External.Network.WebSocket (Message (TextMessage), send,
                                            withConnectionOver)
import Streamly.External.Network.WebSocket qualified as WS (config)

page = withConnection (NT.config "example.org" 443) $ \connection ->
  fetchOver defaultLimits (transport connection) (get "example.org" 443 "/")

chat = withConnection (NT.config "example.org" 443) $ \connection ->
  withConnectionOver (transport connection) (WS.config "example.org" 443 "/chat") $ \socket ->
    send socket (TextMessage "hello")
```

## Building

To build against a copy of
[streamly-network-tcp](https://hackage.haskell.org/package/streamly-network-tcp)
checked out next to this repository, pass
`--project-file=cabal.project.siblings`.

## Limits of this version

- The connection and its handshake are limited in time (`connectTimeout`,
  thirty seconds by default); once open, nothing limits the waits.
- A server proves its name with a certificate and its key
  (`loadCredentials`, `acceptConnection`); it does not ask clients for
  certificates.
- No client certificate, no session resumption, no choice of protocol
  versions or ciphers: the defaults of the tls package.
- No application protocol negotiation (ALPN).