| 1 |
How to Check a Git Commit Signature |
| 2 |
=================================== |
| 3 |
|
| 4 |
Objective |
| 5 |
--------- |
| 6 |
|
| 7 |
This guide demonstrates how to check that a Git commit is signed |
| 8 |
with an SSH key and get the signing public key for further verification. |
| 9 |
|
| 10 |
|
| 11 |
Prerequisites |
| 12 |
------------- |
| 13 |
|
| 14 |
Python packages: |
| 15 |
|
| 16 |
* sshsig |
| 17 |
* dulwich |
| 18 |
|
| 19 |
Reviewing the |
| 20 |
[Check Signature Tutorial](../tutorial/check_signature.md) |
| 21 |
may provide useful background information. |
| 22 |
|
| 23 |
|
| 24 |
Steps |
| 25 |
----- |
| 26 |
|
| 27 |
### 1. Switch to a Git commit with an SSH signature |
| 28 |
|
| 29 |
For this guide, you can switch to any Git commit that has an SSH signature. |
| 30 |
One of many ways to do this is by cloning the `0.2.2` release |
| 31 |
of `sshsig`: |
| 32 |
|
| 33 |
```bash |
| 34 |
git clone https://github.com/castedo/sshsig.git -b 0.2.2 |
| 35 |
cd sshsig |
| 36 |
``` |
| 37 |
|
| 38 |
### 2. Get the Git commit that was signed |
| 39 |
|
| 40 |
From within a Python interpreter or script: |
| 41 |
|
| 42 |
```python |
| 43 |
import dulwich.repo |
| 44 |
|
| 45 |
repo = dulwich.repo.Repo('.') |
| 46 |
commit = repo[b'HEAD'] |
| 47 |
``` |
| 48 |
|
| 49 |
|
| 50 |
### 3. Check the signature against the original message signed |
| 51 |
|
| 52 |
With `commit` defined: |
| 53 |
|
| 54 |
```python |
| 55 |
import sshsig |
| 56 |
|
| 57 |
message = commit.raw_without_sig() |
| 58 |
signature = commit.gpgsig |
| 59 |
pub_key = sshsig.check_signature(message, signature) |
| 60 |
``` |
| 61 |
|
| 62 |
If no exception is raised, |
| 63 |
then `pub_key` is the SSH public key used to sign the Git commit. |
| 64 |
|
| 65 |
|
| 66 |
### 4. Do something with the signing public key |
| 67 |
|
| 68 |
```python |
| 69 |
print(f"HEAD commit signed with public key {pub_key}") |
| 70 |
``` |
| 71 |
|
| 72 |
Calling `check_signature` does not verify that a particular person used the |
| 73 |
public/private key pair to sign the commit. |
| 74 |
Additional steps are necessary to verify the public key is acceptable. |