Skip to content

Commit

Permalink
Added tests for two more PKCS#8 key situations
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jan 19, 2025
1 parent 17119de commit 75947fb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/development/test-vectors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ Custom asymmetric vectors
the key (the OIDs are reversed from ``ec-inconsistent-curve.pem``).
* ``asymmetric/EC/ec-missing-curve.pem`` - A PKCS#1 encoded EC key where the
curve OID is missing.
* ``asymmetric/PKCS8/ec-consistent-curve.pem`` - A PKCS8 encoded EC key where
the the curve OID in the parameters is the same as the curve OID in the key
(encoding the curve OID twice is duplicative, as the inner curve is
optional).
* ``asymmetric/PKCS8/ec-invalid-version.pem`` - A PKCS8 encoded EC key with an
invalid elliptic curve version field.


Key exchange
Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Diffie
disambiguating
Django
Docstrings
duplicative
El
Encodings
endian
Expand Down
22 changes: 22 additions & 0 deletions tests/hazmat/primitives/test_ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,18 @@ def test_pkcs8_inconsistent_curve(self):
with pytest.raises(ValueError):
serialization.load_pem_private_key(data, password=None)

def test_pkcs8_consistent_curve(self):
# Like the above, but both the inner and outer curves match
key = load_vectors_from_file(
os.path.join("asymmetric", "PKCS8", "ec-consistent-curve.pem"),
lambda f: serialization.load_pem_private_key(
f.read(), password=None
),
mode="rb",
)
assert isinstance(key, EllipticCurvePrivateKey)
assert isinstance(key.curve, ec.SECP256R1)

def test_load_private_key_missing_curve(self):
data = load_vectors_from_file(
os.path.join("asymmetric", "EC", "ec-missing-curve.pem"),
Expand All @@ -1156,6 +1168,16 @@ def test_load_private_key_missing_curve(self):
with pytest.raises(ValueError):
serialization.load_pem_private_key(data, password=None)

@pytest.mark.xfail
def test_load_private_key_invalid_version(self):
data = load_vectors_from_file(
os.path.join("asymmetric", "PKCS8", "ec-invalid-version.pem"),
lambda f: f.read(),
mode="rb",
)
with pytest.raises(ValueError):
serialization.load_pem_private_key(data, password=None)


class TestEllipticCurvePEMPublicKeySerialization:
@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgYirTZSx+5O8Y6tlG
cka6W6btJiocdrdolfcukSoTEk+gCgYIKoZIzj0DAQehRANCAAQkvPNu7Pa1GcsW
U4v7ptNfqCJVq8Cxzo0MUVPQgwJ3aJtNM1QMOQUayCrRwfklg+D/rFSUwEUqtZh7
fJDiFqz3
-----END PRIVATE KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBEQQgYirTZSx+5O8Y6tlG
cka6W6btJiocdrdolfcukSoTEk+gCgYIKoZIzj0DAQehRANCAAQkvPNu7Pa1GcsW
U4v7ptNfqCJVq8Cxzo0MUVPQgwJ3aJtNM1QMOQUayCrRwfklg+D/rFSUwEUqtZh7
fJDiFqz3
-----END PRIVATE KEY-----

0 comments on commit 75947fb

Please sign in to comment.