22
22
23
23
from awses_test_vectors .internal .aws_kms import arn_from_key_id
24
24
from awses_test_vectors .internal .defaults import ENCODING
25
- from awses_test_vectors .internal .util import (
26
- dictionary_validator ,
27
- iterable_validator ,
28
- membership_validator ,
29
- validate_manifest_type ,
30
- )
25
+ from awses_test_vectors .internal .util import dictionary_validator , membership_validator , validate_manifest_type
31
26
32
27
try : # Python 3.5.0 and 3.5.1 have incompatible typing modules
33
28
from typing import cast , Dict , Iterable , Optional # noqa pylint: disable=unused-import
42
37
# We only actually need these imports when running the mypy checks
43
38
pass
44
39
45
- SUPPORTED_VERSIONS = (1 ,)
40
+ SUPPORTED_VERSIONS = (3 ,)
46
41
KEY_TYPES = ("symmetric" , "private" , "public" )
47
42
KEY_ENCODINGS = ("base64" , "pem" )
48
43
KEY_ALGORITHMS = ("aes" , "rsa" )
@@ -60,14 +55,16 @@ class KeySpec(object):
60
55
61
56
encrypt = attr .ib (validator = attr .validators .instance_of (bool ))
62
57
decrypt = attr .ib (validator = attr .validators .instance_of (bool ))
58
+ key_id = attr .ib (validator = attr .validators .instance_of (six .string_types ))
63
59
64
- def __init__ (self , encrypt , decrypt ): # noqa=D107
65
- # type: (bool, bool) -> None
60
+ def __init__ (self , encrypt , decrypt , key_id ): # noqa=D107
61
+ # type: (bool, bool, str ) -> None
66
62
# Workaround pending resolution of attrs/mypy interaction.
67
63
# https://github.com/python/mypy/issues/2088
68
64
# https://github.com/python-attrs/attrs/issues/215
69
65
self .encrypt = encrypt
70
66
self .decrypt = decrypt
67
+ self .key_id = key_id
71
68
attr .validate (self )
72
69
73
70
@@ -84,16 +81,14 @@ class AwsKmsKeySpec(KeySpec):
84
81
# pylint: disable=too-few-public-methods
85
82
86
83
type_name = attr .ib (validator = membership_validator (("aws-kms" ,)))
87
- key_id = attr .ib (validator = attr .validators .instance_of (six .string_types ))
88
84
89
85
def __init__ (self , encrypt , decrypt , type_name , key_id ): # noqa=D107
90
86
# type: (bool, bool, str, str) -> None
91
87
# Workaround pending resolution of attrs/mypy interaction.
92
88
# https://github.com/python/mypy/issues/2088
93
89
# https://github.com/python-attrs/attrs/issues/215
94
90
self .type_name = type_name
95
- self .key_id = key_id
96
- super (AwsKmsKeySpec , self ).__init__ (encrypt , decrypt )
91
+ super (AwsKmsKeySpec , self ).__init__ (encrypt , decrypt , key_id )
97
92
98
93
@property
99
94
def manifest_spec (self ):
@@ -117,35 +112,32 @@ class ManualKeySpec(KeySpec):
117
112
118
113
Allowed values described in AWS Crypto Tools Test Vector Framework feature #0002 Keys Manifest.
119
114
115
+ :param str key_id: Master key ID
120
116
:param bool encrypt: Key can be used to encrypt
121
117
:param bool decrypt: Key can be used to decrypt
122
118
:param str algorithm: Algorithm to use with key
123
119
:param str type_name: Key type
124
120
:param int bits: Key length in bits
125
121
:param str encoding: Encoding used to encode key material
126
- :param material: Raw material encoded, then split into lines separated by ``line_separator``
127
- :type material: list of str
128
- :param str line_separator: Character with which to separate members of ``material``
129
- before decoding (optional: default is empty string)
122
+ :param str material: Raw material encoded
130
123
"""
131
124
132
125
algorithm = attr .ib (validator = membership_validator (KEY_ALGORITHMS ))
133
126
type_name = attr .ib (validator = membership_validator (KEY_TYPES ))
134
127
bits = attr .ib (validator = attr .validators .instance_of (int ))
135
128
encoding = attr .ib (validator = membership_validator (KEY_ENCODINGS ))
136
- material = attr .ib (validator = iterable_validator (list , six .string_types ))
137
- line_separator = attr .ib (default = "" , validator = attr .validators .instance_of (six .string_types ))
129
+ material = attr .ib (validator = attr .validators .instance_of (six .string_types ))
138
130
139
131
def __init__ (
140
132
self ,
133
+ key_id , # type: str
141
134
encrypt , # type: bool
142
135
decrypt , # type: bool
143
136
algorithm , # type: str
144
137
type_name , # type: str
145
138
bits , # type: int
146
139
encoding , # type: str
147
140
material , # type: Iterable[str]
148
- line_separator = "" , # type: Optional[str]
149
141
): # noqa=D107
150
142
# type: (...) -> None
151
143
# Workaround pending resolution of attrs/mypy interaction.
@@ -156,8 +148,7 @@ def __init__(
156
148
self .bits = bits
157
149
self .encoding = encoding
158
150
self .material = material
159
- self .line_separator = line_separator
160
- super (ManualKeySpec , self ).__init__ (encrypt , decrypt )
151
+ super (ManualKeySpec , self ).__init__ (encrypt , decrypt , key_id )
161
152
162
153
@property
163
154
def raw_material (self ):
@@ -167,7 +158,7 @@ def raw_material(self):
167
158
:return: Binary key material
168
159
:rtype: bytes
169
160
"""
170
- raw_material = self .line_separator . join ( self . material ) .encode (ENCODING )
161
+ raw_material = self .material .encode (ENCODING )
171
162
if self .encoding == "base64" :
172
163
return base64 .b64decode (raw_material )
173
164
@@ -188,8 +179,8 @@ def manifest_spec(self):
188
179
"type" : self .type_name ,
189
180
"bits" : self .bits ,
190
181
"encoding" : self .encoding ,
191
- "line-separator" : self .line_separator ,
192
182
"material" : self .material ,
183
+ "key-id" : self .key_id ,
193
184
}
194
185
195
186
@@ -201,6 +192,7 @@ def key_from_manifest_spec(key_spec):
201
192
:return: Loaded key
202
193
:rtype: KeySpec
203
194
"""
195
+ key_id = key_spec ["key-id" ] # type: str
204
196
decrypt = key_spec ["decrypt" ] # type: bool
205
197
encrypt = key_spec ["encrypt" ] # type: bool
206
198
type_name = key_spec ["type" ] # type: str
@@ -211,16 +203,15 @@ def key_from_manifest_spec(key_spec):
211
203
algorithm = key_spec ["algorithm" ] # type: str
212
204
bits = key_spec ["bits" ] # type: int
213
205
encoding = key_spec ["encoding" ] # type: str
214
- line_separator = key_spec .get ("line-separator" , "" ) # type: str
215
- material = key_spec ["material" ] # type: Iterable[str]
206
+ material = key_spec ["material" ] # type: str
216
207
return ManualKeySpec (
208
+ key_id = key_id ,
217
209
encrypt = encrypt ,
218
210
decrypt = decrypt ,
219
211
type_name = type_name ,
220
212
algorithm = algorithm ,
221
213
bits = bits ,
222
214
encoding = encoding ,
223
- line_separator = line_separator ,
224
215
material = material ,
225
216
)
226
217
@@ -242,7 +233,7 @@ class KeysManifest(object):
242
233
@classmethod
243
234
def from_manifest_spec (cls , raw_manifest ):
244
235
# type: (KEYS_MANIFEST) -> KeysManifest
245
- """"""
236
+ """Load from a JSON keys manifest. """
246
237
manifest_version = raw_manifest ["manifest" ] # type: MANIFEST_VERSION
247
238
validate_manifest_type (
248
239
type_name = cls .type_name , manifest_version = manifest_version , supported_versions = SUPPORTED_VERSIONS
0 commit comments