1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
//
// PolicyDataParser.m
//
#import "SDLPolicyDataParser.h"
#import "SDLMacros.h"
NS_ASSUME_NONNULL_BEGIN
@implementation SDLPolicyDataParser
- (nullable NSData *)unwrap:(NSData *)wrappedData {
NSData *decodedData = nil;
@try {
NSError *errorJSONSerialization = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:wrappedData options:kNilOptions error:&errorJSONSerialization];
NSArray<NSString *> *array = dictionary[@"data"];
NSString *base64EncodedString = array[0];
if ([NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)]) {
decodedData = [[NSData alloc] initWithBase64EncodedString:base64EncodedString options:0];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
decodedData = [[NSData alloc] initWithBase64Encoding:base64EncodedString];
#pragma clang diagnostic pop
}
}
@catch (NSException *exception) {
decodedData = nil;
[SDLDebugTool logInfo:@"Error in PolicyDataParser::unwrap()"];
}
return decodedData;
}
- (void)parsePolicyData:(NSData *)data {
@try {
Byte *bytes = (Byte *)data.bytes;
Byte firstByte = bytes[0];
self.protocolVersion = (firstByte & 0b11100000) >> 5;
self.isResponseRequired = (firstByte & 0b00010000) != 0;
self.isHighBandwidth = (firstByte & 0b00001000) != 0;
self.isSigned = (firstByte & 0b00000100) != 0;
self.isEncrypted = (firstByte & 0b00000010) != 0;
self.hasESN = (firstByte & 0b00000001) != 0;
self.serviceType = bytes[1];
Byte thirdByte = bytes[2];
self.commandType = (thirdByte & 0b11110000) >> 4;
self.CPUDestination = (thirdByte & 0b00001000) != 0;
self.encryptionKeyIndex = (thirdByte & 0b00000111);
const int payloadSizeOffset = 3;
if (self.isHighBandwidth) {
self.payloadSize = ntohl(*(UInt32 *)(bytes + payloadSizeOffset));
} else {
self.payloadSize = ntohs(*(UInt16 *)(bytes + payloadSizeOffset));
}
if (self.hasESN) {
int esnOffset = self.isHighBandwidth ? 7 : 5;
self.ESN = [NSData dataWithBytes:(bytes + esnOffset) length:8];
}
if (self.isHighBandwidth) {
int moduleMessageIdOffset = 7;
if (self.hasESN)
moduleMessageIdOffset += self.ESN.length;
self.moduleMessageId = ntohl(*(UInt32 *)(bytes + moduleMessageIdOffset));
int serverMessageIdOffset = 11;
if (self.hasESN)
serverMessageIdOffset += self.ESN.length;
self.serverMessageId = ntohl(*(UInt32 *)(bytes + serverMessageIdOffset));
int messageStatusOffset = 15;
if (self.hasESN)
messageStatusOffset += self.ESN.length;
self.messageStatus = bytes[messageStatusOffset];
}
if (self.isEncrypted) {
int ivOffset = 5;
if (self.isHighBandwidth)
ivOffset += 11;
if (self.hasESN)
ivOffset += self.ESN.length;
self.initializationVector = [NSData dataWithBytes:(bytes + ivOffset) length:16];
}
int payloadOffset = 5;
if (self.isHighBandwidth) {
payloadOffset += 11;
}
if (self.hasESN) {
payloadOffset += self.ESN.length;
}
if (self.isEncrypted) {
payloadOffset += self.initializationVector.length;
}
self.payload = [NSData dataWithBytes:(bytes + payloadOffset) length:self.payloadSize];
if (self.isSigned) {
int signatureTagOffset = (int)data.length - 16;
self.signatureTag = [NSData dataWithBytes:(bytes + signatureTagOffset) length:16];
}
}
@catch (NSException *exception) {
[SDLDebugTool logInfo:@"Error in PolicyDataParser::parsePolicyData()"];
}
}
- (NSString *)description {
NSString *esnString = [[NSString alloc] initWithData:self.ESN encoding:NSUTF8StringEncoding];
NSMutableString *descriptionString = [NSMutableString stringWithFormat:@"Ver:%u; ", self.protocolVersion];
[descriptionString appendFormat:@"RespReq:%u; ", self.isResponseRequired];
[descriptionString appendFormat:@"HiBnd:%u; ", self.isHighBandwidth];
[descriptionString appendFormat:@"Sign:%u; ", self.isSigned];
[descriptionString appendFormat:@"Enc:%u; ", self.isEncrypted];
[descriptionString appendFormat:@"HasESN:%u; ", self.hasESN];
[descriptionString appendFormat:@"Svc:%u; ", self.serviceType];
[descriptionString appendFormat:@"Cmd:%u; ", self.commandType];
[descriptionString appendFormat:@"CPU:%u; ", self.CPUDestination];
[descriptionString appendFormat:@"EncKey:%u; ", self.encryptionKeyIndex];
[descriptionString appendFormat:@"sz:%u; ", (unsigned int)self.payloadSize];
[descriptionString appendFormat:@"ESN:%@; ", esnString];
[descriptionString appendFormat:@"mmid:%u; ", (unsigned int)self.moduleMessageId];
[descriptionString appendFormat:@"smid:%u; ", (unsigned int)self.serverMessageId];
[descriptionString appendFormat:@"status:%u; ", self.messageStatus];
[descriptionString appendFormat:@"iv:%@; ", self.initializationVector];
[descriptionString appendFormat:@"hmac:%@", self.signatureTag];
return descriptionString;
}
@end
NS_ASSUME_NONNULL_END
|