]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeers/TPVoucher.m
Security-58286.1.32.tar.gz
[apple/security.git] / keychain / trust / TrustedPeers / TPVoucher.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import "TPVoucher.h"
25 #import "TPUtils.h"
26
27 static const NSString *kBeneficiaryID = @"beneficiaryID";
28 static const NSString *kSponsorID = @"sponsorID";
29 static const NSString *kClock = @"clock";
30
31
32 @interface TPVoucher ()
33 @property (nonatomic, strong) NSString *beneficiaryID;
34 @property (nonatomic, strong) NSString *sponsorID;
35 @property (nonatomic, assign) TPCounter clock;
36 @property (nonatomic, strong) NSData *voucherInfoPList;
37 @property (nonatomic, strong) NSData *voucherInfoSig;
38 @end
39
40
41 @implementation TPVoucher
42
43 + (instancetype)voucherWithBeneficiaryID:(NSString *)beneficiaryID
44 sponsorID:(NSString *)sponsorID
45 clock:(TPCounter)clock
46 trustSigningKey:(id<TPSigningKey>)trustSigningKey
47 error:(NSError **)error
48 {
49 NSDictionary *dict = @{
50 kBeneficiaryID: beneficiaryID,
51 kSponsorID: sponsorID,
52 kClock: @(clock)
53 };
54 NSData *data = [TPUtils serializedPListWithDictionary:dict];
55 NSData *sig = [trustSigningKey signatureForData:data withError:error];
56 if (nil == sig) {
57 return nil;
58 }
59
60 TPVoucher *voucher = [[TPVoucher alloc] init];
61 voucher.beneficiaryID = [beneficiaryID copy];
62 voucher.sponsorID = [sponsorID copy];
63 voucher.clock = clock;
64 voucher.voucherInfoPList = data;
65 voucher.voucherInfoSig = sig;
66 return voucher;
67 }
68
69 + (instancetype)voucherWithPList:(NSData *)voucherInfoPList
70 sig:(NSData *)voucherInfoSig
71 {
72 TPVoucher *voucher = [[TPVoucher alloc] init];
73 voucher.voucherInfoPList = [voucherInfoPList copy];
74 voucher.voucherInfoSig = [voucherInfoSig copy];
75
76 id dict = [NSPropertyListSerialization propertyListWithData:voucherInfoPList
77 options:NSPropertyListImmutable
78 format:nil
79 error:NULL];
80 if (![dict isKindOfClass:[NSDictionary class]]) {
81 return nil;
82 }
83
84 if (![dict[kBeneficiaryID] isKindOfClass:[NSString class]]) {
85 return nil;
86 }
87 voucher.beneficiaryID = dict[kBeneficiaryID];
88
89 if (![dict[kSponsorID] isKindOfClass:[NSString class]]) {
90 return nil;
91 }
92 voucher.sponsorID = dict[kSponsorID];
93
94 if (![dict[kClock] isKindOfClass:[NSNumber class]]) {
95 return nil;
96 }
97 voucher.clock = [dict[kClock] unsignedLongLongValue];
98
99 return voucher;
100 }
101
102 - (BOOL)isEqualToVoucher:(TPVoucher *)other
103 {
104 if (other == self) {
105 return YES;
106 }
107 return [self.voucherInfoPList isEqualToData:other.voucherInfoPList]
108 && [self.voucherInfoSig isEqualToData:other.voucherInfoSig];
109 }
110
111 #pragma mark - NSObject
112
113 - (BOOL)isEqual:(id)object
114 {
115 if (self == object) {
116 return YES;
117 }
118 if (![object isKindOfClass:[TPVoucher class]]) {
119 return NO;
120 }
121 return [self isEqualToVoucher:object];
122 }
123
124 - (NSUInteger)hash
125 {
126 return [self.voucherInfoPList hash];
127 }
128
129 @end