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