File: mac.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 (220 lines) | stat: -rw-r--r-- 7,355 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
# Copyright (c) 2017 Pure Storage, Inc. 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 exceptions
from kmip.core.enums import Tags
from kmip.core.messages.payloads import base
from kmip.core.objects import Data, MACData
from kmip.core.utils import BytearrayStream


# 4.33
class MACRequestPayload(base.RequestPayload):

    def __init__(self,
                 unique_identifier=None,
                 cryptographic_parameters=None,
                 data=None):

        super(MACRequestPayload, self).__init__()

        self._unique_identifier = None
        self._cryptographic_parameters = None
        self._data = None

        self.unique_identifier = unique_identifier
        self.cryptographic_parameters = cryptographic_parameters
        self.data = data

    @property
    def unique_identifier(self):
        return self._unique_identifier

    @unique_identifier.setter
    def unique_identifier(self, value):
        if value is None:
            self._unique_identifier = None
        elif isinstance(value, attributes.UniqueIdentifier):
            self._unique_identifier = value
        else:
            raise TypeError("unique identifier must be UniqueIdentifier type")

    @property
    def cryptographic_parameters(self):
        return self._cryptographic_parameters

    @cryptographic_parameters.setter
    def cryptographic_parameters(self, value):
        if value is None:
            self._cryptographic_parameters = None
        elif isinstance(value, attributes.CryptographicParameters):
            self._cryptographic_parameters = value
        else:
            raise TypeError("cryptographic parameters must "
                            "be CryptographicParameters type")

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        if value is None:
            self._data = None
        elif isinstance(value, Data):
            self._data = value
        else:
            raise TypeError("data must be Data type")

    def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        super(MACRequestPayload, self).read(
            istream,
            kmip_version=kmip_version
        )
        tstream = BytearrayStream(istream.read(self.length))

        if self.is_tag_next(Tags.UNIQUE_IDENTIFIER, tstream):
            self.unique_identifier = attributes.UniqueIdentifier()
            self.unique_identifier.read(tstream, kmip_version=kmip_version)

        if self.is_tag_next(Tags.CRYPTOGRAPHIC_PARAMETERS, tstream):
            self.cryptographic_parameters = \
                attributes.CryptographicParameters()
            self.cryptographic_parameters.read(
                tstream,
                kmip_version=kmip_version
            )

        if self.is_tag_next(Tags.DATA, tstream):
            self.data = Data()
            self.data.read(tstream, kmip_version=kmip_version)
        else:
            raise exceptions.InvalidKmipEncoding(
                "expected mac request data not found"
            )

        self.is_oversized(tstream)

    def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        tstream = BytearrayStream()

        if self._unique_identifier is not None:
            self._unique_identifier.write(tstream, kmip_version=kmip_version)
        if self._cryptographic_parameters is not None:
            self._cryptographic_parameters.write(
                tstream,
                kmip_version=kmip_version
            )
        if self._data is not None:
            self.data.write(tstream, kmip_version=kmip_version)
        else:
            raise exceptions.InvalidField(
                "The mac request data is required"
            )

        self.length = tstream.length()
        super(MACRequestPayload, self).write(
            ostream,
            kmip_version=kmip_version
        )
        ostream.write(tstream.buffer)


class MACResponsePayload(base.ResponsePayload):

    def __init__(self,
                 unique_identifier=None,
                 mac_data=None):
        super(MACResponsePayload, self).__init__()

        self._unique_identifier = None
        self._mac_data = None

        self.unique_identifier = unique_identifier
        self.mac_data = mac_data

    @property
    def unique_identifier(self):
        return self._unique_identifier

    @unique_identifier.setter
    def unique_identifier(self, value):
        if value is None:
            self._unique_identifier = None
        elif isinstance(value, attributes.UniqueIdentifier):
            self._unique_identifier = value
        else:
            raise TypeError("unique identifier must be UniqueIdentifier type")

    @property
    def mac_data(self):
        return self._mac_data

    @mac_data.setter
    def mac_data(self, value):
        if value is None:
            self._mac_data = None
        elif isinstance(value, MACData):
            self._mac_data = value
        else:
            raise TypeError("mac_data must be MACData type")

    def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        super(MACResponsePayload, self).read(
            istream,
            kmip_version=kmip_version
        )
        tstream = BytearrayStream(istream.read(self.length))

        if self.is_tag_next(Tags.UNIQUE_IDENTIFIER, tstream):
            self._unique_identifier = attributes.UniqueIdentifier()
            self._unique_identifier.read(tstream, kmip_version=kmip_version)
        else:
            raise exceptions.InvalidKmipEncoding(
                "expected mac response unique identifier not found"
            )

        if self.is_tag_next(Tags.MAC_DATA, tstream):
            self._mac_data = MACData()
            self._mac_data.read(tstream, kmip_version=kmip_version)
        else:
            raise exceptions.InvalidKmipEncoding(
                "expected mac response mac data not found"
            )

        self.is_oversized(tstream)

    def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        tstream = BytearrayStream()

        if self._unique_identifier is not None:
            self._unique_identifier.write(tstream, kmip_version=kmip_version)
        else:
            raise exceptions.InvalidField(
                "The mac response unique identifier is required"
            )
        if self._mac_data is not None:
            self._mac_data.write(tstream, kmip_version=kmip_version)
        else:
            raise exceptions.InvalidField(
                "The mac response mac data is required"
            )
        self.length = tstream.length()
        super(MACResponsePayload, self).write(
            ostream,
            kmip_version=kmip_version
        )
        ostream.write(tstream.buffer)