2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 static const NSString *kBeneficiaryID = @"beneficiaryID";
28 static const NSString *kSponsorID = @"sponsorID";
29 static const NSString *kClock = @"clock";
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;
41 @implementation TPVoucher
43 + (instancetype)voucherWithBeneficiaryID:(NSString *)beneficiaryID
44 sponsorID:(NSString *)sponsorID
45 clock:(TPCounter)clock
46 trustSigningKey:(id<TPSigningKey>)trustSigningKey
47 error:(NSError **)error
49 NSDictionary *dict = @{
50 kBeneficiaryID: beneficiaryID,
51 kSponsorID: sponsorID,
54 NSData *data = [TPUtils serializedPListWithDictionary:dict];
55 NSData *sig = [trustSigningKey signatureForData:data withError:error];
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;
69 + (instancetype)voucherWithPList:(NSData *)voucherInfoPList
70 sig:(NSData *)voucherInfoSig
72 TPVoucher *voucher = [[TPVoucher alloc] init];
73 voucher.voucherInfoPList = [voucherInfoPList copy];
74 voucher.voucherInfoSig = [voucherInfoSig copy];
76 id dict = [NSPropertyListSerialization propertyListWithData:voucherInfoPList
77 options:NSPropertyListImmutable
80 if (![dict isKindOfClass:[NSDictionary class]]) {
84 if (![dict[kBeneficiaryID] isKindOfClass:[NSString class]]) {
87 voucher.beneficiaryID = dict[kBeneficiaryID];
89 if (![dict[kSponsorID] isKindOfClass:[NSString class]]) {
92 voucher.sponsorID = dict[kSponsorID];
94 if (![dict[kClock] isKindOfClass:[NSNumber class]]) {
97 voucher.clock = [dict[kClock] unsignedLongLongValue];
102 - (BOOL)isEqualToVoucher:(TPVoucher *)other
107 return [self.voucherInfoPList isEqualToData:other.voucherInfoPList]
108 && [self.voucherInfoSig isEqualToData:other.voucherInfoSig];
111 #pragma mark - NSObject
113 - (BOOL)isEqual:(id)object
115 if (self == object) {
118 if (![object isKindOfClass:[TPVoucher class]]) {
121 return [self isEqualToVoucher:object];
126 return [self.voucherInfoPList hash];