Skip to content
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

Added new test cases for invalid EC keys #12309

Merged
merged 1 commit into from
Jan 19, 2025
Merged
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
7 changes: 7 additions & 0 deletions docs/development/test-vectors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ Custom asymmetric vectors
* ``asymmetric/Traditional_OpenSSL_Serialization/dsa-wrong-version.pem`` - A
DSA key, encoded as a "traditional" ``DSA PRIVATE KEY`` PEM block, with an
invalid version number.
* ``asymmetric/PKCS8/ec-inconsistent-curve.pem`` - A PKCS8 encoded EC key where
the the curve OID in the parameters does not match the curve OID in the key.
* ``asymmetric/PKCS8/ec-inconsistent-curve2.pem`` - A PKCS8 encoded EC key
where the the curve OID in the parameters does not match the curve OID in
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.


Key exchange
Expand Down
29 changes: 29 additions & 0 deletions tests/hazmat/primitives/test_ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,35 @@ def test_load_public_keys(self, key_file, curve, backend):
assert isinstance(key, ec.EllipticCurvePublicKey)
assert isinstance(key.curve, curve)

@pytest.mark.xfail
def test_pkcs8_inconsistent_curve(self):
# The curve can appear twice in a PKCS8 EC key, error if they're not
# consistent
data = load_vectors_from_file(
os.path.join("asymmetric", "PKCS8", "ec-inconsistent-curve.pem"),
lambda f: f.read(),
mode="rb",
)
with pytest.raises(ValueError):
serialization.load_pem_private_key(data, password=None)

data = load_vectors_from_file(
os.path.join("asymmetric", "PKCS8", "ec-inconsistent-curve2.pem"),
lambda f: f.read(),
mode="rb",
)
with pytest.raises(ValueError):
serialization.load_pem_private_key(data, password=None)

def test_load_private_key_missing_curve(self):
data = load_vectors_from_file(
os.path.join("asymmetric", "EC", "ec-missing-curve.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,5 @@
-----BEGIN EC PRIVATE KEY-----
MGsCAQEEIGIq02UsfuTvGOrZRnJGulum7SYqHHa3aJX3LpEqExJPoUQDQgAEJLzz
buz2tRnLFlOL+6bTX6giVavAsc6NDFFT0IMCd2ibTTNUDDkFGsgq0cH5JYPg/6xU
lMBFKrWYe3yQ4has9w==
-----END EC PRIVATE KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PRIVATE KEY-----
MIGQAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHYwdAIBAQQgYirTZSx+5O8Y6tlG
cka6W6btJiocdrdolfcukSoTEk+gBwYFK4EEACKhRANCAAQkvPNu7Pa1GcsWU4v7
ptNfqCJVq8Cxzo0MUVPQgwJ3aJtNM1QMOQUayCrRwfklg+D/rFSUwEUqtZh7fJDi
Fqz3
-----END PRIVATE KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PRIVATE KEY-----
MIGQAgEAMBAGByqGSM49AgEGBSuBBAAiBHkwdwIBAQQgYirTZSx+5O8Y6tlGcka6
W6btJiocdrdolfcukSoTEk+gCgYIKoZIzj0DAQehRANCAAQkvPNu7Pa1GcsWU4v7
ptNfqCJVq8Cxzo0MUVPQgwJ3aJtNM1QMOQUayCrRwfklg+D/rFSUwEUqtZh7fJDi
Fqz3
-----END PRIVATE KEY-----
Loading