• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
7import abc
8from enum import Enum
9
10import six
11
12from cryptography import utils
13from cryptography.hazmat.backends import _get_backend
14
15
16def load_pem_private_key(data, password, backend=None):
17    backend = _get_backend(backend)
18    return backend.load_pem_private_key(data, password)
19
20
21def load_pem_public_key(data, backend=None):
22    backend = _get_backend(backend)
23    return backend.load_pem_public_key(data)
24
25
26def load_pem_parameters(data, backend=None):
27    backend = _get_backend(backend)
28    return backend.load_pem_parameters(data)
29
30
31def load_der_private_key(data, password, backend=None):
32    backend = _get_backend(backend)
33    return backend.load_der_private_key(data, password)
34
35
36def load_der_public_key(data, backend=None):
37    backend = _get_backend(backend)
38    return backend.load_der_public_key(data)
39
40
41def load_der_parameters(data, backend=None):
42    backend = _get_backend(backend)
43    return backend.load_der_parameters(data)
44
45
46class Encoding(Enum):
47    PEM = "PEM"
48    DER = "DER"
49    OpenSSH = "OpenSSH"
50    Raw = "Raw"
51    X962 = "ANSI X9.62"
52    SMIME = "S/MIME"
53
54
55class PrivateFormat(Enum):
56    PKCS8 = "PKCS8"
57    TraditionalOpenSSL = "TraditionalOpenSSL"
58    Raw = "Raw"
59    OpenSSH = "OpenSSH"
60
61
62class PublicFormat(Enum):
63    SubjectPublicKeyInfo = "X.509 subjectPublicKeyInfo with PKCS#1"
64    PKCS1 = "Raw PKCS#1"
65    OpenSSH = "OpenSSH"
66    Raw = "Raw"
67    CompressedPoint = "X9.62 Compressed Point"
68    UncompressedPoint = "X9.62 Uncompressed Point"
69
70
71class ParameterFormat(Enum):
72    PKCS3 = "PKCS3"
73
74
75@six.add_metaclass(abc.ABCMeta)
76class KeySerializationEncryption(object):
77    pass
78
79
80@utils.register_interface(KeySerializationEncryption)
81class BestAvailableEncryption(object):
82    def __init__(self, password):
83        if not isinstance(password, bytes) or len(password) == 0:
84            raise ValueError("Password must be 1 or more bytes.")
85
86        self.password = password
87
88
89@utils.register_interface(KeySerializationEncryption)
90class NoEncryption(object):
91    pass
92