-
Notifications
You must be signed in to change notification settings - Fork 516
/
RNDecryptor.m
256 lines (211 loc) · 8.21 KB
/
RNDecryptor.m
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// RNDecryptor
//
// Copyright (c) 2012 Rob Napier
//
// This code is licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#import "RNCryptor+Private.h"
#import "RNDecryptor.h"
#import "RNCryptorEngine.h"
static const NSUInteger kPreambleSize = 2;
@interface RNDecryptor ()
@property (nonatomic, readonly, strong) NSMutableData *inData;
@property (nonatomic, readwrite, copy) NSData *encryptionKey;
@property (nonatomic, readwrite, copy) NSData *HMACKey;
@property (nonatomic, readwrite, copy) NSString *password;
@property (nonatomic, readwrite, assign) BOOL hasV1HMAC;
@end
@implementation RNDecryptor
{
CCHmacContext _HMACContext;
NSMutableData *__inData;
}
@synthesize encryptionKey = _encryptionKey;
@synthesize HMACKey = _HMACKey;
@synthesize password = _password;
+ (NSData *)decryptData:(NSData *)theCipherText withPassword:(NSString *)aPassword error:(NSError **)anError
{
RNDecryptor *cryptor = [[self alloc] initWithPassword:aPassword
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:theCipherText error:anError];
}
+ (NSData *)decryptData:(NSData *)theCipherText withEncryptionKey:(NSData *)encryptionKey HMACKey:(NSData *)HMACKey error:(NSError **)anError;
{
RNDecryptor *cryptor = [[self alloc] initWithEncryptionKey:encryptionKey
HMACKey:HMACKey
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:theCipherText error:anError];
}
- (RNDecryptor *)initWithEncryptionKey:(NSData *)anEncryptionKey HMACKey:(NSData *)anHMACKey handler:(RNCryptorHandler)aHandler
{
self = [super initWithHandler:aHandler];
if (self) {
_encryptionKey = [anEncryptionKey copy];
_HMACKey = [anHMACKey copy];
}
return self;
}
- (RNDecryptor *)initWithPassword:(NSString *)aPassword handler:(RNCryptorHandler)aHandler
{
NSParameterAssert(aPassword != nil);
self = [self initWithEncryptionKey:nil HMACKey:nil handler:aHandler];
if (self) {
_password = aPassword;
}
return self;
}
- (NSMutableData *)inData
{
if (!__inData) {
__inData = [NSMutableData data];
}
return __inData;
}
- (void)decryptData:(NSData *)data
{
dispatch_async(self.queue, ^{
if (self.hasHMAC) {
CCHmacUpdate(&_HMACContext, data.bytes, data.length);
}
NSError *error = nil;
NSData *decryptedData = [self.engine addData:data error:&error];
if (!decryptedData) {
[self cleanupAndNotifyWithError:error];
return;
}
[self.outData appendData:decryptedData];
dispatch_sync(self.responseQueue, ^{
self.handler(self, self.outData);
});
[self.outData setLength:0];
});
}
- (void)addData:(NSData *)theData
{
if (self.isFinished) {
return;
}
[self.inData appendData:theData];
if (!self.engine) {
[self consumeHeaderFromData:self.inData];
}
if (self.engine) {
NSUInteger HMACLength = self.HMACLength;
if (self.inData.length > HMACLength) {
NSData *data = [self.inData _RNConsumeToIndex:self.inData.length - HMACLength];
[self decryptData:data];
}
}
}
- (BOOL)getSettings:(RNCryptorSettings *)settings forPreamble:(NSData *)preamble
{
const uint8_t *bytes = [preamble bytes];
// See http://robnapier.net/blog/rncryptor-hmac-vulnerability-827 for information on the v1 bad HMAC
#ifdef RNCRYPTOR_ALLOW_V1_BAD_HMAC
if (bytes[0] == 1) {
*settings = kRNCryptorAES256Settings;
self.options = bytes[1];
self.hasV1HMAC = YES;
return YES;
}
#endif
if (bytes[0] == kRNCryptorFileVersion) {
*settings = kRNCryptorAES256Settings;
self.options = bytes[1];
return YES;
}
return NO;
}
- (void)consumeHeaderFromData:(NSMutableData *)data
{
if (data.length < kPreambleSize) {
return;
}
RNCryptorSettings settings = {};
if (![self getSettings:&settings forPreamble:[data subdataWithRange:NSMakeRange(0, kPreambleSize)]]) {
[self cleanupAndNotifyWithError:[NSError errorWithDomain:kRNCryptorErrorDomain
code:kRNCryptorUnknownHeader
userInfo:[NSDictionary dictionaryWithObject:@"Unknown header" /* DNL */
forKey:NSLocalizedDescriptionKey]]];
}
NSUInteger headerSize = kPreambleSize + settings.IVSize;
if (self.options & kRNCryptorOptionHasPassword) {
headerSize += settings.keySettings.saltSize + settings.HMACKeySettings.saltSize;
}
if (data.length < headerSize) {
return;
}
NSData *header = [data subdataWithRange:NSMakeRange(0, headerSize)]; // We'll need this for the HMAC later
[[data _RNConsumeToIndex:kPreambleSize] mutableCopy]; // Throw away the preamble
NSError *error = nil;
if (self.options & kRNCryptorOptionHasPassword) {
NSAssert(!self.encryptionKey && !self.HMACKey, @"Both password and the key (%d) or HMACKey (%d) are set.", self.encryptionKey != nil, self.HMACKey != nil);
NSData *encryptionKeySalt = [data _RNConsumeToIndex:settings.keySettings.saltSize];
NSData *HMACKeySalt = [data _RNConsumeToIndex:settings.HMACKeySettings.saltSize];
self.encryptionKey = [[self class] keyForPassword:self.password salt:encryptionKeySalt settings:settings.keySettings];
self.HMACKey = [[self class] keyForPassword:self.password salt:HMACKeySalt settings:settings.HMACKeySettings];
self.password = nil; // Don't need this anymore.
}
NSData *IV = [data _RNConsumeToIndex:settings.IVSize];
self.engine = [[RNCryptorEngine alloc] initWithOperation:kCCDecrypt settings:settings key:self.encryptionKey IV:IV error:&error];
self.encryptionKey = nil; // Don't need this anymore
if (!self.engine) {
[self cleanupAndNotifyWithError:error];
return;
}
if (self.HMACKey) {
CCHmacInit(&_HMACContext, settings.HMACAlgorithm, self.HMACKey.bytes, self.HMACKey.length);
self.HMACLength = settings.HMACLength;
self.HMACKey = nil; // Don't need this anymore
if (! self.hasV1HMAC) {
CCHmacUpdate(&_HMACContext, [header bytes], [header length]);
}
}
}
- (void)finish
{
if (self.isFinished) {
return;
}
dispatch_async(self.queue, ^{
NSError *error = nil;
NSData *decryptedData = [self.engine finishWithError:&error];
if (!decryptedData) {
[self cleanupAndNotifyWithError:error];
return;
}
[self.outData appendData:decryptedData];
if (self.hasHMAC) {
NSMutableData *HMACData = [NSMutableData dataWithLength:self.HMACLength];
CCHmacFinal(&_HMACContext, [HMACData mutableBytes]);
if (![HMACData isEqualToData:self.inData]) {
[self cleanupAndNotifyWithError:[NSError errorWithDomain:kRNCryptorErrorDomain
code:kRNCryptorHMACMismatch
userInfo:[NSDictionary dictionaryWithObject:@"HMAC Mismatch" /* DNL */
forKey:NSLocalizedDescriptionKey]]];
return;
}
}
[self cleanupAndNotifyWithError:nil];
});
}
@end