-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlorPasswordRetriever.m
executable file
·274 lines (201 loc) · 8.23 KB
/
BlorPasswordRetriever.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//
// BlorPasswordRetriever.m
// Notation
//
// Created by Zachary Schneirov on 12/13/06.
// Copyright 2006 Zachary Schneirov. All rights reserved.
//
#import "BlorPasswordRetriever.h"
#import "NoteObject.h"
#import "GlobalPrefs.h"
#import "NSData_transformations.h"
#import "AttributedPlainText.h"
#import "NotationPrefs.h"
#include "idea_ossl.h"
@implementation BlorPasswordRetriever
- (id)initWithBlor:(NSString*)blorPath {
if ([super init]) {
path = [blorPath retain];
couldRetrieveFromKeychain = NO;
//read hash (first 20 bytes) of file
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
hashData = [[handle readDataOfLength:20] retain];
[handle closeFile];
if (!hashData || [hashData length] < 20)
return nil;
}
return self;
}
- (IBAction)cancelAction:(id)sender {
[NSApp stopModalWithCode:0];
[window close];
if (![[GlobalPrefs defaultPrefs] triedToImportBlor])
NSRunAlertPanel(NSLocalizedString(@"Note Importing Cancelled", nil),
NSLocalizedString(@"You can import your old notes at any time by choosing quotemarkImport...quotemark from the quotemarkNotequotemark menu and selecting your NotationalDatabase.blor file.",nil),
NSLocalizedString(@"OK",nil), nil, nil);
}
- (IBAction)importAction:(id)sender {
//check pw against hash and defaultCStringEncoding
NSData *passData = [[passphraseField stringValue] dataUsingEncoding:[NSString defaultCStringEncoding] allowLossyConversion:NO];
if ([[passData SHA1Digest] isEqualToData:hashData]) {
[NSApp stopModalWithCode:1];
[window close];
} else {
NSBeginAlertSheet(NSLocalizedString(@"Sorry, you entered an incorrect passphrase.",nil), NSLocalizedString(@"OK",nil),
nil, nil, window, nil, NULL, NULL, NULL, NSLocalizedString(@"Please try again.",nil));
}
}
- (NSData*)keychainPasswordData {
NSString *keychainAccountString = [[path stringByAbbreviatingWithTildeInPath] lowercaseString];
if ([keychainAccountString length] > 255) keychainAccountString = [keychainAccountString substringToIndex:255];
const char *keychainAccountCString = [[keychainAccountString dataUsingEncoding:
[NSString defaultCStringEncoding] allowLossyConversion:YES] bytes];
UInt32 len;
void *p = (void *)calloc(256, sizeof(char));
if (kcfindgenericpassword("NV", keychainAccountCString, 255, p, &len, NULL) != noErr) {
free(p);
return NULL;
}
return [NSData dataWithBytesNoCopy:p length:len freeWhenDone:YES];
}
- (NSData*)validPasswordHashData {
[originalPasswordString release];
originalPasswordString = nil;
//try to get PW from keychain. if that fails, request from user
NSData *passwordData = [self keychainPasswordData];
if (passwordData && [[passwordData SHA1Digest] isEqualToData:hashData]) {
couldRetrieveFromKeychain = YES;
originalPasswordString = [[NSString alloc] initWithData:passwordData encoding:[NSString defaultCStringEncoding]];
return [passwordData BrokenMD5Digest];
}
//run dialog and grab PW
if (!window) {
if (![NSBundle loadNibNamed:@"BlorPasswordRetriever" owner:self]) {
NSLog(@"Failed to load BlorPasswordRetriever.nib");
NSBeep();
return NULL;
}
}
[helpStringField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Please enter the passphrase to import old notes at %@.",nil),
[path stringByAbbreviatingWithTildeInPath]]];
EnableSecureEventInput();
int result = [NSApp runModalForWindow:window];
DisableSecureEventInput();
NSString *passwordString = [passphraseField stringValue];
passwordData = [passwordString dataUsingEncoding:[NSString defaultCStringEncoding] allowLossyConversion:NO];
if (result && [[passwordData SHA1Digest] isEqualToData:hashData]) {
originalPasswordString = [passwordString copy];
[passphraseField setStringValue:@""];
return [passwordData BrokenMD5Digest];
}
[passphraseField setStringValue:@""];
return NULL;
}
- (NSString*)originalPasswordString {
return originalPasswordString;
}
- (BOOL)canRetrieveFromKeychain {
return couldRetrieveFromKeychain;
}
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:)
name:NSControlTextDidChangeNotification object:passphraseField];
}
- (void)textDidChange:(NSNotification *)aNotification {
[importButton setEnabled:([[passphraseField stringValue] length] > 0)];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[hashData release];
[path release];
[originalPasswordString release];
[super dealloc];
}
@end
@implementation BlorNoteEnumerator
- (id)initWithBlor:(NSString*)blorPath passwordHashData:(NSData*)passwordHashData {
if ([super init]) {
path = [blorPath retain];
if (!(keyData = [passwordHashData retain]))
return nil;
if (!(blorData = [[NSMutableData dataWithContentsOfFile:path] retain]))
return nil;
if ([blorData length] < 28) {
NSLog(@"read data is too small (%d) to hold any notes!", [blorData length]);
return nil;
}
successfullyReadNoteCount = 0;
suspectedNoteCount = *(unsigned int*)([blorData bytes] + 20);
suspectedNoteCount = CFSwapInt32BigToHost(suspectedNoteCount);
currentByteOffset = 24;
//read past the # of notes marker--we're just going to read as many notes as possible
}
return self;
}
- (void)dealloc {
[blorData release];
[keyData release];
[super dealloc];
}
- (void)decryptNextBytesOfLength:(long)length {
unsigned char iv[] = {
0x50, 0x7E, 0x4C, 0x17,
0x99, 0x3A, 0x07, 0x01
};
int num = 0;
IDEA_KEY_SCHEDULE enc;
idea_set_encrypt_key([keyData bytes], &enc);
unsigned char *bytes = [blorData mutableBytes] + currentByteOffset;
idea_cfb64_encrypt(bytes, bytes, length, &enc, iv, &num, IDEA_DECRYPT);
}
- (unsigned int)suspectedNoteCount {
return suspectedNoteCount;
}
#define ASSERT_CAN_READ_BYTE_COUNT(n) do { \
if (!((n) + currentByteOffset <= [blorData length])) { \
NSLog(@"Attempted to read %d bytes past the length of the blor!", ((n) + currentByteOffset) - [blorData length]);\
return nil;\
} \
} while (0)
- (id)nextNote {
int titleBytesLength;
//read length of title
ASSERT_CAN_READ_BYTE_COUNT(sizeof(titleBytesLength));
titleBytesLength = *(int*)([blorData bytes] + currentByteOffset);
titleBytesLength = CFSwapInt32BigToHost(titleBytesLength);
currentByteOffset += sizeof(titleBytesLength);
//read/decrypt title
ASSERT_CAN_READ_BYTE_COUNT(titleBytesLength);
[self decryptNextBytesOfLength:titleBytesLength];
NSData *titleData = [NSData dataWithBytesNoCopy:[blorData mutableBytes] + currentByteOffset length:titleBytesLength freeWhenDone:NO];
NSString *titleString = [[NSString alloc] initWithData:titleData encoding:NSUnicodeStringEncoding];
currentByteOffset += titleBytesLength;
int bodyBufferBytesLength, bodyBytesLength;
//read lengths of body
ASSERT_CAN_READ_BYTE_COUNT(sizeof(bodyBufferBytesLength));
bodyBufferBytesLength = *(int*)([blorData bytes] + currentByteOffset);
bodyBufferBytesLength = CFSwapInt32BigToHost(bodyBufferBytesLength);
currentByteOffset += sizeof(bodyBufferBytesLength);
ASSERT_CAN_READ_BYTE_COUNT(sizeof(bodyBytesLength));
bodyBytesLength = *(int*)([blorData bytes] + currentByteOffset);
bodyBytesLength = CFSwapInt32BigToHost(bodyBytesLength);
currentByteOffset += sizeof(bodyBytesLength);
//read/decrypt body
ASSERT_CAN_READ_BYTE_COUNT(bodyBytesLength);
[self decryptNextBytesOfLength:bodyBytesLength];
NSData *bodyData = [NSData dataWithBytesNoCopy:[blorData mutableBytes] + currentByteOffset length:bodyBytesLength freeWhenDone:NO];
NSString *bodyString = [[NSString alloc] initWithData:bodyData encoding:NSUnicodeStringEncoding];
currentByteOffset += bodyBufferBytesLength;
//do we need to assert bodyBufferBytesLength, too?
//create new note and return it
NSMutableAttributedString *attributedBody = [[NSMutableAttributedString alloc] initWithString:bodyString
attributes:[[GlobalPrefs defaultPrefs] noteBodyAttributes]];
[attributedBody addLinkAttributesForRange:NSMakeRange(0, [attributedBody length])];
NoteObject *note = [[NoteObject alloc] initWithNoteBody:attributedBody title:titleString uniqueFilename:nil format:SingleDatabaseFormat];
[bodyString release];
[attributedBody release];
[titleString release];
successfullyReadNoteCount++;
return [note autorelease];
}
@end