-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy path__init__.py
215 lines (167 loc) · 8.44 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Primitive structures for use when interacting with crypto material managers.
.. versionadded:: 1.3.0
"""
import attr
import six
from attr.validators import deep_iterable, deep_mapping, instance_of, optional
from ..identifiers import Algorithm, KeyringTraceFlag
from ..internal.utils.streams import ROStream
from ..structures import DataKey, EncryptedDataKey, KeyringTrace
@attr.s(hash=False)
class EncryptionMaterialsRequest(object):
"""Request object to provide to a crypto material manager's `get_encryption_materials` method.
.. versionadded:: 1.3.0
.. warning::
If plaintext_rostream seek position is modified, it must be returned before leaving method.
:param dict encryption_context: Encryption context passed to underlying master key provider and master keys
:param int frame_length: Frame length to be used while encrypting stream
:param plaintext_rostream: Source plaintext read-only stream (optional)
:type plaintext_rostream: aws_encryption_sdk.internal.utils.streams.ROStream
:param algorithm: Algorithm passed to underlying master key provider and master keys (optional)
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
:param int plaintext_length: Length of source plaintext (optional)
"""
encryption_context = attr.ib(validator=attr.validators.instance_of(dict))
frame_length = attr.ib(validator=attr.validators.instance_of(six.integer_types))
plaintext_rostream = attr.ib(
default=None, validator=attr.validators.optional(attr.validators.instance_of(ROStream))
)
algorithm = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(Algorithm)))
plaintext_length = attr.ib(
default=None, validator=attr.validators.optional(attr.validators.instance_of(six.integer_types))
)
@attr.s
class CryptographicMaterials(object):
"""Cryptographic materials core.
.. versionadded:: 1.5.0
:param Algorithm algorithm: Algorithm to use for encrypting message
:param dict encryption_context: Encryption context tied to `encrypted_data_keys`
:param DataKey data_encryption_key: Plaintext data key to use for encrypting message
:param encrypted_data_keys: List of encrypted data keys
:type encrypted_data_keys: list of :class:`EncryptedDataKey`
:param keyring_trace: Any KeyRing trace entries
:type keyring_trace: list of :class:`KeyringTrace`
"""
algorithm = attr.ib(validator=optional(instance_of(Algorithm)))
encryption_context = attr.ib(
validator=optional(
deep_mapping(key_validator=instance_of(six.string_types), value_validator=instance_of(six.string_types))
)
)
data_encryption_key = attr.ib(default=None, validator=optional(instance_of(DataKey)))
encrypted_data_keys = attr.ib(
default=attr.Factory(list), validator=optional(deep_iterable(member_validator=instance_of(EncryptedDataKey)))
)
keyring_trace = attr.ib(
default=attr.Factory(list), validator=optional(deep_iterable(member_validator=instance_of(KeyringTrace)))
)
@attr.s(hash=False, init=False)
class EncryptionMaterials(CryptographicMaterials):
"""Encryption materials returned by a crypto material manager's `get_encryption_materials` method.
.. versionadded:: 1.3.0
.. versionadded:: 1.5.0
The **keyring_trace** parameter.
.. versionadded:: 1.5.0
Most parameters are now optional.
:param Algorithm algorithm: Algorithm to use for encrypting message
:param DataKey data_encryption_key: Plaintext data key to use for encrypting message (optional)
:param encrypted_data_keys: List of encrypted data keys (optional)
:type encrypted_data_keys: list of :class:`EncryptedDataKey`
:param dict encryption_context: Encryption context tied to `encrypted_data_keys`
:param bytes signing_key: Encoded signing key (optional)
:param keyring_trace: Any KeyRing trace entries (optional)
:type keyring_trace: list of :class:`KeyringTrace`
"""
signing_key = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(bytes)))
def __init__(
self,
algorithm=None,
data_encryption_key=None,
encrypted_data_keys=None,
encryption_context=None,
signing_key=None,
**kwargs
):
if algorithm is None:
raise TypeError("algorithm must not be None")
if encryption_context is None:
raise TypeError("encryption_context must not be None")
super(EncryptionMaterials, self).__init__(
algorithm=algorithm,
encryption_context=encryption_context,
data_encryption_key=data_encryption_key,
encrypted_data_keys=encrypted_data_keys,
**kwargs
)
self.signing_key = signing_key
attr.validate(self)
@attr.s(hash=False)
class DecryptionMaterialsRequest(object):
"""Request object to provide to a crypto material manager's `decrypt_materials` method.
.. versionadded:: 1.3.0
:param algorithm: Algorithm to provide to master keys for underlying decrypt requests
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
:param encrypted_data_keys: Set of encrypted data keys
:type encrypted_data_keys: set of `aws_encryption_sdk.structures.EncryptedDataKey`
:param dict encryption_context: Encryption context to provide to master keys for underlying decrypt requests
"""
algorithm = attr.ib(validator=attr.validators.instance_of(Algorithm))
encrypted_data_keys = attr.ib(validator=attr.validators.instance_of(set))
encryption_context = attr.ib(validator=attr.validators.instance_of(dict))
_DEFAULT_SENTINEL = object()
@attr.s(hash=False, init=False)
class DecryptionMaterials(CryptographicMaterials):
"""Decryption materials returned by a crypto material manager's `decrypt_materials` method.
.. versionadded:: 1.3.0
.. versionadded:: 1.5.0
The **algorithm**, **data_encryption_key**, **encrypted_data_keys**,
**encryption_context**, and **keyring_trace** parameters.
.. versionadded:: 1.5.0
All parameters are now optional.
:param Algorithm algorithm: Algorithm to use for encrypting message (optional)
:param DataKey data_encryption_key: Plaintext data key to use for encrypting message (optional)
:param encrypted_data_keys: List of encrypted data keys (optional)
:type encrypted_data_keys: list of :class:`EncryptedDataKey`
:param dict encryption_context: Encryption context tied to `encrypted_data_keys` (optional)
:param bytes verification_key: Raw signature verification key (optional)
:param keyring_trace: Any KeyRing trace entries (optional)
:type keyring_trace: list of :class:`KeyringTrace`
"""
verification_key = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(bytes)))
def __init__(self, data_key=_DEFAULT_SENTINEL, verification_key=None, **kwargs):
if any(
(
data_key is _DEFAULT_SENTINEL and "data_encryption_key" not in kwargs,
data_key is not _DEFAULT_SENTINEL and "data_encryption_key" in kwargs,
)
):
raise TypeError("Exactly one of data_key or data_encryption_key must be set")
if data_key is not _DEFAULT_SENTINEL and "data_encryption_key" not in kwargs:
kwargs["data_encryption_key"] = data_key
for legacy_missing in ("algorithm", "encryption_context"):
if legacy_missing not in kwargs:
kwargs[legacy_missing] = None
super(DecryptionMaterials, self).__init__(**kwargs)
self.verification_key = verification_key
attr.validate(self)
@property
def data_key(self):
"""Backwards-compatible shim."""
return self.data_encryption_key
@data_key.setter
def data_key(self, value):
# type: (DataKey) -> None
"""Backwards-compatible shim."""
self.data_encryption_key = value