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

Add more stubs for cryptography #3307

Merged
merged 4 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 0 additions & 3 deletions third_party/2and3/cryptography/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from typing import Any

def __getattr__(name: str) -> Any: ...
6 changes: 6 additions & 0 deletions third_party/2and3/cryptography/exceptions.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AlreadyFinalized(Exception): ...
class AlreadyUpdated(Exception): ...
class InvalidSignature(Exception): ...
jlaine marked this conversation as resolved.
Show resolved Hide resolved
class InvalidTag(Exception): ...
class NotYetFinalized(Exception): ...
class UnsupportedAlgorithm(Exception): ...
jlaine marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions third_party/2and3/cryptography/fernet.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import List, Optional

class Fernet:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, key: bytes) -> None: ...
def decrypt(self, token: bytes, ttl: Optional[int]) -> bytes: ...
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def encrypt(self, data: bytes) -> bytes: ...
def extract_timestamp(self, token: bytes) -> int: ...
@classmethod
def generate_key(cls) -> bytes: ...

class MultiFernet:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, fernets: List[Fernet]) -> None: ...
def decrypt(self, token: bytes, ttl: Optional[int]) -> bytes: ...
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def encrypt(self, data: bytes) -> bytes: ...
def rotate(self, msg: bytes) -> bytes: ...
jlaine marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions third_party/2and3/cryptography/hazmat/backends/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from typing import Any

def default_backend() -> Any: ...
196 changes: 196 additions & 0 deletions third_party/2and3/cryptography/hazmat/backends/interfaces.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
from abc import ABCMeta, abstractmethod
from typing import Any, Optional, Union

from cryptography.hazmat.primitives.asymmetric.dh import (
DHParameterNumbers,
DHParameters,
DHPrivateKey,
DHPrivateNumbers,
DHPublicKey,
DHPublicNumbers,
)
from cryptography.hazmat.primitives.asymmetric.dsa import (
DSAParameterNumbers,
DSAParameters,
DSAPrivateKey,
DSAPrivateNumbers,
DSAPublicKey,
DSAPublicNumbers,
)
from cryptography.hazmat.primitives.asymmetric.ec import (
EllipticCurve,
EllipticCurvePrivateKey,
EllipticCurvePrivateNumbers,
EllipticCurvePublicKey,
EllipticCurvePublicNumbers,
EllipticCurveSignatureAlgorithm,
)
from cryptography.hazmat.primitives.asymmetric.padding import AsymmetricPadding
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey, RSAPrivateNumbers, RSAPublicKey, RSAPublicNumbers
from cryptography.hazmat.primitives.ciphers import BlockCipherAlgorithm, CipherAlgorithm, CipherContext
from cryptography.hazmat.primitives.ciphers.modes import Mode
from cryptography.hazmat.primitives.hashes import HashAlgorithm, HashContext
from cryptography.x509 import (
Certificate,
CertificateBuilder,
CertificateRevocationList,
CertificateRevocationListBuilder,
CertificateSigningRequest,
CertificateSigningRequestBuilder,
Name,
RevokedCertificate,
RevokedCertificateBuilder,
)

class CipherBackend(metaclass=ABCMeta):
@abstractmethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not bother including the various backend API methods -- basically no user should ever be invoking them, so it's a lot of code for relatively little benefit (that's my opinion at least, maybe the approach of typeshed is different)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my opinion too, but as @srittau pointed out in #3307 (comment) these methods are part of the cryptography documentation.. so public API?

def cipher_supported(self, cipher: CipherAlgorithm, mode: Mode) -> bool: ...
@abstractmethod
def create_symmetric_encryption_ctx(self, cipher: CipherAlgorithm, mode: Mode) -> CipherContext: ...
@abstractmethod
def create_symmetric_decryption_ctx(self, cipher: CipherAlgorithm, mode: Mode) -> CipherContext: ...

class CMACBackend(metaclass=ABCMeta):
@abstractmethod
def cmac_algorithm_supported(self, algorithm: BlockCipherAlgorithm) -> bool: ...
@abstractmethod
def create_cmac_ctx(self, algorithm: BlockCipherAlgorithm) -> Any: ...

