]> git.saurik.com Git - apple/security.git/blob - keychain/otpaird/OTPairingPacketContext.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / otpaird / OTPairingPacketContext.m
1 #import <TargetConditionals.h>
2 #import <Foundation/Foundation.h>
3 #import <IDS/IDS.h>
4 #import <Security/SecXPCHelper.h>
5
6 #import "keychain/categories/NSError+UsefulConstructors.h"
7
8 #import "OTPairingPacketContext.h"
9 #import "OTPairingConstants.h"
10
11 @interface OTPairingPacketContext ()
12 @property (readwrite) NSDictionary *message;
13 @property (readwrite) NSString *fromID;
14 @property (readwrite) IDSMessageContext *context;
15 @end
16
17 @implementation OTPairingPacketContext
18
19 @synthesize message = _message;
20 @synthesize fromID = _fromID;
21 @synthesize context = _context;
22 @synthesize error = _error;
23
24 - (instancetype)initWithMessage:(NSDictionary *)message fromID:(NSString *)fromID context:(IDSMessageContext *)context
25 {
26 self = [super init];
27 if (self != nil) {
28 self.message = message;
29 self.fromID = fromID;
30 self.context = context;
31 }
32 return self;
33 }
34
35 - (enum OTPairingIDSMessageType)messageType
36 {
37 NSNumber *typeNum;
38 enum OTPairingIDSMessageType type;
39
40 typeNum = self.message[OTPairingIDSKeyMessageType];
41 if (typeNum != nil) {
42 type = [typeNum intValue];
43 } else {
44 // From older internal builds; remove soon
45 if (self.packetData != nil) {
46 type = OTPairingIDSMessageTypePacket;
47 } else {
48 type = OTPairingIDSMessageTypeError;
49 }
50 }
51
52 return type;
53 }
54
55 - (NSString *)sessionIdentifier
56 {
57 return self.message[OTPairingIDSKeySession];
58 }
59
60 - (NSData *)packetData
61 {
62 return self.message[OTPairingIDSKeyPacket];
63 }
64
65 - (NSError *)error
66 {
67 if (self.messageType != OTPairingIDSMessageTypeError) {
68 return nil;
69 }
70
71 if (!self->_error) {
72 NSData *errorData = self.message[OTPairingIDSKeyError];
73 if (errorData != NULL) {
74 self->_error = [SecXPCHelper errorFromEncodedData:errorData];
75 } else {
76 // Key from older iOS builds; remove soon
77 // When this is removed, it will still be useful to have a fallback in case errorData is missing or errorFromEncodedData fails
78 NSString *errorString = self.message[OTPairingIDSKeyErrorDeprecated];
79 self->_error = [NSError errorWithDomain:OTPairingErrorDomain code:OTPairingErrorTypeRemote description:errorString];
80 }
81 }
82
83 return self->_error;
84 }
85
86 - (NSString *)incomingResponseIdentifier
87 {
88 return self.context.incomingResponseIdentifier;
89 }
90
91 - (NSString *)outgoingResponseIdentifier
92 {
93 return self.context.outgoingResponseIdentifier;
94 }
95
96 @end