Python cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey() Examples
The following are 30
code examples of cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cryptography.hazmat.primitives.asymmetric.ec
, or try the search function
.
Example #1
Source File: extensions.py From quickstart-redhat-openshift with Apache License 2.0 | 6 votes |
def _key_identifier_from_public_key(public_key): if isinstance(public_key, RSAPublicKey): data = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.PKCS1, ) elif isinstance(public_key, EllipticCurvePublicKey): data = public_key.public_bytes( serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint ) else: # This is a very slow way to do this. serialized = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo ) data = bytes(PublicKeyInfo.load(serialized)['public_key']) return hashlib.sha1(data).digest()
Example #2
Source File: extensions.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _key_identifier_from_public_key(public_key): if isinstance(public_key, RSAPublicKey): data = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.PKCS1, ) elif isinstance(public_key, EllipticCurvePublicKey): data = public_key.public_bytes( serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint ) else: # This is a very slow way to do this. serialized = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo ) data = bytes(PublicKeyInfo.load(serialized)['public_key']) return hashlib.sha1(data).digest()
Example #3
Source File: keys.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def type(self): """ Return the type of the object we wrap. Currently this can only be 'RSA', 'DSA', or 'EC'. @rtype: L{str} @raises RuntimeError: If the object type is unknown. """ if isinstance( self._keyObject, (rsa.RSAPublicKey, rsa.RSAPrivateKey)): return 'RSA' elif isinstance( self._keyObject, (dsa.DSAPublicKey, dsa.DSAPrivateKey)): return 'DSA' elif isinstance( self._keyObject, (ec.EllipticCurvePublicKey, ec.EllipticCurvePrivateKey)): return 'EC' else: raise RuntimeError( 'unknown type of object: %r' % (self._keyObject,))
Example #4
Source File: extensions.py From quickstart-git2s3 with Apache License 2.0 | 6 votes |
def _key_identifier_from_public_key(public_key): if isinstance(public_key, RSAPublicKey): data = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.PKCS1, ) elif isinstance(public_key, EllipticCurvePublicKey): data = public_key.public_numbers().encode_point() else: # This is a very slow way to do this. serialized = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo ) data = six.binary_type(PublicKeyInfo.load(serialized)['public_key']) return hashlib.sha1(data).digest()
Example #5
Source File: extensions.py From teleport with Apache License 2.0 | 6 votes |
def _key_identifier_from_public_key(public_key): if isinstance(public_key, RSAPublicKey): data = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.PKCS1, ) elif isinstance(public_key, EllipticCurvePublicKey): data = public_key.public_numbers().encode_point() else: # This is a very slow way to do this. serialized = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo ) data = six.binary_type(PublicKeyInfo.load(serialized)['public_key']) return hashlib.sha1(data).digest()
Example #6
Source File: keys.py From learn_python3_spider with MIT License | 6 votes |
def type(self): """ Return the type of the object we wrap. Currently this can only be 'RSA', 'DSA', or 'EC'. @rtype: L{str} @raises RuntimeError: If the object type is unknown. """ if isinstance( self._keyObject, (rsa.RSAPublicKey, rsa.RSAPrivateKey)): return 'RSA' elif isinstance( self._keyObject, (dsa.DSAPublicKey, dsa.DSAPrivateKey)): return 'DSA' elif isinstance( self._keyObject, (ec.EllipticCurvePublicKey, ec.EllipticCurvePrivateKey)): return 'EC' else: raise RuntimeError( 'unknown type of object: %r' % (self._keyObject,))
Example #7
Source File: test_es256.py From google-auth-library-python with Apache License 2.0 | 5 votes |
def test_from_string_pub_cert(self): verifier = es256.ES256Verifier.from_string(PUBLIC_CERT_BYTES) assert isinstance(verifier, es256.ES256Verifier) assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)
Example #8
Source File: test_es256.py From google-auth-library-python with Apache License 2.0 | 5 votes |
def test_from_string_pub_key(self): verifier = es256.ES256Verifier.from_string(PUBLIC_KEY_BYTES) assert isinstance(verifier, es256.ES256Verifier) assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)
Example #9
Source File: test_es256.py From google-auth-library-python with Apache License 2.0 | 5 votes |
def test_from_string_pub_cert_unicode(self): public_cert = _helpers.from_bytes(PUBLIC_CERT_BYTES) verifier = es256.ES256Verifier.from_string(public_cert) assert isinstance(verifier, es256.ES256Verifier) assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)
Example #10
Source File: base.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def public_key(self, key): """ Sets the requestor's public key (as found in the signing request). """ if not isinstance(key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') if self._public_key is not None: raise ValueError('The public key may only be set once.') return CertificateBuilder( self._issuer_name, self._subject_name, key, self._serial_number, self._not_valid_before, self._not_valid_after, self._extensions )
Example #11
Source File: keys.py From learn_python3_spider with MIT License | 5 votes |
def _fromECEncodedPoint(cls, encodedPoint, curve, privateValue=None): """ Build a key from an EC encoded point. @param encodedPoint: The public point encoded as in SEC 1 v2.0 section 2.3.3. @type encodedPoint: L{bytes} @param curve: NIST name of elliptic curve. @type curve: L{bytes} @param privateValue: The private value. @type privateValue: L{int} """ if privateValue is None: # We have public components. keyObject = ec.EllipticCurvePublicKey.from_encoded_point( _curveTable[curve], encodedPoint ) else: keyObject = ec.derive_private_key( privateValue, _curveTable[curve], default_backend() ) return cls(keyObject)
Example #12
Source File: keys.py From learn_python3_spider with MIT License | 5 votes |
def isPublic(self): """ Check if this instance is a public key. @return: C{True} if this is a public key. """ return isinstance( self._keyObject, (rsa.RSAPublicKey, dsa.DSAPublicKey, ec.EllipticCurvePublicKey))
Example #13
Source File: x509.py From learn_python3_spider with MIT License | 5 votes |
def is_signature_valid(self, public_key): if not isinstance(public_key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') res = self._backend._lib.X509_CRL_verify( self._x509_crl, public_key._evp_pkey ) if res != 1: self._backend._consume_errors() return False return True
Example #14
Source File: utils.py From lemur with Apache License 2.0 | 5 votes |
def check_cert_signature(cert, issuer_public_key): """ Check a certificate's signature against an issuer public key. Before EC validation, make sure we support the algorithm, otherwise raise UnsupportedAlgorithm On success, returns None; on failure, raises UnsupportedAlgorithm or InvalidSignature. """ if isinstance(issuer_public_key, rsa.RSAPublicKey): # RSA requires padding, just to make life difficult for us poor developers :( if cert.signature_algorithm_oid == x509.SignatureAlgorithmOID.RSASSA_PSS: # In 2005, IETF devised a more secure padding scheme to replace PKCS #1 v1.5. To make sure that # nobody can easily support or use it, they mandated lots of complicated parameters, unlike any # other X.509 signature scheme. # https://tools.ietf.org/html/rfc4056 raise UnsupportedAlgorithm("RSASSA-PSS not supported") else: padder = padding.PKCS1v15() issuer_public_key.verify( cert.signature, cert.tbs_certificate_bytes, padder, cert.signature_hash_algorithm, ) elif isinstance(issuer_public_key, ec.EllipticCurvePublicKey) and isinstance( ec.ECDSA(cert.signature_hash_algorithm), ec.ECDSA ): issuer_public_key.verify( cert.signature, cert.tbs_certificate_bytes, ec.ECDSA(cert.signature_hash_algorithm), ) else: raise UnsupportedAlgorithm( "Unsupported Algorithm '{var}'.".format( var=cert.signature_algorithm_oid._name ) )
Example #15
Source File: x509.py From quickstart-redhat-openshift with Apache License 2.0 | 5 votes |
def is_signature_valid(self, public_key): if not isinstance(public_key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') res = self._backend._lib.X509_CRL_verify( self._x509_crl, public_key._evp_pkey ) if res != 1: self._backend._consume_errors() return False return True
Example #16
Source File: base.py From quickstart-git2s3 with Apache License 2.0 | 5 votes |
def public_key(self, key): """ Sets the requestor's public key (as found in the signing request). """ if not isinstance(key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') if self._public_key is not None: raise ValueError('The public key may only be set once.') return CertificateBuilder( self._issuer_name, self._subject_name, key, self._serial_number, self._not_valid_before, self._not_valid_after, self._extensions )
Example #17
Source File: x509.py From quickstart-git2s3 with Apache License 2.0 | 5 votes |
def is_signature_valid(self, public_key): if not isinstance(public_key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') res = self._backend._lib.X509_CRL_verify( self._x509_crl, public_key._evp_pkey ) if res != 1: self._backend._consume_errors() return False return True
Example #18
Source File: backend.py From quickstart-git2s3 with Apache License 2.0 | 5 votes |
def _openssh_public_key_bytes(self, key): if isinstance(key, rsa.RSAPublicKey): public_numbers = key.public_numbers() return b"ssh-rsa " + base64.b64encode( serialization._ssh_write_string(b"ssh-rsa") + serialization._ssh_write_mpint(public_numbers.e) + serialization._ssh_write_mpint(public_numbers.n) ) elif isinstance(key, dsa.DSAPublicKey): public_numbers = key.public_numbers() parameter_numbers = public_numbers.parameter_numbers return b"ssh-dss " + base64.b64encode( serialization._ssh_write_string(b"ssh-dss") + serialization._ssh_write_mpint(parameter_numbers.p) + serialization._ssh_write_mpint(parameter_numbers.q) + serialization._ssh_write_mpint(parameter_numbers.g) + serialization._ssh_write_mpint(public_numbers.y) ) else: assert isinstance(key, ec.EllipticCurvePublicKey) public_numbers = key.public_numbers() try: curve_name = { ec.SECP256R1: b"nistp256", ec.SECP384R1: b"nistp384", ec.SECP521R1: b"nistp521", }[type(public_numbers.curve)] except KeyError: raise ValueError( "Only SECP256R1, SECP384R1, and SECP521R1 curves are " "supported by the SSH public key format" ) return b"ecdsa-sha2-" + curve_name + b" " + base64.b64encode( serialization._ssh_write_string(b"ecdsa-sha2-" + curve_name) + serialization._ssh_write_string(curve_name) + serialization._ssh_write_string(public_numbers.encode_point()) )
Example #19
Source File: base.py From quickstart-redhat-openshift with Apache License 2.0 | 5 votes |
def public_key(self, key): """ Sets the requestor's public key (as found in the signing request). """ if not isinstance(key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') if self._public_key is not None: raise ValueError('The public key may only be set once.') return CertificateBuilder( self._issuer_name, self._subject_name, key, self._serial_number, self._not_valid_before, self._not_valid_after, self._extensions )
Example #20
Source File: ecdsa_verify.py From jws with Apache License 2.0 | 5 votes |
def __init__(self, pub_key, algorithm): """Constructor for EcdsaVerify. Args: pub_key: ec.EllipticCurvePublicKey, the Ecdsa public key. algorithm: string, Ecdsa algorithm as defined at https://tools.ietf.org/html/rfc7518#section-3.1. Raises: TypeError: if the public key is not an instance of ec.EllipticCurvePublicKey. UnsupportedAlgorithm: if the algorithm is not supported. """ if not isinstance(pub_key, ec.EllipticCurvePublicKey): raise TypeError( "The public key must be an instance of ec.EllipticCurvePublicKey") self.pub_key = pub_key curve_name = "" if algorithm == "ES256": self.hash = hashes.SHA256() curve_name = "secp256r1" elif algorithm == "ES384": self.hash = hashes.SHA384() curve_name = "secp384r1" elif algorithm == "ES512": self.hash = hashes.SHA512() curve_name = "secp521r1" else: raise exceptions.UnsupportedAlgorithm( "Unknown algorithm : %s" % (algorithm)) # In Ecdsa, both the key and the algorithm define the curve. Therefore, we # must cross check them to make sure they're the same. if curve_name != pub_key.curve.name: raise exceptions.UnsupportedAlgorithm( "The curve in public key %s and in algorithm % don't match" % (pub_key.curve.name, curve_name)) self.algorithm = algorithm
Example #21
Source File: x509.py From teleport with Apache License 2.0 | 5 votes |
def is_signature_valid(self, public_key): if not isinstance(public_key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') res = self._backend._lib.X509_CRL_verify( self._x509_crl, public_key._evp_pkey ) if res != 1: self._backend._consume_errors() return False return True
Example #22
Source File: _cli_connector.py From sslyze with GNU Affero General Public License v3.0 | 5 votes |
def _get_basic_certificate_text(cls, certificate: Certificate) -> List[str]: text_output = [ cls._format_field( "SHA1 Fingerprint:", binascii.hexlify(certificate.fingerprint(hashes.SHA1())).decode("ascii") ), cls._format_field("Common Name:", _get_name_as_short_text(certificate.subject)), cls._format_field("Issuer:", _get_name_as_short_text(certificate.issuer)), cls._format_field("Serial Number:", str(certificate.serial_number)), cls._format_field("Not Before:", certificate.not_valid_before.date().isoformat()), cls._format_field("Not After:", certificate.not_valid_after.date().isoformat()), cls._format_field("Public Key Algorithm:", certificate.public_key().__class__.__name__), ] if certificate.signature_hash_algorithm: # The signature_hash_algorithm can be None if signature did not use separate hash (ED25519, ED448) # https://cryptography.io/en/latest/x509/reference/#cryptography.x509.Certificate.signature_hash_algorithm text_output.append(cls._format_field("Signature Algorithm:", certificate.signature_hash_algorithm.name)) public_key = certificate.public_key() if isinstance(public_key, EllipticCurvePublicKey): text_output.append(cls._format_field("Key Size:", str(public_key.curve.key_size))) text_output.append(cls._format_field("Curve:", str(public_key.curve.name))) elif isinstance(public_key, RSAPublicKey): text_output.append(cls._format_field("Key Size:", str(public_key.key_size))) text_output.append(cls._format_field("Exponent:", str(public_key.public_numbers().e))) # type: ignore else: # DSA Key? https://github.com/nabla-c0d3/sslyze/issues/314 pass try: # Print the SAN extension if there's one text_output.append( cls._format_field( "DNS Subject Alternative Names:", str(extract_dns_subject_alternative_names(certificate)) ) ) except KeyError: pass return text_output
Example #23
Source File: jwk.py From jwcrypto with GNU Lesser General Public License v3.0 | 5 votes |
def import_from_pyca(self, key): if isinstance(key, rsa.RSAPrivateKey): self._import_pyca_pri_rsa(key) elif isinstance(key, rsa.RSAPublicKey): self._import_pyca_pub_rsa(key) elif isinstance(key, ec.EllipticCurvePrivateKey): self._import_pyca_pri_ec(key) elif isinstance(key, ec.EllipticCurvePublicKey): self._import_pyca_pub_ec(key) elif isinstance(key, (Ed25519PrivateKey, Ed448PrivateKey)): self._import_pyca_pri_okp(key) elif isinstance(key, (Ed25519PublicKey, Ed448PublicKey)): self._import_pyca_pub_okp(key) else: raise InvalidJWKValue('Unknown key object %r' % key)
Example #24
Source File: base.py From oss-ftp with MIT License | 5 votes |
def public_key(self, key): """ Sets the requestor's public key (as found in the signing request). """ if not isinstance(key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') if self._public_key is not None: raise ValueError('The public key may only be set once.') return CertificateBuilder( self._issuer_name, self._subject_name, key, self._serial_number, self._not_valid_before, self._not_valid_after, self._extensions )
Example #25
Source File: keys.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def isPublic(self): """ Check if this instance is a public key. @return: C{True} if this is a public key. """ return isinstance( self._keyObject, (rsa.RSAPublicKey, dsa.DSAPublicKey, ec.EllipticCurvePublicKey))
Example #26
Source File: x509.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def is_signature_valid(self, public_key): if not isinstance(public_key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') res = self._backend._lib.X509_CRL_verify( self._x509_crl, public_key._evp_pkey ) if res != 1: self._backend._consume_errors() return False return True
Example #27
Source File: base.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def public_key(self, key): """ Sets the requestor's public key (as found in the signing request). """ if not isinstance(key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') if self._public_key is not None: raise ValueError('The public key may only be set once.') return CertificateBuilder( self._issuer_name, self._subject_name, key, self._serial_number, self._not_valid_before, self._not_valid_after, self._extensions )
Example #28
Source File: extensions.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _key_identifier_from_public_key(public_key): if isinstance(public_key, RSAPublicKey): data = public_key.public_bytes( serialization.Encoding.DER, serialization.PublicFormat.PKCS1, ) elif isinstance(public_key, EllipticCurvePublicKey): data = public_key.public_numbers().encode_point() else: # 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 hashlib.sha1(data).digest()
Example #29
Source File: base.py From teleport with Apache License 2.0 | 5 votes |
def public_key(self, key): """ Sets the requestor's public key (as found in the signing request). """ if not isinstance(key, (dsa.DSAPublicKey, rsa.RSAPublicKey, ec.EllipticCurvePublicKey)): raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' ' or EllipticCurvePublicKey.') if self._public_key is not None: raise ValueError('The public key may only be set once.') return CertificateBuilder( self._issuer_name, self._subject_name, key, self._serial_number, self._not_valid_before, self._not_valid_after, self._extensions )
Example #30
Source File: test_es256.py From google-auth-library-python with Apache License 2.0 | 5 votes |
def test_from_string_pub_key_unicode(self): public_key = _helpers.from_bytes(PUBLIC_KEY_BYTES) verifier = es256.ES256Verifier.from_string(public_key) assert isinstance(verifier, es256.ES256Verifier) assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)