File: create_key_pair.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 (857 lines) | stat: -rw-r--r-- 34,773 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# 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.

import six

from kmip.core import enums
from kmip.core import exceptions
from kmip.core import objects
from kmip.core import primitives
from kmip.core import utils
from kmip.core.messages.payloads import base


class CreateKeyPairRequestPayload(base.RequestPayload):
    """
    A request payload for the CreateKeyPair operation.

    Attributes:
        common_template_attribute: A group of attributes to set on the new
            public and private keys.
        private_key_template_attribute: A group of attributes to set on the new
            private key.
        public_key_template_attribute: A group of attributes to set on the new
            public key.
        common_protection_storage_masks: A ProtectionStorageMasks structure
            containing the storage masks permissible for both new public and
            private keys. Added in KMIP 2.0.
        private_protection_storage_masks: A ProtectionStorageMasks structure
            containing the storage masks permissible for the new private key.
            Added in KMIP 2.0.
        public_protection_storage_masks: A ProtectionStorageMasks structure
            containing the storage masks permissible for the new public key.
            Added in KMIP 2.0.
    """

    def __init__(self,
                 common_template_attribute=None,
                 private_key_template_attribute=None,
                 public_key_template_attribute=None,
                 common_protection_storage_masks=None,
                 private_protection_storage_masks=None,
                 public_protection_storage_masks=None):
        """
        Construct a CreateKeyPair request payload structure.

        Args:
            common_template_attribute (TemplateAttribute): A TemplateAttribute
                structure with the CommonTemplateAttribute tag containing a set
                of attributes to set on the new public and private keys.
                Optional, defaults to None.
            private_key_template_attribute (TemplateAttribute): A
                TemplateAttribute structure with the
                PrivateKeyTemplateAttribute tag containing a set of attributes
                to set on the new private key. Optional, defaults to None.
            public_key_template_attribute (TemplateAttribute): A
                TemplateAttribute structure with the PublicKeyTemplateAttribute
                tag containing a set of attributes to set on the new public
                key. Optional, defaults to None.
            common_protection_storage_masks (structure): A
                ProtectionStorageMasks structure containing the storage masks
                permissible for both new public and private keys. Added in KMIP
                2.0. Optional, defaults to None.
            private_protection_storage_masks (structure): A
                ProtectionStorageMasks structure containing the storage masks
                permissible for the new private key. Added in KMIP 2.0.
                Optional, defaults to None.
            public_protection_storage_masks (structure): A
                ProtectionStorageMasks structure containing the storage masks
                permissible for the new public key. Added in KMIP 2.0.
                Optional, defaults to None.
        """
        super(CreateKeyPairRequestPayload, self).__init__()

        self._common_template_attribute = None
        self._private_key_template_attribute = None
        self._public_key_template_attribute = None
        self._common_protection_storage_masks = None
        self._private_protection_storage_masks = None
        self._public_protection_storage_masks = None

        self.common_template_attribute = common_template_attribute
        self.private_key_template_attribute = private_key_template_attribute
        self.public_key_template_attribute = public_key_template_attribute
        self.common_protection_storage_masks = common_protection_storage_masks
        self.private_protection_storage_masks = \
            private_protection_storage_masks
        self.public_protection_storage_masks = public_protection_storage_masks

    @property
    def common_template_attribute(self):
        return self._common_template_attribute

    @common_template_attribute.setter
    def common_template_attribute(self, value):
        if value is None:
            self._common_template_attribute = None
        elif isinstance(value, objects.TemplateAttribute):
            if value.tag == enums.Tags.COMMON_TEMPLATE_ATTRIBUTE:
                self._common_template_attribute = value
            else:
                raise TypeError(
                    "Common template attribute must be a TemplateAttribute "
                    "structure with a CommonTemplateAttribute tag."
                )
        else:
            raise TypeError(
                "Common template attribute must be a TemplateAttribute "
                "structure."
            )

    @property
    def private_key_template_attribute(self):
        return self._private_key_template_attribute

    @private_key_template_attribute.setter
    def private_key_template_attribute(self, value):
        if value is None:
            self._private_key_template_attribute = None
        elif isinstance(value, objects.TemplateAttribute):
            if value.tag == enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE:
                self._private_key_template_attribute = value
            else:
                raise TypeError(
                    "Private key template attribute must be a "
                    "TemplateAttribute structure with a "
                    "PrivateKeyTemplateAttribute tag."
                )
        else:
            raise TypeError(
                "Private key template attribute must be a TemplateAttribute "
                "structure."
            )

    @property
    def public_key_template_attribute(self):
        return self._public_key_template_attribute

    @public_key_template_attribute.setter
    def public_key_template_attribute(self, value):
        if value is None:
            self._public_key_template_attribute = None
        elif isinstance(value, objects.TemplateAttribute):
            if value.tag == enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE:
                self._public_key_template_attribute = value
            else:
                raise TypeError(
                    "Public key template attribute must be a "
                    "TemplateAttribute structure with a "
                    "PublicKeyTemplateAttribute tag."
                )
        else:
            raise TypeError(
                "Public key template attribute must be a TemplateAttribute "
                "structure."
            )

    @property
    def common_protection_storage_masks(self):
        return self._common_protection_storage_masks

    @common_protection_storage_masks.setter
    def common_protection_storage_masks(self, value):
        if value is None:
            self._common_protection_storage_masks = None
        elif isinstance(value, objects.ProtectionStorageMasks):
            if value.tag == enums.Tags.COMMON_PROTECTION_STORAGE_MASKS:
                self._common_protection_storage_masks = value
            else:
                raise TypeError(
                    "The common protection storage masks must be a "
                    "ProtectionStorageMasks structure with a "
                    "CommonProtectionStorageMasks tag."
                )
        else:
            raise TypeError(
                "The common protection storage masks must be a "
                "ProtectionStorageMasks structure."
            )

    @property
    def private_protection_storage_masks(self):
        return self._private_protection_storage_masks

    @private_protection_storage_masks.setter
    def private_protection_storage_masks(self, value):
        if value is None:
            self._private_protection_storage_masks = None
        elif isinstance(value, objects.ProtectionStorageMasks):
            if value.tag == enums.Tags.PRIVATE_PROTECTION_STORAGE_MASKS:
                self._private_protection_storage_masks = value
            else:
                raise TypeError(
                    "The private protection storage masks must be a "
                    "ProtectionStorageMasks structure with a "
                    "PrivateProtectionStorageMasks tag."
                )
        else:
            raise TypeError(
                "The private protection storage masks must be a "
                "ProtectionStorageMasks structure."
            )

    @property
    def public_protection_storage_masks(self):
        return self._public_protection_storage_masks

    @public_protection_storage_masks.setter
    def public_protection_storage_masks(self, value):
        if value is None:
            self._public_protection_storage_masks = None
        elif isinstance(value, objects.ProtectionStorageMasks):
            if value.tag == enums.Tags.PUBLIC_PROTECTION_STORAGE_MASKS:
                self._public_protection_storage_masks = value
            else:
                raise TypeError(
                    "The public protection storage masks must be a "
                    "ProtectionStorageMasks structure with a "
                    "PublicProtectionStorageMasks tag."
                )
        else:
            raise TypeError(
                "The public protection storage masks must be a "
                "ProtectionStorageMasks structure."
            )

    def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the CreateKeyPair request payload and decode it
        into its constituent parts.

        Args:
            input_buffer (stream): A data buffer containing encoded object
                data, supporting a read method.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.
        """
        super(CreateKeyPairRequestPayload, self).read(
            input_buffer,
            kmip_version=kmip_version
        )
        local_buffer = utils.BytearrayStream(input_buffer.read(self.length))

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(
                    enums.Tags.COMMON_TEMPLATE_ATTRIBUTE,
                    local_buffer
            ):
                self._common_template_attribute = objects.TemplateAttribute(
                    tag=enums.Tags.COMMON_TEMPLATE_ATTRIBUTE
                )
                self._common_template_attribute.read(
                    local_buffer,
                    kmip_version=kmip_version
                )
        else:
            if self.is_tag_next(enums.Tags.COMMON_ATTRIBUTES, local_buffer):
                attributes = objects.Attributes(
                    tag=enums.Tags.COMMON_ATTRIBUTES
                )
                attributes.read(local_buffer, kmip_version=kmip_version)
                self._common_template_attribute = \
                    objects.convert_attributes_to_template_attribute(
                        attributes
                    )

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(
                    enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE,
                    local_buffer
            ):
                self._private_key_template_attribute = \
                    objects.TemplateAttribute(
                        tag=enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE
                    )
                self._private_key_template_attribute.read(
                    local_buffer,
                    kmip_version=kmip_version
                )
        else:
            if self.is_tag_next(
                    enums.Tags.PRIVATE_KEY_ATTRIBUTES,
                    local_buffer
            ):
                attributes = objects.Attributes(
                    tag=enums.Tags.PRIVATE_KEY_ATTRIBUTES
                )
                attributes.read(local_buffer, kmip_version=kmip_version)
                self._private_key_template_attribute = \
                    objects.convert_attributes_to_template_attribute(
                        attributes
                    )

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(
                    enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE,
                    local_buffer
            ):
                self._public_key_template_attribute = \
                    objects.TemplateAttribute(
                        tag=enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE
                    )
                self._public_key_template_attribute.read(
                    local_buffer,
                    kmip_version=kmip_version
                )
        else:
            if self.is_tag_next(
                    enums.Tags.PUBLIC_KEY_ATTRIBUTES,
                    local_buffer
            ):
                attributes = objects.Attributes(
                    tag=enums.Tags.PUBLIC_KEY_ATTRIBUTES
                )
                attributes.read(local_buffer, kmip_version=kmip_version)
                self._public_key_template_attribute = \
                    objects.convert_attributes_to_template_attribute(
                        attributes
                    )

        if kmip_version >= enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(
                enums.Tags.COMMON_PROTECTION_STORAGE_MASKS,
                local_buffer
            ):
                storage_masks = objects.ProtectionStorageMasks(
                    tag=enums.Tags.COMMON_PROTECTION_STORAGE_MASKS
                )
                storage_masks.read(local_buffer, kmip_version=kmip_version)
                self._common_protection_storage_masks = storage_masks
            if self.is_tag_next(
                enums.Tags.PRIVATE_PROTECTION_STORAGE_MASKS,
                local_buffer
            ):
                storage_masks = objects.ProtectionStorageMasks(
                    tag=enums.Tags.PRIVATE_PROTECTION_STORAGE_MASKS
                )
                storage_masks.read(local_buffer, kmip_version=kmip_version)
                self._private_protection_storage_masks = storage_masks
            if self.is_tag_next(
                enums.Tags.PUBLIC_PROTECTION_STORAGE_MASKS,
                local_buffer
            ):
                storage_masks = objects.ProtectionStorageMasks(
                    tag=enums.Tags.PUBLIC_PROTECTION_STORAGE_MASKS
                )
                storage_masks.read(local_buffer, kmip_version=kmip_version)
                self._public_protection_storage_masks = storage_masks

        self.is_oversized(local_buffer)

    def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the CreateKeyPair request payload to a buffer.

        Args:
            output_buffer (stream): A data buffer in which to encode object
                data, supporting a write method.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.
        """
        local_buffer = utils.BytearrayStream()

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self._common_template_attribute is not None:
                self._common_template_attribute.write(
                    local_buffer,
                    kmip_version=kmip_version
                )
        else:
            if self._common_template_attribute is not None:
                attributes = objects.convert_template_attribute_to_attributes(
                    self._common_template_attribute
                )
                attributes.write(local_buffer, kmip_version=kmip_version)

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self._private_key_template_attribute is not None:
                self._private_key_template_attribute.write(
                    local_buffer,
                    kmip_version=kmip_version
                )
        else:
            if self._private_key_template_attribute is not None:
                attributes = objects.convert_template_attribute_to_attributes(
                    self._private_key_template_attribute
                )
                attributes.write(local_buffer, kmip_version=kmip_version)

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self._public_key_template_attribute is not None:
                self._public_key_template_attribute.write(
                    local_buffer,
                    kmip_version=kmip_version
                )
        else:
            if self._public_key_template_attribute is not None:
                attributes = objects.convert_template_attribute_to_attributes(
                    self._public_key_template_attribute
                )
                attributes.write(local_buffer, kmip_version=kmip_version)

        if kmip_version >= enums.KMIPVersion.KMIP_2_0:
            if self._common_protection_storage_masks:
                self._common_protection_storage_masks.write(
                    local_buffer,
                    kmip_version=kmip_version
                )
            if self._private_protection_storage_masks:
                self._private_protection_storage_masks.write(
                    local_buffer,
                    kmip_version=kmip_version
                )
            if self._public_protection_storage_masks:
                self._public_protection_storage_masks.write(
                    local_buffer,
                    kmip_version=kmip_version
                )

        self.length = local_buffer.length()
        super(CreateKeyPairRequestPayload, self).write(
            output_buffer,
            kmip_version=kmip_version
        )
        output_buffer.write(local_buffer.buffer)

    def __eq__(self, other):
        if isinstance(other, CreateKeyPairRequestPayload):
            if self.common_template_attribute != \
                    other.common_template_attribute:
                return False
            elif self.private_key_template_attribute != \
                    other.private_key_template_attribute:
                return False
            elif self.public_key_template_attribute != \
                    other.public_key_template_attribute:
                return False
            elif self.common_protection_storage_masks != \
                    other.common_protection_storage_masks:
                return False
            elif self.private_protection_storage_masks != \
                    other.private_protection_storage_masks:
                return False
            elif self.public_protection_storage_masks != \
                    other.public_protection_storage_masks:
                return False
            else:
                return True
        else:
            return NotImplemented

    def __ne__(self, other):
        if isinstance(other, CreateKeyPairRequestPayload):
            return not (self == other)
        else:
            return NotImplemented

    def __repr__(self):
        args = ", ".join([
            "common_template_attribute={}".format(
                self.common_template_attribute
            ),
            "private_key_template_attribute={}".format(
                self.private_key_template_attribute
            ),
            "public_key_template_attribute={}".format(
                self.public_key_template_attribute
            ),
            "common_protection_storage_masks={}".format(
                repr(self.common_protection_storage_masks)
            ),
            "private_protection_storage_masks={}".format(
                repr(self.private_protection_storage_masks)
            ),
            "public_protection_storage_masks={}".format(
                repr(self.public_protection_storage_masks)
            )
        ])
        return "CreateKeyPairRequestPayload({})".format(args)

    def __str__(self):
        value = ", ".join(
            [
                '"common_template_attribute": {}'.format(
                    self.common_template_attribute
                ),
                '"private_key_template_attribute": {}'.format(
                    self.private_key_template_attribute
                ),
                '"public_key_template_attribute": {}'.format(
                    self.public_key_template_attribute
                ),
                '"common_protection_storage_masks": {}'.format(
                    str(self.common_protection_storage_masks)
                ),
                '"private_protection_storage_masks": {}'.format(
                    str(self.private_protection_storage_masks)
                ),
                '"public_protection_storage_masks": {}'.format(
                    str(self.public_protection_storage_masks)
                )
            ]
        )
        return '{' + value + '}'


