1 #import <TargetConditionals.h>
2 #import <Foundation/Foundation.h>
4 #import <Security/SecXPCHelper.h>
6 #import "keychain/categories/NSError+UsefulConstructors.h"
8 #import "OTPairingPacketContext.h"
9 #import "OTPairingConstants.h"
11 @interface OTPairingPacketContext ()
12 @property (readwrite) NSDictionary *message;
13 @property (readwrite) NSString *fromID;
14 @property (readwrite) IDSMessageContext *context;
17 @implementation OTPairingPacketContext
19 @synthesize message = _message;
20 @synthesize fromID = _fromID;
21 @synthesize context = _context;
22 @synthesize error = _error;
24 - (instancetype)initWithMessage:(NSDictionary *)message fromID:(NSString *)fromID context:(IDSMessageContext *)context
28 self.message = message;
30 self.context = context;
35 - (enum OTPairingIDSMessageType)messageType
38 enum OTPairingIDSMessageType type;
40 typeNum = self.message[OTPairingIDSKeyMessageType];
42 type = [typeNum intValue];
44 // From older internal builds; remove soon
45 if (self.packetData != nil) {
46 type = OTPairingIDSMessageTypePacket;
48 type = OTPairingIDSMessageTypeError;
55 - (NSString *)sessionIdentifier
57 return self.message[OTPairingIDSKeySession];
60 - (NSData *)packetData
62 return self.message[OTPairingIDSKeyPacket];
67 if (self.messageType != OTPairingIDSMessageTypeError) {
72 NSData *errorData = self.message[OTPairingIDSKeyError];
73 if (errorData != NULL) {
74 self->_error = [SecXPCHelper errorFromEncodedData:errorData];
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];
86 - (NSString *)incomingResponseIdentifier
88 return self.context.incomingResponseIdentifier;
91 - (NSString *)outgoingResponseIdentifier
93 return self.context.outgoingResponseIdentifier;