1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
ssh-base Plugin
================
## Introduction
lws-ssh-base is a protcol plugin for libwebsockets that implements a
generic, abstract, ssh server.
- very small footprint in code and memory, takes up small part of ESP32
- written with security in mind: valgrind and Coverity -clean
- binds to one or more vhosts, that controls listen port(s)
- all IO and settings abstracted through a single "ops" struct from user code
- each instance on a vhost has its own "ops" struct, defining server keys,
auth method and functions to implement IO and other operations
- The plugin has no built-in behaviours like check ~/.ssh/authorized_keys,
treat auth usernames as system usernames, or spawn the user's shell.
Everything potentially dangerous is left to the user ops code to decide
how to handle. It's NOT like sshd where running it implies it will accept
existing keys for any system user, will spawn a shell, etc, unless you
implement those parts in the ops callbacks.
- The plugin requires extra code around it in the form of the ops struct
handlers. So it's role is something like an abstract base class for an ssh
server. All the crypto, protocol sequencing and state machine are inside,
but all the IO except the network connection is outside.
- Built as part of libwebsockets, like all plugins may be dynamically loaded
at runtime or built statically. Test app `libwebsockets-test-sshd` provided
- Uses hash and RSA functions from either mbedTLS or OpenSSL automatically,
according to which library libwebsockets was built for
To maintain its small size, it implements a single "best of breed" crypto for
the following functions:
|Function|Crypto|
|---|---|
|KEX|curve25519-sha256@libssh.org|
|Server host key|ssh-rsa (4096b)|
|Encryption|chacha20-poly1305@openssh.com|
|Compression|None|
## License
lws-ssh-base is Free Software, available under libwebsockets' MIT license.
The crypto parts are available elsewhere under a BSD license. But for
simplicity the whole plugin is under MIT.
## Generating your own keys
```
$ ssh-keygen -t rsa -b 4096 -f mykeys
```
will ask for a passphrase and generate the private key in `mykeys` and the
public key in `mykeys.pub`. If you already have a suitable RSA key you use
with ssh, you can just use that directly.
lws installs a test keypair in /usr[/local]/share/libwebsockets-test-server
that the test apps will accept.
## Example code
1) There's a working example app `libwebsockets-test-sshd` included that
spawns a bash shell when an ssh client authenticates. The username used on
the remote ssh has no meaning, it spawns the shell under the credentials of
"lws-test-sshd" was run under. It accepts the lws ssh test key which is
installed into /usr[/local]/share/libwebsockets-test-server.
Start the server like this (it wants root only because the server key is stored
in /etc)
```
$ sudo libwebsockets-test-sshd
```
Connect to it using the test private key like this
```
$ ssh -p 2200 -i /usr/local/share/libwebsockets-test-server/lws-ssh-test-keys anyuser@127.0.0.1
```
2) There's also a working example plugin `lws-sshd-demo` that "subclasses" the
abstract `lws-ssh-base` plugin to make a protocol which can be used from,
eg, lwsws. For an lwsws vhost that listens on port 2222 and responds with
the lws-sshd-demo ssh server, the related config is:
```
{
"name": "sshd",
"port": "2222",
"onlyraw": "1",
"ws-protocols": [{
"lws-ssh-base": {
"status": "ok",
"ops-from": "lws-sshd-demo"
},
"lws-sshd-demo": {
"status": "ok",
"raw": "1"
}
}]
}
```
## Integration to other apps
### Step 0: Build and install libwebsockets
For the `libwebsockets-test-sshd` example, you will need CMake options
`LWS_WITH_CGI`, since it uses lws helpers to spawn a shell.
lws-ssh-base itself doesn't require CGI support in libwebsockets.
### Step 1: make the code available in your app
Include `lws-plugin-ssh-base` in your app, either as a runtime plugin or by using
the lws static include scheme.
To bring in the whole of the ssh-base plugin
into your app in one step, statically, just include
`plugins/ssh-base/include/lws-plugin-sshd-static-build-includes.h`, you can see
an example of this in `./test-apps/test-sshd.c`.
### Step 2: define your `struct lws_ssh_ops`
`plugins/ssh-base/include/lws-plugin-ssh.h` defines
`struct lws_ssh_ops` which is used for all customization and integration
of the plugin per vhost. Eg,
```
static const struct lws_ssh_ops ssh_ops = {
.channel_create = ssh_ops_channel_create,
.channel_destroy = ssh_ops_channel_destroy,
.tx_waiting = ssh_ops_tx_waiting,
.tx = ssh_ops_tx,
.rx = ssh_ops_rx,
.get_server_key = ssh_ops_get_server_key,
.set_server_key = ssh_ops_set_server_key,
.set_env = ssh_ops_set_env,
.pty_req = ssh_ops_pty_req,
.child_process_io = ssh_ops_child_process_io,
.child_process_terminated = ssh_ops_child_process_terminated,
.exec = ssh_ops_exec,
.shell = ssh_ops_shell,
.is_pubkey_authorized = ssh_ops_is_pubkey_authorized,
.banner = ssh_ops_banner,
.disconnect_reason = ssh_ops_disconnect_reason,
.server_string = "SSH-2.0-Libwebsockets",
.api_version = 1,
};
```
The `ssh_ops_...()` functions are your implementations for the operations
needed by the plugin for your purposes.
### Step 3: enable `lws-ssh-base` protocol to a vhost and configure using pvo
A pointer to your struct lws_ssh_ops is passed into the vhost instance of the
protocol using per-vhost options
```
static const struct lws_protocol_vhost_options pvo_ssh_ops = {
NULL,
NULL,
"ops",
(void *)&ssh_ops
};
static const struct lws_protocol_vhost_options pvo_ssh = {
NULL,
&pvo_ssh_ops,
"lws-sshd-base",
"" /* ignored, just matches the protocol name above */
};
...
info.port = 22;
info.options = LWS_SERVER_OPTION_ONLY_RAW;
info.vhost_name = "sshd";
info.protocols = protocols_sshd;
info.pvo = &pvo_ssh;
vh_sshd = lws_create_vhost(context, &info);
```
There are two possible pvos supported, "ops", shown above, directly passes the
ops structure in using the value on the "ops" pvo.
To support other protocols that want to provide ops to lws-ssh-base themselves
for a particular vhost, you can also provide a pvo `"ops-from"` whose value is
the name of the protocol also enabled on this vhost, whose protocol ".user"
pointer points to the ops struct lws-ssh-base should use.
## Integration to other plugins
A worked example of using the abstract `lws-ssh-base` plugin from another
plugin that provides the ops struct is in `./plugins/protocol_lws_sshd_demo`.
The key points to note
- the plugin sets the ops struct for the vhost instantiation of `lws-ssh-base`
by passing a pointer to the ops struct in its `lws_protocols` struct `user`
member.
- the config for the vhost tells `lws-ssh-base` to pick up the ops struct
pointer using an "ops-from" pvo that indicates the protocol name.
```
"lws-ssh-base": {
"status": "ok",
"ops-from": "lws-sshd-demo"
},
```
- the config for the vhost tells lws this vhost only serves RAW (ie, no http)
```
{
"name": "sshd",
"port": "2222",
"onlyraw": "1",
...
```
- the config for the vhost marks the protocol that uses `lws-ssh-base`, not
`lws-ssh-base` itself, as the protocol to be served for raw connections
```
"lws-sshd-demo": {
"status": "ok",
"raw": "1"
...
```
## Notes
You can have the vhost it binds to listen on a nonstandard port. The ssh
commandline app cane be told to connect to a non-22 port with
`ssh -p portnum user@hostname`
|