aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2015-08-05 17:30:39 +0100
committerPaul Kehrer <paul.l.kehrer@gmail.com>2015-08-08 14:58:07 -0500
commit253929a85c45c1313fd68d10ec7a7a45e380d5c0 (patch)
tree8e52c5b3e93d053319a0658dae32026cf39bd050 /src
parentd5bf17ad99939920aa73e6d00a36818ecaf1c2cc (diff)
downloadcryptography-253929a85c45c1313fd68d10ec7a7a45e380d5c0.tar.gz
cryptography-253929a85c45c1313fd68d10ec7a7a45e380d5c0.tar.bz2
cryptography-253929a85c45c1313fd68d10ec7a7a45e380d5c0.zip
add AuthorityKeyIdentifier from_issuer_public_key
Refactored SKI's creation code into a separate function, added doctest examples
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/x509.py49
1 files changed, 31 insertions, 18 deletions
diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py
index 5ed3c094..3f306e3a 100644
--- a/src/cryptography/x509.py
+++ b/src/cryptography/x509.py
@@ -32,6 +32,27 @@ class _SubjectPublicKeyInfo(univ.Sequence):
)
+def _key_identifier_from_public_key(public_key):
+ # This is a very slow way to do this.
+ serialized = public_key.public_bytes(
+ serialization.Encoding.DER,
+ serialization.PublicFormat.SubjectPublicKeyInfo
+ )
+ spki, remaining = decoder.decode(
+ serialized, asn1Spec=_SubjectPublicKeyInfo()
+ )
+ # the univ.BitString object is a tuple of bits. We need bytes and
+ # pyasn1 really doesn't want to give them to us. To get it we'll
+ # build an integer and convert that to bytes.
+ assert not remaining
+ bits = 0
+ for bit in spki.getComponentByName("subjectPublicKey"):
+ bits = bits << 1 | bit
+
+ data = utils.int_to_bytes(bits)
+ return hashlib.sha1(data).digest()
+
+
_OID_NAMES = {
"2.5.4.3": "commonName",
"2.5.4.6": "countryName",
@@ -710,24 +731,7 @@ class SubjectKeyIdentifier(object):
@classmethod
def from_public_key(cls, public_key):
- # This is a very slow way to do this.
- serialized = public_key.public_bytes(
- serialization.Encoding.DER,
- serialization.PublicFormat.SubjectPublicKeyInfo
- )
- spki, remaining = decoder.decode(
- serialized, asn1Spec=_SubjectPublicKeyInfo()
- )
- assert not remaining
- # the univ.BitString object is a tuple of bits. We need bytes and
- # pyasn1 really doesn't want to give them to us. To get it we'll
- # build an integer and convert that to bytes.
- bits = 0
- for bit in spki.getComponentByName("subjectPublicKey"):
- bits = bits << 1 | bit
-
- data = utils.int_to_bytes(bits)
- return cls(hashlib.sha1(data).digest())
+ return cls(_key_identifier_from_public_key(public_key))
digest = utils.read_only_property("_digest")
@@ -1318,6 +1322,15 @@ class AuthorityKeyIdentifier(object):
self._authority_cert_issuer = authority_cert_issuer
self._authority_cert_serial_number = authority_cert_serial_number
+ @classmethod
+ def from_issuer_public_key(cls, public_key):
+ digest = _key_identifier_from_public_key(public_key)
+ return cls(
+ key_identifier=digest,
+ authority_cert_issuer=None,
+ authority_cert_serial_number=None
+ )
+
def __repr__(self):
return (
"<AuthorityKeyIdentifier(key_identifier={0.key_identifier!r}, "