class DERSerializationBackend(metaclass=ABCMeta):
@abstractmethod
def load_der_parameters(self, data: bytes) -> Any: ...
@abstractmethod
def load_der_private_key(self, data: bytes, password: Optional[bytes]) -> Any: ...
@abstractmethod
def load_der_public_key(self, data: bytes) -> Any: ...

class DHBackend(metaclass=ABCMeta):
@abstractmethod
def dh_parameters_supported(self, p: int, g: int, q: Optional[int]) -> bool: ...
@abstractmethod
def dh_x942_serialization_supported(self) -> bool: ...
@abstractmethod
def generate_dh_parameters(self, generator: int, key_size: int) -> DHParameters: ...
@abstractmethod
def generate_dh_private_key(self, parameters: DHParameters) -> DHPrivateKey: ...
@abstractmethod
def generate_dh_private_key_and_parameters(self, generator: int, key_size: int) -> DHPrivateKey: ...
@abstractmethod
def load_dh_parameter_numbers(self, numbers: DHParameterNumbers) -> DHParameters: ...
@abstractmethod
def load_dh_private_numbers(self, numbers: DHPrivateNumbers) -> DHPrivateKey: ...
@abstractmethod
def load_dh_public_numbers(self, numbers: DHPublicNumbers) -> DHPublicKey: ...

class DSABackend(metaclass=ABCMeta):
@abstractmethod
def dsa_hash_supported(self, algorithm: HashAlgorithm) -> bool: ...
@abstractmethod
def dsa_parameters_supported(self, p: int, q: int, g: int) -> bool: ...
@abstractmethod
def generate_dsa_parameters(self, key_size: int) -> DSAParameters: ...
@abstractmethod
def generate_dsa_private_key(self, parameters: DSAParameters) -> DSAPrivateKey: ...
@abstractmethod
def generate_dsa_private_key_and_parameters(self, key_size: int) -> DSAPrivateKey: ...
@abstractmethod
def load_dsa_parameter_numbers(self, numbers: DSAParameterNumbers) -> DSAParameters: ...
@abstractmethod
def load_dsa_private_numbers(self, numbers: DSAPrivateNumbers) -> DSAPrivateKey: ...
@abstractmethod
def load_dsa_public_numbers(self, numbers: DSAPublicNumbers) -> DSAPublicKey: ...

class EllipticCurveBackend(metaclass=ABCMeta):
@abstractmethod
def derive_elliptic_curve_private_key(self, private_value: int, curve: EllipticCurve) -> EllipticCurvePrivateKey: ...
@abstractmethod
def elliptic_curve_signature_algorithm_supported(
self, signature_algorithm: EllipticCurveSignatureAlgorithm, curve: EllipticCurve
) -> bool: ...
@abstractmethod
def elliptic_curve_supported(self, curve: EllipticCurve) -> bool: ...
@abstractmethod
def generate_elliptic_curve_private_key(self, curve: EllipticCurve) -> EllipticCurvePrivateKey: ...
@abstractmethod
def load_elliptic_curve_private_numbers(self, numbers: EllipticCurvePrivateNumbers) -> EllipticCurvePrivateKey: ...
@abstractmethod
def load_elliptic_curve_public_numbers(self, numbers: EllipticCurvePublicNumbers) -> EllipticCurvePublicKey: ...

class HMACBackend(metaclass=ABCMeta):
@abstractmethod
def create_hmac_ctx(self, key: bytes, algorithm: HashAlgorithm) -> HashContext: ...
@abstractmethod
def cmac_algorithm_supported(self, algorithm: HashAlgorithm) -> bool: ...

class HashBackend(metaclass=ABCMeta):
@abstractmethod
def create_hash_ctx(self, algorithm: HashAlgorithm) -> HashContext: ...
@abstractmethod
def hash_supported(self, algorithm: HashAlgorithm) -> bool: ...

class PBKDF2HMACBackend(metaclass=ABCMeta):
@abstractmethod
def derive_pbkdf2_hmac(
self, algorithm: HashAlgorithm, length: int, salt: bytes, iterations: int, key_material: bytes
) -> bytes: ...
@abstractmethod
def pbkdf2_hmac_supported(self, algorithm: HashAlgorithm) -> bool: ...

