| 1 |
Check Signature Tutorial |
| 2 |
======================== |
| 3 |
|
| 4 |
Objective |
| 5 |
--------- |
| 6 |
|
| 7 |
In this tutorial, you will |
| 8 |
check that an SSH signature is for a specific message |
| 9 |
and print the public key that was used to sign the message. |
| 10 |
|
| 11 |
|
| 12 |
Prerequisites |
| 13 |
------------- |
| 14 |
|
| 15 |
You need to install the `sshsig` Python package. |
| 16 |
A popular way to do this is by running: |
| 17 |
|
| 18 |
``` |
| 19 |
pip install sshsig |
| 20 |
``` |
| 21 |
|
| 22 |
The steps in this tutorial include lines of Python code to be run within a Python interpreter. |
| 23 |
A popular way to run the Python interpreter is to run: |
| 24 |
``` |
| 25 |
python3 |
| 26 |
``` |
| 27 |
from the command line. |
| 28 |
|
| 29 |
|
| 30 |
Steps |
| 31 |
----- |
| 32 |
|
| 33 |
### 1. Get the message that was signed. |
| 34 |
|
| 35 |
```python |
| 36 |
message = "Hello World.\n" |
| 37 |
``` |
| 38 |
|
| 39 |
The author of this tutorial signed the above message using a |
| 40 |
[public/private key pair](https://en.wikipedia.org/wiki/Public-key_cryptography). |
| 41 |
|
| 42 |
|
| 43 |
### 2. Get the plain-text encoded signature. |
| 44 |
|
| 45 |
```python |
| 46 |
signature = """ |
| 47 |
-----BEGIN SSH SIGNATURE----- |
| 48 |
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAghB1C63jrmh3eWRXJVbrTfw9wP/ |
| 49 |
BIZf/aKPdFxBlMCq0AAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5 |
| 50 |
AAAAQCK1pq8IPKuxTat0RJbZ0LjElEgXNowFdFKElzuq9gunl2b4DHrla0FTkUJKp1eOk1 |
| 51 |
+GOGN+EejKcsFnrfmZ1gE= |
| 52 |
-----END SSH SIGNATURE----- |
| 53 |
""" |
| 54 |
``` |
| 55 |
|
| 56 |
The above SSH signature was generated by the `ssh-keygen` utility |
| 57 |
using the message and the author's public/private key pair. |
| 58 |
|
| 59 |
|
| 60 |
### 3. Check the signature with the message. |
| 61 |
|
| 62 |
```python |
| 63 |
import sshsig |
| 64 |
pub_key = sshsig.check_signature(message, signature) |
| 65 |
``` |
| 66 |
|
| 67 |
Since `check_signature` returns without raising an exception, we know that |
| 68 |
the signature resulted from signing `message`. |
| 69 |
The public key returned is from the public/private key pair used to sign |
| 70 |
the message. |
| 71 |
|
| 72 |
|
| 73 |
### 4. Print the public key used to sign. |
| 74 |
|
| 75 |
We can also print out a textual representation of the signing public key in OpenSSH format. |
| 76 |
|
| 77 |
``` |
| 78 |
str(pub_key) |
| 79 |
``` |
| 80 |
|
| 81 |
|
| 82 |
### 5. Check with a different message. |
| 83 |
|
| 84 |
The signature is specifically for `message`. |
| 85 |
Checking a different message will fail. |
| 86 |
|
| 87 |
```python |
| 88 |
sshsig.check_signature("A different message", signature) |
| 89 |
``` |
| 90 |
|
| 91 |
An `sshsig.sshsig.InvalidSignature` exception is raised, |
| 92 |
indicating that the signature is not for a different message. |
| 93 |
|
| 94 |
|
| 95 |
### Conclusion |
| 96 |
|
| 97 |
You have successfully checked that the signature is for the message in this tutorial. |
| 98 |
You have also printed the public key that was used to sign the message. |