File: attribute_values.py

package info (click to toggle)
python-pykmip 0.10.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,796 kB
  • sloc: python: 102,456; makefile: 33; sh: 12
file content (294 lines) | stat: -rw-r--r-- 14,225 bytes parent folder | download | duplicates (4)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Copyright (c) 2014 The Johns Hopkins University/Applied Physics Laboratory
# 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. You may obtain
# a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.

from kmip.core import attributes
from kmip.core import enums
from kmip.core import primitives
from kmip.core import utils


class AttributeValueFactory(object):

    def create_attribute_value(self, name, value):
        # Switch on the name of the attribute
        if name is enums.AttributeType.UNIQUE_IDENTIFIER:
            return attributes.UniqueIdentifier(value)
        elif name is enums.AttributeType.NAME:
            return self._create_name(value)
        elif name is enums.AttributeType.OBJECT_TYPE:
            return attributes.ObjectType(value)
        elif name is enums.AttributeType.CRYPTOGRAPHIC_ALGORITHM:
            return attributes.CryptographicAlgorithm(value)
        elif name is enums.AttributeType.CRYPTOGRAPHIC_LENGTH:
            return self._create_cryptographic_length(value)
        elif name is enums.AttributeType.CRYPTOGRAPHIC_PARAMETERS:
            return self._create_cryptographic_parameters(value)
        elif name is enums.AttributeType.CRYPTOGRAPHIC_DOMAIN_PARAMETERS:
            raise NotImplementedError()
        elif name is enums.AttributeType.CERTIFICATE_TYPE:
            return primitives.Enumeration(
                enums.CertificateType,
                value=value,
                tag=enums.Tags.CERTIFICATE_TYPE
            )
        elif name is enums.AttributeType.CERTIFICATE_LENGTH:
            return primitives.Integer(value, enums.Tags.CERTIFICATE_LENGTH)
        elif name is enums.AttributeType.X_509_CERTIFICATE_IDENTIFIER:
            raise NotImplementedError()
        elif name is enums.AttributeType.X_509_CERTIFICATE_SUBJECT:
            raise NotImplementedError()
        elif name is enums.AttributeType.X_509_CERTIFICATE_ISSUER:
            raise NotImplementedError()
        elif name is enums.AttributeType.CERTIFICATE_IDENTIFIER:
            raise NotImplementedError()
        elif name is enums.AttributeType.CERTIFICATE_SUBJECT:
            raise NotImplementedError()
        elif name is enums.AttributeType.CERTIFICATE_ISSUER:
            raise NotImplementedError()
        elif name is enums.AttributeType.DIGITAL_SIGNATURE_ALGORITHM:
            raise NotImplementedError()
        elif name is enums.AttributeType.DIGEST:
            return attributes.Digest()
        elif name is enums.AttributeType.OPERATION_POLICY_NAME:
            return attributes.OperationPolicyName(value)
        elif name is enums.AttributeType.CRYPTOGRAPHIC_USAGE_MASK:
            return self._create_cryptographic_usage_mask(value)
        elif name is enums.AttributeType.LEASE_TIME:
            return primitives.Interval(value, enums.Tags.LEASE_TIME)
        elif name is enums.AttributeType.USAGE_LIMITS:
            raise NotImplementedError()
        elif name is enums.AttributeType.STATE:
            return attributes.State(value)
        elif name is enums.AttributeType.INITIAL_DATE:
            return primitives.DateTime(value, enums.Tags.INITIAL_DATE)
        elif name is enums.AttributeType.ACTIVATION_DATE:
            return primitives.DateTime(value, enums.Tags.ACTIVATION_DATE)
        elif name is enums.AttributeType.PROCESS_START_DATE:
            return primitives.DateTime(value, enums.Tags.PROCESS_START_DATE)
        elif name is enums.AttributeType.PROTECT_STOP_DATE:
            return primitives.DateTime(value, enums.Tags.PROTECT_STOP_DATE)
        elif name is enums.AttributeType.DEACTIVATION_DATE:
            return primitives.DateTime(value, enums.Tags.DEACTIVATION_DATE)
        elif name is enums.AttributeType.DESTROY_DATE:
            return primitives.DateTime(value, enums.Tags.DESTROY_DATE)
        elif name is enums.AttributeType.COMPROMISE_OCCURRENCE_DATE:
            return primitives.DateTime(
                value, enums.Tags.COMPROMISE_OCCURRENCE_DATE)
        elif name is enums.AttributeType.COMPROMISE_DATE:
            return primitives.DateTime(value, enums.Tags.COMPROMISE_DATE)
        elif name is enums.AttributeType.REVOCATION_REASON:
            raise NotImplementedError()
        elif name is enums.AttributeType.ARCHIVE_DATE:
            return primitives.DateTime(value, enums.Tags.ARCHIVE_DATE)
        elif name is enums.AttributeType.OBJECT_GROUP:
            return primitives.TextString(value, enums.Tags.OBJECT_GROUP)
        elif name is enums.AttributeType.FRESH:
            return primitives.Boolean(value, enums.Tags.FRESH)
        elif name is enums.AttributeType.LINK:
            raise NotImplementedError()
        elif name is enums.AttributeType.APPLICATION_SPECIFIC_INFORMATION:
            return self._create_application_specific_information(value)
        elif name is enums.AttributeType.CONTACT_INFORMATION:
            return self._create_contact_information(value)
        elif name is enums.AttributeType.LAST_CHANGE_DATE:
            return primitives.DateTime(value, enums.Tags.LAST_CHANGE_DATE)
        elif name is enums.AttributeType.SENSITIVE:
            return primitives.Boolean(value, enums.Tags.SENSITIVE)
        elif name is enums.AttributeType.CUSTOM_ATTRIBUTE:
            return attributes.CustomAttribute(value)
        else:
            if not isinstance(name, str):
                raise ValueError('Unrecognized attribute type: '
                                 '{0}'.format(name))
            elif name.startswith('x-'):
                # Custom attribute indicated
                return attributes.CustomAttribute(value)

    def create_attribute_value_by_enum(self, enum, value):
        # Switch on the name of the attribute
        if enum is enums.Tags.UNIQUE_IDENTIFIER:
            return attributes.UniqueIdentifier(value)
        elif enum is enums.Tags.NAME:
            return self._create_name(value)
        elif enum is enums.Tags.OBJECT_TYPE:
            return attributes.ObjectType(value)
        elif enum is enums.Tags.CRYPTOGRAPHIC_ALGORITHM:
            return attributes.CryptographicAlgorithm(value)
        elif enum is enums.Tags.CRYPTOGRAPHIC_LENGTH:
            return self._create_cryptographic_length(value)
        elif enum is enums.Tags.CRYPTOGRAPHIC_PARAMETERS:
            return self._create_cryptographic_parameters(value)
        elif enum is enums.Tags.CRYPTOGRAPHIC_DOMAIN_PARAMETERS:
            raise NotImplementedError()
        elif enum is enums.Tags.CERTIFICATE_TYPE:
            raise NotImplementedError()
        elif enum is enums.Tags.CERTIFICATE_LENGTH:
            return primitives.Integer(value, enums.Tags.CERTIFICATE_LENGTH)
        elif enum is enums.Tags.X_509_CERTIFICATE_IDENTIFIER:
            raise NotImplementedError()
        elif enum is enums.Tags.X_509_CERTIFICATE_SUBJECT:
            raise NotImplementedError()
        elif enum is enums.Tags.X_509_CERTIFICATE_ISSUER:
            raise NotImplementedError()
        elif enum is enums.Tags.CERTIFICATE_IDENTIFIER:
            raise NotImplementedError()
        elif enum is enums.Tags.CERTIFICATE_SUBJECT:
            raise NotImplementedError()
        elif enum is enums.Tags.CERTIFICATE_ISSUER:
            raise NotImplementedError()
        elif enum is enums.Tags.DIGITAL_SIGNATURE_ALGORITHM:
            raise NotImplementedError()
        elif enum is enums.Tags.DIGEST:
            return attributes.Digest()
        elif enum is enums.Tags.OPERATION_POLICY_NAME:
            return attributes.OperationPolicyName(value)
        elif enum is enums.Tags.CRYPTOGRAPHIC_USAGE_MASK:
            return self._create_cryptographic_usage_mask(value)
        elif enum is enums.Tags.LEASE_TIME:
            return primitives.Interval(value, enums.Tags.LEASE_TIME)
        elif enum is enums.Tags.USAGE_LIMITS:
            raise NotImplementedError()
        elif enum is enums.Tags.STATE:
            return attributes.State(value)
        elif enum is enums.Tags.INITIAL_DATE:
            return primitives.DateTime(value, enums.Tags.INITIAL_DATE)
        elif enum is enums.Tags.ACTIVATION_DATE:
            return primitives.DateTime(value, enums.Tags.ACTIVATION_DATE)
        elif enum is enums.Tags.PROCESS_START_DATE:
            return primitives.DateTime(value, enums.Tags.PROCESS_START_DATE)
        elif enum is enums.Tags.PROTECT_STOP_DATE:
            return primitives.DateTime(value, enums.Tags.PROTECT_STOP_DATE)
        elif enum is enums.Tags.DEACTIVATION_DATE:
            return primitives.DateTime(value, enums.Tags.DEACTIVATION_DATE)
        elif enum is enums.Tags.DESTROY_DATE:
            return primitives.DateTime(value, enums.Tags.DESTROY_DATE)
        elif enum is enums.Tags.COMPROMISE_OCCURRENCE_DATE:
            return primitives.DateTime(
                value, enums.Tags.COMPROMISE_OCCURRENCE_DATE)
        elif enum is enums.Tags.COMPROMISE_DATE:
            return primitives.DateTime(value, enums.Tags.COMPROMISE_DATE)
        elif enum is enums.Tags.REVOCATION_REASON:
            raise NotImplementedError()
        elif enum is enums.Tags.ARCHIVE_DATE:
            return primitives.DateTime(value, enums.Tags.ARCHIVE_DATE)
        elif enum is enums.Tags.OBJECT_GROUP:
            return primitives.TextString(value, enums.Tags.OBJECT_GROUP)
            return self._create_object_group(value)
        elif enum is enums.Tags.FRESH:
            return primitives.Boolean(value, enums.Tags.FRESH)
        elif enum is enums.Tags.LINK:
            raise NotImplementedError()
        elif enum is enums.Tags.APPLICATION_SPECIFIC_INFORMATION:
            return self._create_application_specific_information(value)
        elif enum is enums.Tags.CONTACT_INFORMATION:
            return self._create_contact_information(value)
        elif enum is enums.Tags.LAST_CHANGE_DATE:
            return primitives.DateTime(value, enums.Tags.LAST_CHANGE_DATE)
        elif enum is enums.Tags.SENSITIVE:
            return primitives.Boolean(value, enums.Tags.SENSITIVE)
        elif enum is enums.Tags.CUSTOM_ATTRIBUTE:
            return attributes.CustomAttribute(value)
        else:
            raise ValueError("Unrecognized attribute type: {}".format(enum))

    def _create_name(self, name):
        if name is not None:
            if isinstance(name, attributes.Name):
                return attributes.Name.create(name.name_value, name.name_type)

            elif isinstance(name, str):
                return attributes.Name.create(
                            name,
                            enums.NameType.UNINTERPRETED_TEXT_STRING
                        )
            else:
                raise ValueError('Unrecognized attribute type: '
                                 '{0}'.format(name))
        else:
            return attributes.Name()

    def _create_cryptographic_length(self, length):
        if length is not None and not isinstance(length, int):
            msg = utils.build_er_error(attributes.CryptographicLength,
                                       'constructor argument type', int,
                                       type(length))
            raise TypeError(msg)

        return attributes.CryptographicLength(length)

    def _create_cryptographic_parameters(self, params):
        if params is None:
            params = {}

        if isinstance(params, dict):
            return attributes.CryptographicParameters(
                block_cipher_mode=params.get('block_cipher_mode', None),
                padding_method=params.get('padding_method', None),
                hashing_algorithm=params.get('hashing_algorithm', None),
                key_role_type=params.get('key_role_type', None),
                digital_signature_algorithm=params.get(
                    'digital_signature_algorithm',
                    None
                ),
                cryptographic_algorithm=params.get(
                    'cryptographic_algorithm',
                    None
                ),
                random_iv=params.get('random_iv', None),
                iv_length=params.get('iv_length', None),
                tag_length=params.get('tag_length', None),
                fixed_field_length=params.get('fixed_field_length', None),
                invocation_field_length=params.get(
                    'invocation_field_length',
                    None
                ),
                counter_length=params.get('counter_length', None),
                initial_counter_value=params.get(
                    'initial_counter_value',
                    None
                )
            )
        else:
            raise TypeError("cryptographic parameters must be a dict")

    def _create_cryptographic_usage_mask(self, flags):
        mask = None
        if flags is not None:
            mask = 0
            for flag in flags:
                mask |= flag.value

        return attributes.CryptographicUsageMask(mask)

    def _create_application_specific_information(self, info):
        if info:
            return attributes.ApplicationSpecificInformation(
                application_namespace=info.get("application_namespace"),
                application_data=info.get("application_data")
            )
        else:
            return attributes.ApplicationSpecificInformation()

    def _create_contact_information(self, info):
        if info is None:
            return attributes.ContactInformation()
        else:
            if not isinstance(info, str):
                msg = utils.build_er_error(attributes.ContactInformation,
                                           'constructor argument type', str,
                                           type(info))
                raise TypeError(msg)

            return attributes.ContactInformation(info)