class PEMSerializationBackend(metaclass=ABCMeta):
@abstractmethod
def load_pem_parameters(self, data: bytes) -> Any: ...
@abstractmethod
def load_pem_private_key(self, data: bytes, password: Optional[bytes]) -> Any: ...
@abstractmethod
def load_pem_public_key(self, data: bytes) -> Any: ...

class RSABackend(metaclass=ABCMeta):
@abstractmethod
def generate_rsa_parameters_supported(self, public_exponent: int, key_size: int) -> bool: ...
@abstractmethod
def generate_rsa_private_key(self, public_exponent: int, key_size: int) -> RSAPrivateKey: ...
@abstractmethod
def load_rsa_public_numbers(self, numbers: RSAPublicNumbers) -> RSAPublicKey: ...
@abstractmethod
def load_rsa_private_numbers(self, numbers: RSAPrivateNumbers) -> RSAPrivateKey: ...
@abstractmethod
def rsa_padding_supported(self, padding: AsymmetricPadding) -> bool: ...

class ScryptBackend(metaclass=ABCMeta):
@abstractmethod
def derive_scrypt(self, key_material: bytes, salt: bytes, length: int, n: int, r: int, p: int) -> bytes: ...

class X509Backend(metaclass=ABCMeta):
@abstractmethod
def create_x509_certificate(
self,
builder: CertificateBuilder,
private_key: Union[DSAPrivateKey, EllipticCurvePrivateKey, RSAPrivateKey],
algorithm: HashAlgorithm,
) -> Certificate: ...
@abstractmethod
def create_x509_crl(
self,
builder: CertificateRevocationListBuilder,
private_key: Union[DSAPrivateKey, EllipticCurvePrivateKey, RSAPrivateKey],
algorithm: HashAlgorithm,
) -> CertificateRevocationList: ...
@abstractmethod
def create_x509_csr(
self,
builder: CertificateSigningRequestBuilder,
private_key: Union[DSAPrivateKey, EllipticCurvePrivateKey, RSAPrivateKey],
algorithm: HashAlgorithm,
) -> CertificateSigningRequest: ...
@abstractmethod
def create_x509_revoked_certificate(self, builder: RevokedCertificateBuilder) -> RevokedCertificate: ...
@abstractmethod
def load_der_x509_certificate(self, data: bytes) -> Certificate: ...
@abstractmethod
def load_der_x509_csr(self, data: bytes) -> CertificateSigningRequest: ...
@abstractmethod
def load_pem_x509_certificate(self, data: bytes) -> Certificate: ...
@abstractmethod
def load_pem_x509_csr(self, data: bytes) -> CertificateSigningRequest: ...
@abstractmethod
def x509_name_bytes(self, name: Name) -> bytes: ...
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import Any

class Binding:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
ffi: Any
lib: Any
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def init_static_locks(self) -> None: ...
68 changes: 68 additions & 0 deletions third_party/2and3/cryptography/hazmat/primitives/asymmetric/dh.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from abc import ABCMeta, abstractmethod
from typing import Optional

from cryptography.hazmat.backends.interfaces import DHBackend
from cryptography.hazmat.primitives.serialization import (
Encoding,
KeySerializationEncryption,
ParameterFormat,
PrivateFormat,
PublicFormat,
)

class DHParameters(metaclass=ABCMeta):
@abstractmethod
def generate_private_key(self) -> DHPrivateKey: ...
@abstractmethod
def parameter_bytes(self, encoding: Encoding, format: ParameterFormat) -> bytes: ...
@abstractmethod
def parameter_numbers(self) -> DHParameterNumbers: ...

DHParametersWithSerialization = DHParameters

class DHParameterNumbers:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
p: int
g: int
q: int
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, p: int, g: int, q: Optional[int]) -> None: ...
def parameters(self, backend: DHBackend) -> DHParameters: ...

class DHPrivateKey(metaclass=ABCMeta):
key_size: int
@abstractmethod
def exchange(self, peer_public_key: DHPublicKey) -> bytes: ...
@abstractmethod
def parameters(self) -> DHParameters: ...
@abstractmethod
def public_key(self) -> DHPublicKey: ...

