Skip to content

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

docs/registration_policies.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,117 @@ Stop the server that serves the public keys
399399
```console
400400
$ kill $python_http_server_pid
401401
```
402+
403+
### Binding Notary Keys to a Trusted Platform Module
404+
405+
Check if you have a TPM and if it's TPM2
406+
407+
```echo
408+
$ echo TPM version $(cat /sys/class/tpm/tpm*/tpm_version_major)
409+
TPM version 2
410+
```
411+
412+
Upstream: https://github.com/tpm2-software/tpm2-pkcs11/blob/master/docs/SSH.md
413+
414+
Below, will be examples and discussion on how to configure SSH with tpm2-pkcs11 to ssh to
415+
the local host. The example described here could be extended for remote ssh login as well.
416+
417+
We assume a machine configured in such a state where a user can ssh locally and login with
418+
a password prompt, ala:
419+
```sh
420+
421+
[email protected]'s password:
422+
Last login: Thu Sep 6 12:23:07 2018 from 127.0.0.1
423+
```
424+
works.
425+
426+
**Thus we assume a working ssh server, client and ssh-keygen services and utilities are present.**
427+
428+
#### Step 1 - Initializing a Store
429+
430+
Start by reading the document on initialization [here](INITIALIZING.md). Only brief commands
431+
will be provided here, so a basic understanding of the initialization process is paramount.
432+
433+
We start by creating a tpm2-pkcs11 *store* and set up an RSA2048 key that SSH can used.
434+
**Note**: Most SSH configurations allow RSA2048 keys to be used, but this can be turned off
435+
in the config, but this is quite rare.
436+
437+
```bash
438+
tpm2_ptool.py init --path=~/tmp
439+
440+
tpm2_ptool.py addtoken --pid=1 --label=label --sopin=mysopin --userpin=myuserpin --path=~/tmp
441+
442+
tpm2_ptool.py addkey --algorithm=rsa2048 --label=label --userpin=myuserpin --path=~/tmp
443+
```
444+
445+
#### Step 2 - Exporting the Store
446+
447+
Since we didn't use the default store location by setting `--path` in the `tpm2-ptool` tool, we must export the
448+
store so the library can find it. We do this via:
449+
```sh
450+
export TPM2_PKCS11_STORE=$HOME/tmp
451+
```
452+
453+
**Note**: The tpm2-pkcs11.so library *WILL NOT EXPAND `~`* and thus you have to use something the shell will expand,
454+
like `$HOME`.
455+
456+
#### Step 3 - Generating the SSH key public portion
457+
458+
The next step will use `ssh-keygen` command to generate the public portion of an ssh key. The command is slightly complicated
459+
as we use tee to redirect the output to both a file called `my.pub` and to *stdout* for viewing.
460+
461+
Note: You may need to update the path to the tpm2-pkcs11 shared object below.
462+
463+
```bash
464+
ssh-keygen -D ./src/.libs/libtpm2_pkcs11.so | tee my.pub
465+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0CTmUAAB8jfNNHrw99m7K3U/+qbV1pAb7es3L+COqDh4eDqqekCm8gKHV4PFM9nW7z6CEfqzpUxYi5VvRFdYaU460bhye7NJbE0t9wjOirWtQbI6XMCKFiv/v8ThAtROT+KKYso7BK2A6spkCQwcHoaQU72C1vGouqtP5l/XRIYydp3P1wUdgQDZ8FoGhdH5dL3KnRpKR2d301GcbxMxKg5yhc/mTNkv1ZoLIcwMY7juAjzin/BhcYIDSz3sJ9C2VsX8FZXmbEo3olYU4ZfBZ+45KJ81MtWgrkXSzetwUfiH6eeTqNfqGT2IpSwDLFHTX2TsJyFDcM7Q+QR44lEU/
466+
```
467+
468+
#### Step 4 - Configuring SSH to Accept the Key
469+
470+
Now that the public portion of the key is in ssh format and located in file `my.pub` we can add this to the `authorized_keys2` file for the user:
471+
```bash
472+
cat my.pub >> ~/.ssh/authorized_keys2
473+
```
474+
475+
SSH consults this file and trusts private keys corresponding with the public entries.
476+
477+
#### Step 5 - Ensuring the Library is in a Good Path
478+
479+
Using the ssh client, we login. Note that ssh won't accept pkcs11 libraries outside of "trusted" locations. So we copy the PKCS\#11 library to
480+
a trusted location. Thus you can either do `sudo make install` to move the binary to a trusted location or just do it manually.
481+
482+
Manual Method:
483+
```sh
484+
sudo cp src/.libs/libtpm2_pkcs11.so /usr/local/lib/libtpm2_pkcs11.so
485+
```
486+
487+
On Ubuntu 16.04 with no configuration options specified to alter installation locations, they end up in the same location for both the *manual method*
488+
and `sudo make install` method.
489+
490+
#### Step 6 - Logging In via SSH
491+
492+
To log in, one used the `ssh` client application and specifies the path to the PKCS11 library via the `-I` option. It will prompt for the user PIN, which
493+
in the example is set to `myuserpin`.
494+
495+
```bash
496+
ssh -I /usr/local/lib/libtpm2_pkcs11.so 127.0.0.1
497+
Enter PIN for 'label': myuserpin
498+
Last login: Fri Sep 21 13:28:31 2018 from 127.0.0.1
499+
```
500+
501+
You are now logged in with a key resident in the TPM being exported via the tpm2-pkcs11 library.
502+
503+
#### TODO
504+
505+
- [ ] `unittest.mock.patch` the `pycose.algorithms._Ecdsa.sign` method to
506+
attempt usage of PKCS#11 module to sign.
507+
508+
```python
509+
class _Ecdsa(CoseAlgorithm, ABC):
510+
@classmethod
511+
def sign(cls, key: 'EC2', data: bytes) -> bytes:
512+
sk = SigningKey.from_secret_exponent(int(hexlify(key.d), 16), curve=cls.get_curve())
513+
514+
return sk.sign_deterministic(data, hashfunc=cls.get_hash_func())
515+
```

0 commit comments

Comments
 (0)