class CreateKeyPairResponsePayload(base.ResponsePayload):
    """
    A response payload for the CreateKeyPair operation.

    Attributes:
        private_key_unique_identifier: The ID of the new private key.
        public_key_unique_identifier: The ID of the new public key.
        private_key_template_attribute: A group of attributes to set on the new
            private key.
        public_key_template_attribute: A group of attributes to set on the new
            public key.
    """

    def __init__(self,
                 private_key_unique_identifier=None,
                 public_key_unique_identifier=None,
                 private_key_template_attribute=None,
                 public_key_template_attribute=None):
        """
        Construct a CreateKeyPair response payload structure.

        Args:
            private_key_unique_identifier (string): A string specifying the
                ID of the new private key. Optional, defaults to None. Required
                for read/write.
            public_key_unique_identifier (string): A string specifying the
                ID of the new public key. Optional, defaults to None. Required
                for read/write.
            private_key_template_attribute (TemplateAttribute): A
                TemplateAttribute structure with the
                PrivateKeyTemplateAttribute tag containing the set of
                attributes that were set on the new private key. Optional,
                defaults to None.
            public_key_template_attribute (TemplateAttribute): A
                TemplateAttribute structure with the PublicKeyTemplateAttribute
                tag containing the set of attributes that were set on the new
                public key. Optional, defaults to None.
        """
        super(CreateKeyPairResponsePayload, self).__init__()

        self._private_key_unique_identifier = None
        self._public_key_unique_identifier = None
        self._private_key_template_attribute = None
        self._public_key_template_attribute = None

        self.private_key_unique_identifier = private_key_unique_identifier
        self.public_key_unique_identifier = public_key_unique_identifier
        self.private_key_template_attribute = private_key_template_attribute
        self.public_key_template_attribute = public_key_template_attribute

    @property
    def private_key_unique_identifier(self):
        if self._private_key_unique_identifier:
            return self._private_key_unique_identifier.value
        else:
            return None

    @private_key_unique_identifier.setter
    def private_key_unique_identifier(self, value):
        if value is None:
            self._private_key_unique_identifier = None
        elif isinstance(value, six.string_types):
            self._private_key_unique_identifier = primitives.TextString(
                value=value,
                tag=enums.Tags.PRIVATE_KEY_UNIQUE_IDENTIFIER
            )
        else:
            raise TypeError("Private key unique identifier must be a string.")

    @property
    def public_key_unique_identifier(self):
        if self._public_key_unique_identifier:
            return self._public_key_unique_identifier.value
        else:
            return None

    @public_key_unique_identifier.setter
    def public_key_unique_identifier(self, value):
        if value is None:
            self._public_key_unique_identifier = None
        elif isinstance(value, six.string_types):
            self._public_key_unique_identifier = primitives.TextString(
                value=value,
                tag=enums.Tags.PUBLIC_KEY_UNIQUE_IDENTIFIER
            )
        else:
            raise TypeError("Public key unique identifier must be a string.")

    @property
    def private_key_template_attribute(self):
        return self._private_key_template_attribute

    @private_key_template_attribute.setter
    def private_key_template_attribute(self, value):
        if value is None:
            self._private_key_template_attribute = None
        elif isinstance(value, objects.TemplateAttribute):
            if value.tag == enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE:
                self._private_key_template_attribute = value
            else:
                raise TypeError(
                    "Private key template attribute must be a "
                    "TemplateAttribute structure with a "
                    "PrivateKeyTemplateAttribute tag."
                )
        else:
            raise TypeError(
                "Private key template attribute must be a TemplateAttribute "
                "structure."
            )

    @property
    def public_key_template_attribute(self):
        return self._public_key_template_attribute

    @public_key_template_attribute.setter
    def public_key_template_attribute(self, value):
        if value is None:
            self._public_key_template_attribute = None
        elif isinstance(value, objects.TemplateAttribute):
            if value.tag == enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE:
                self._public_key_template_attribute = value
            else:
                raise TypeError(
                    "Public key template attribute must be a "
                    "TemplateAttribute structure with a "
                    "PublicKeyTemplateAttribute tag."
                )
        else:
            raise TypeError(
                "Public key template attribute must be a TemplateAttribute "
                "structure."
            )

    def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the CreateKeyPair response payload and decode it
        into its constituent parts.

        Args:
            input_buffer (stream): A data buffer containing encoded object
                data, supporting a read method.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            InvalidKmipEncoding: Raised if the private key unique identifier or
                the public key unique identifier is missing from the encoded
                payload.
        """
        super(CreateKeyPairResponsePayload, self).read(
            input_buffer,
            kmip_version=kmip_version
        )
        local_buffer = utils.BytearrayStream(input_buffer.read(self.length))

        if self.is_tag_next(
                enums.Tags.PRIVATE_KEY_UNIQUE_IDENTIFIER,
                local_buffer
        ):
            self._private_key_unique_identifier = primitives.TextString(
                tag=enums.Tags.PRIVATE_KEY_UNIQUE_IDENTIFIER
            )
            self._private_key_unique_identifier.read(
                local_buffer,
                kmip_version=kmip_version
            )
        else:
            raise exceptions.InvalidKmipEncoding(
                "The CreateKeyPair response payload encoding is missing the "
                "private key unique identifier."
            )

        if self.is_tag_next(
                enums.Tags.PUBLIC_KEY_UNIQUE_IDENTIFIER,
                local_buffer
        ):
            self._public_key_unique_identifier = primitives.TextString(
                tag=enums.Tags.PUBLIC_KEY_UNIQUE_IDENTIFIER
            )
            self._public_key_unique_identifier.read(
                local_buffer,
                kmip_version=kmip_version
            )
        else:
            raise exceptions.InvalidKmipEncoding(
                "The CreateKeyPair response payload encoding is missing the "
                "public key unique identifier."
            )

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(
                    enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE,
                    local_buffer
            ):
                self._private_key_template_attribute = \
                    objects.TemplateAttribute(
                        tag=enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE
                    )
                self._private_key_template_attribute.read(
                    local_buffer,
                    kmip_version=kmip_version
                )

            if self.is_tag_next(
                    enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE,
                    local_buffer
            ):
                self._public_key_template_attribute = \
                    objects.TemplateAttribute(
                        tag=enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE
                    )
                self._public_key_template_attribute.read(
                    local_buffer,
                    kmip_version=kmip_version
                )

        self.is_oversized(local_buffer)

    def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the CreateKeyPair response payload to a buffer.

        Args:
            output_buffer (stream): A data buffer in which to encode object
                data, supporting a write method.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            InvalidField: Raised if the private key unique identifier or the
                public key unique identifier is not defined.
        """
        local_buffer = utils.BytearrayStream()

        if self._private_key_unique_identifier:
            self._private_key_unique_identifier.write(
                local_buffer,
                kmip_version=kmip_version
            )
        else:
            raise exceptions.InvalidField(
                "The CreateKeyPair response payload is missing the private "
                "key unique identifier field."
            )

        if self._public_key_unique_identifier:
            self._public_key_unique_identifier.write(
                local_buffer,
                kmip_version=kmip_version
            )
        else:
            raise exceptions.InvalidField(
                "The CreateKeyPair response payload is missing the public "
                "key unique identifier field."
            )

        if self._private_key_template_attribute:
            self._private_key_template_attribute.write(
                local_buffer,
                kmip_version=kmip_version
            )

        if self._public_key_template_attribute:
            self._public_key_template_attribute.write(
                local_buffer,
                kmip_version=kmip_version
            )

        self.length = local_buffer.length()
        super(CreateKeyPairResponsePayload, self).write(
            output_buffer,
            kmip_version=kmip_version
        )
        output_buffer.write(local_buffer.buffer)

    def __eq__(self, other):
        if isinstance(other, CreateKeyPairResponsePayload):
            if self.private_key_unique_identifier != \
                    other.private_key_unique_identifier:
                return False
            elif self.public_key_unique_identifier != \
                    other.public_key_unique_identifier:
                return False
            elif self.private_key_template_attribute != \
                    other.private_key_template_attribute:
                return False
            elif self.public_key_template_attribute != \
                    other.public_key_template_attribute:
                return False
            else:
                return True
        else:
            return NotImplemented

    def __ne__(self, other):
        if isinstance(other, CreateKeyPairResponsePayload):
            return not (self == other)
        else:
            return NotImplemented

    def __repr__(self):
        args = ", ".join([
            "private_key_unique_identifier='{}'".format(
                self.private_key_unique_identifier
            ),
            "public_key_unique_identifier='{}'".format(
                self.public_key_unique_identifier
            ),
            "private_key_template_attribute={}".format(
                self.private_key_template_attribute
            ),
            "public_key_template_attribute={}".format(
                self.public_key_template_attribute
            )
        ])
        return "CreateKeyPairResponsePayload({})".format(args)

    def __str__(self):
        value = ", ".join(
            [
                '"private_key_unique_identifier": "{}"'.format(
                    self.private_key_unique_identifier
                ),
                '"public_key_unique_identifier": "{}"'.format(
                    self.public_key_unique_identifier
                ),
                '"private_key_template_attribute": {}'.format(
                    self.private_key_template_attribute
                ),
                '"public_key_template_attribute": {}'.format(
                    self.public_key_template_attribute
                )
            ]
        )
        return '{' + value + '}'