class DHPrivateKeyWithSerialization(DHPrivateKey):
@abstractmethod
def private_bytes(
self, encoding: Encoding, format: PrivateFormat, encryption_algorithm: KeySerializationEncryption
) -> bytes: ...
@abstractmethod
def private_numbers(self) -> DHPrivateNumbers: ...

class DHPrivateNumbers:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
public_numbers: DHPublicNumbers
x: int
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, x: int, public_numbers: DHPublicNumbers) -> None: ...
def private_key(self, backend: DHBackend) -> DHPrivateKey: ...

class DHPublicKey(metaclass=ABCMeta):
key_size: int
jlaine marked this conversation as resolved.
Show resolved Hide resolved
@abstractmethod
def parameters(self) -> DHParameters: ...
@abstractmethod
def public_bytes(self, encoding: Encoding, format: PublicFormat) -> bytes: ...
@abstractmethod
def public_numbers(self) -> DHPublicNumbers: ...

DHPublicKeyWithSerialization = DHPublicKey

class DHPublicNumbers:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
parameter_numbers: DHParameterNumbers
y: int
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, y: int, parameter_numbers: DHParameterNumbers) -> None: ...
def public_key(self, backend: DHBackend) -> DHPublicKey: ...
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
# Minimal stub expressing only the classes required by OpenSSL.crypto.
from abc import ABCMeta, abstractmethod

class DSAPrivateKey: ...
class DSAPublicKey: ...
from cryptography.hazmat.backends.interfaces import DSABackend
from cryptography.hazmat.primitives.asymmetric.padding import AsymmetricPadding
from cryptography.hazmat.primitives.hashes import HashAlgorithm
from cryptography.hazmat.primitives.serialization import Encoding, KeySerializationEncryption, PrivateFormat, PublicFormat

class DSAParameters(metaclass=ABCMeta):
@abstractmethod
def generate_private_key(self) -> DSAPrivateKey: ...

class DSAParametersWithNumbers(DSAParameters):
@abstractmethod
def parameter_numbers(self) -> DSAParameterNumbers: ...

class DSAParameterNumbers:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
p: int
q: int
g: int
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, p: int, q: int, g: int) -> None: ...
def parameters(self, backend: DSABackend) -> DSAParameters: ...

class DSAPrivateKey(metaclass=ABCMeta):
key_size: int
jlaine marked this conversation as resolved.
Show resolved Hide resolved
@abstractmethod
def parameters(self) -> DSAParameters: ...
@abstractmethod
def public_key(self) -> DSAPublicKey: ...
@abstractmethod
def sign(self, data: bytes, algorithm: HashAlgorithm) -> bytes: ...

class DSAPrivateKeyWithSerialization(DSAPrivateKey):
@abstractmethod
def private_bytes(
self, encoding: Encoding, format: PrivateFormat, encryption_algorithm: KeySerializationEncryption
) -> bytes: ...
@abstractmethod
def private_numbers(self) -> DSAPrivateNumbers: ...

class DSAPrivateNumbers:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
x: int
public_numbers: DSAPublicNumbers
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, x: int, public_numbers: DSAPublicNumbers) -> None: ...

class DSAPublicKey(metaclass=ABCMeta):
key_size: int
jlaine marked this conversation as resolved.
Show resolved Hide resolved
@abstractmethod
def public_bytes(self, encoding: Encoding, format: PublicFormat) -> bytes: ...
@abstractmethod
def public_numbers(self) -> DSAPublicNumbers: ...
@abstractmethod
def sign(self, data: bytes, padding: AsymmetricPadding, algorithm: HashAlgorithm) -> bytes: ...
@abstractmethod
def verify(self, signature: bytes, data: bytes, padding: AsymmetricPadding, algorithm: HashAlgorithm) -> None: ...

DSAPublicKeyWithSerialization = DSAPublicKey

class DSAPublicNumbers:
jlaine marked this conversation as resolved.
Show resolved Hide resolved
y: int
parameter_numbers: DSAParameterNumbers
jlaine marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, y: int, parameter_numbers: DSAParameterNumbers) -> None: ...

def generate_parameters(key_size: int, backend: DSABackend) -> DSAParameters: ...
def generate_private_key(key_size: int, backend: DSABackend) -> DSAPrivateKey: ...
Loading