Skip to content

Add support for ed25519 keys #1377

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
if: ${{ matrix.conf.os == 'ubuntu-latest' }}
run: |
# Fetch the latest sigstore/timestamp-authority build
SIGSTORE_TIMESTAMP_VERSION=$(gh api /repos/sigstore/timestamp-authority/tags --jq '.[0].name')
SIGSTORE_TIMESTAMP_VERSION=$(gh api /repos/sigstore/timestamp-authority/releases --jq '.[0].tag_name')
wget https://github.com/sigstore/timestamp-authority/releases/download/${SIGSTORE_TIMESTAMP_VERSION}/timestamp-server-linux-amd64 -O /tmp/timestamp-server
chmod +x /tmp/timestamp-server

Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ All versions prior to 0.9.0 are untracked.

## [Unreleased]

### Added

* Added support for ed25519 keys.
[#1377](https://github.com/sigstore/sigstore-python/pull/1377)

### Fixed

* TSA: Changed the Timestamp Authority requests to explicitly use sha256 for message digests.
Expand All @@ -20,6 +25,10 @@ All versions prior to 0.9.0 are untracked.
* API: Make Rekor APIs compatible with Rekor v2 by removing trailing slashes
from endpoints ([#1366](https://github.com/sigstore/sigstore-python/pull/1366))

* CI: Timestamp Authority tests use latest release, not latest tag, of
[sigstore/timestamp-authority](https://github.com/sigstore/timestamp-authority)
[#1377](https://github.com/sigstore/sigstore-python/pull/1377)

### Changed

* `--trust-config` now requires a file with SigningConfig v0.2, and is able to fully
Expand Down
28 changes: 22 additions & 6 deletions sigstore/_internal/trust.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
from datetime import datetime, timezone
from enum import Enum
from pathlib import Path
from typing import ClassVar, NewType
from typing import ClassVar, NewType, Optional

import cryptography.hazmat.primitives.asymmetric.padding as padding
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec, rsa
from cryptography.hazmat.primitives.asymmetric import ec, ed25519, rsa
from cryptography.x509 import (
Certificate,
load_der_x509_certificate,
Expand Down Expand Up @@ -94,7 +94,7 @@ class Key:
Represents a key in a `Keyring`.
"""

hash_algorithm: hashes.HashAlgorithm
hash_algorithm: Optional[hashes.HashAlgorithm]
key: PublicKey
key_id: KeyID

Expand All @@ -121,7 +121,7 @@ def __init__(self, public_key: _PublicKey) -> None:
if not public_key.raw_bytes:
raise VerificationError("public key is empty")

hash_algorithm: hashes.HashAlgorithm
hash_algorithm: Optional[hashes.HashAlgorithm]
if public_key.key_details in self._RSA_SHA_256_DETAILS:
hash_algorithm = hashes.SHA256()
key = load_der_public_key(public_key.raw_bytes, types=(rsa.RSAPublicKey,))
Expand All @@ -130,6 +130,11 @@ def __init__(self, public_key: _PublicKey) -> None:
key = load_der_public_key(
public_key.raw_bytes, types=(ec.EllipticCurvePublicKey,)
)
elif public_key.key_details == _PublicKeyDetails.PKIX_ED25519:
hash_algorithm = None
key = load_der_public_key(
public_key.raw_bytes, types=(ed25519.Ed25519PublicKey,)
)
else:
raise VerificationError(f"unsupported key type: {public_key.key_details}")

Expand All @@ -141,20 +146,31 @@ def verify(self, signature: bytes, data: bytes) -> None:
"""
Verifies the given `data` against `signature` using the current key.
"""
if isinstance(self.key, rsa.RSAPublicKey):
if isinstance(self.key, rsa.RSAPublicKey) and self.hash_algorithm is not None:
self.key.verify(
signature=signature,
data=data,
# TODO: Parametrize this as well, for PSS.
padding=padding.PKCS1v15(),
algorithm=self.hash_algorithm,
)
elif isinstance(self.key, ec.EllipticCurvePublicKey):
elif (
isinstance(self.key, ec.EllipticCurvePublicKey)
and self.hash_algorithm is not None
):
self.key.verify(
signature=signature,
data=data,
signature_algorithm=ec.ECDSA(self.hash_algorithm),
)
elif (
isinstance(self.key, ed25519.Ed25519PublicKey)
and self.hash_algorithm is None
):
self.key.verify(
signature=signature,
data=data,
)
else:
# Unreachable without API misuse.
raise VerificationError(f"keyring: unsupported key: {self.key}")
Expand Down
22 changes: 17 additions & 5 deletions sigstore/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from typing import IO, NewType, Union

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec, rsa
from cryptography.hazmat.primitives.asymmetric import ec, ed25519, rsa
from cryptography.x509 import (
Certificate,
ExtensionNotFound,
Expand All @@ -43,9 +43,13 @@
from importlib import resources


PublicKey = Union[rsa.RSAPublicKey, ec.EllipticCurvePublicKey]
PublicKey = Union[rsa.RSAPublicKey, ec.EllipticCurvePublicKey, ed25519.Ed25519PublicKey]

PublicKeyTypes = Union[type[rsa.RSAPublicKey], type[ec.EllipticCurvePublicKey]]
PublicKeyTypes = Union[
type[rsa.RSAPublicKey],
type[ec.EllipticCurvePublicKey],
type[ed25519.Ed25519PublicKey],
]

HexStr = NewType("HexStr", str)
"""
Expand All @@ -64,7 +68,11 @@
def load_pem_public_key(
key_pem: bytes,
*,
types: tuple[PublicKeyTypes, ...] = (rsa.RSAPublicKey, ec.EllipticCurvePublicKey),
types: tuple[PublicKeyTypes, ...] = (
rsa.RSAPublicKey,
ec.EllipticCurvePublicKey,
ed25519.Ed25519PublicKey,
),
) -> PublicKey:
"""
A specialization of `cryptography`'s `serialization.load_pem_public_key`
Expand All @@ -86,7 +94,11 @@ def load_pem_public_key(
def load_der_public_key(
key_der: bytes,
*,
types: tuple[PublicKeyTypes, ...] = (rsa.RSAPublicKey, ec.EllipticCurvePublicKey),
types: tuple[PublicKeyTypes, ...] = (
rsa.RSAPublicKey,
ec.EllipticCurvePublicKey,
ed25519.Ed25519PublicKey,
),
) -> PublicKey:
"""
The `load_pem_public_key` specialization, but DER.
Expand Down