-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add replacement API for '*keys' and 'interface' key generation and import #604
Merged
lukpueh
merged 16 commits into
secure-systems-lab:main
from
lukpueh:crypto-signer-keygen-and-import
Aug 11, 2023
+1,009
−185
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6b9dd85
signer: refactor SSlibSigner
lukpueh 7b5a13a
test: test SSlibSigner with all supported schemes
lukpueh 788332e
signer: move '_get_keyid' to signer._utils
lukpueh 0c54aa7
key: add 'FIXME' comment for bad method name
lukpueh 7952c3f
signer: add test key files in standard PEM format
lukpueh 7dbbb2e
key: add public key file import method
lukpueh 95059c1
signer: implement CryptoSigner.from_priv_key_uri
lukpueh d633af7
signer: add sslib key generation methods
lukpueh 16e48dd
signer: fix copy/paste mistake in CryptoSigner
lukpueh 0f965eb
signer: move SSlibSigner and CryptoSigner
lukpueh d14a21c
signer: move CryptoSigner generate functions
lukpueh de28eb7
signer: make CryptoSigner subclasses non-public
lukpueh 414460f
key: SSlibKey.from_file -> SSlibKey.from_pem
lukpueh f021d8b
docs: add CryptoSigner usage documentation
lukpueh 9ddde6d
signer: deprecate SSlibSigner and sslib keydict
lukpueh 3951fed
signer: fix rebase issues in aws_signer
lukpueh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
# CryptoSigner | ||
|
||
`CryptoSigner` is a modern replacement for the legacy `securesystemslib.keys` | ||
module. It can be used via the `Signer.from_priv_key_uri` API to load private | ||
*rsa*, *ecdsa* and *ed25519* keys from file. It also provides API to generate | ||
in-memory signers for ad-hoc signing. | ||
|
||
## Code examples | ||
|
||
### Example 1: Ad-hoc signing | ||
|
||
`CryptoSigner` provides `generate_{rsa, ed25519, ecdsa}` methods for ad-hoc | ||
signing and signature verification, e.g. in tests or demos. | ||
|
||
```python | ||
from securesystemslib.signer import CryptoSigner | ||
|
||
signer = CryptoSigner.generate_ed25519() | ||
signature = signer.sign(b"data") | ||
signer.public_key.verify_signature(signature, b"data") | ||
``` | ||
|
||
### Example 2: Asynchronous key management and signing | ||
|
||
The typical Signer API usage is described in | ||
[this blog post](https://theupdateframework.github.io/python-tuf/2023/01/24/securesystemslib-signer-api.html) | ||
and outlined below for a file-based signer. | ||
|
||
#### 1. Generate key files | ||
*`CryptoSigner` does not provide API to generate key files. Compatible | ||
keys can be generated with standard tools like `openssl genpkey` (CLI) or | ||
`pyca/cryptography` (Python).* | ||
|
||
```python | ||
from cryptography.hazmat.primitives import asymmetric, serialization | ||
|
||
# Generate key pair | ||
private_key = asymmetric.ed25519.Ed25519PrivateKey.generate() | ||
|
||
# Serialize private key as encrypted PEM/PKCS8 | ||
private_pem = private_key.private_bytes( | ||
encoding=serialization.Encoding.PEM, | ||
format=serialization.PrivateFormat.PKCS8, | ||
encryption_algorithm=serialization.BestAvailableEncryption(b"hunter2"), | ||
) | ||
|
||
# Serialize public key as encrypted PEM/subjectPublicKeyInfo | ||
public_pem = private_key.public_key().public_bytes( | ||
encoding=serialization.Encoding.PEM, | ||
format=serialization.PublicFormat.SubjectPublicKeyInfo, | ||
) | ||
|
||
# Write key files | ||
with open("private.pem", "wb") as f: | ||
f.write(private_pem) | ||
with open("public.pem", "wb") as f: | ||
f.write(public_pem) | ||
``` | ||
|
||
#### 2. Prepare signing environment | ||
|
||
```python | ||
import os | ||
from securesystemslib.signer import SSlibKey | ||
|
||
with open("public.pem", "rb") as f: | ||
public_bytes = f.read() | ||
|
||
# Make public key, signer URI, and key decryption password available to the | ||
# signer, e.g. via environment variables. The private key file must also be | ||
# available to the signer at the specified path. | ||
os.environ.update({ | ||
"SIGNER_URI": "file:private.pem?encrypted=true", | ||
"SIGNER_PUBLIC": public_bytes.decode(), | ||
"SIGNER_SECRET": "hunter2" | ||
}) | ||
``` | ||
|
||
#### 3. Load and use signer | ||
|
||
```python | ||
import os | ||
from securesystemslib.signer import SSlibKey, Signer, CryptoSigner, SIGNER_FOR_URI_SCHEME | ||
|
||
# NOTE: Registration becomes obsolete once CryptoSigner is the default file signer | ||
SIGNER_FOR_URI_SCHEME.update({CryptoSigner.FILE_URI_SCHEME: CryptoSigner}) | ||
|
||
# Read signer details | ||
uri = os.environ["SIGNER_URI"] | ||
public_key = SSlibKey.from_pem(os.environ["SIGNER_PUBLIC"].encode()) | ||
secrets_handler = lambda sec: os.environ["SIGNER_SECRET"] | ||
|
||
# Load and sign | ||
signer = Signer.from_priv_key_uri(uri, public_key, secrets_handler) | ||
signer.sign(b"data") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we change the default handler for FILE_URI_SCHEME? (and maybe leave a comment that ENVVAR_URI_SCHEME is deprecated unless someone reimplements it)