-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoPassphrase.m
186 lines (156 loc) · 5.28 KB
/
CryptoPassphrase.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
/*
* Copyright (c) 2016 - 2024 Jonathan Schleifer <[email protected]>
*
* https://fl.nil.im/cryptopassphrase
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <unistd.h>
#import "CryptoPassphrase.h"
#import "NewPasswordGenerator.h"
#import "LegacyPasswordGenerator.h"
OF_APPLICATION_DELEGATE(CryptoPassphrase)
static void
showHelp(OFStream *output, bool verbose)
{
[output writeFormat: @"Usage: %@ [-hlr] site\n",
[OFApplication programName]];
if (verbose)
[output writeString:
@"\n"
@"Options:\n"
@" -h --help Show this help\n"
@" -k --keyfile Use the specified key file\n"
@" -l --length Length for the derived password\n"
@" -L --legacy Use the legacy algorithm "
@"(compatible with scrypt-genpass)\n"
@" -r --repeat Repeat input\n"];
}
@implementation CryptoPassphrase
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
OFString *keyFilePath, *lengthString;
const OFOptionsParserOption options[] = {
{ 'h', @"help", 0, NULL, NULL },
{ 'k', @"keyfile", 1, NULL, &keyFilePath },
{ 'l', @"length", 1, NULL, &lengthString },
{ 'L', @"legacy", 0, &_legacy, NULL },
{ 'r', @"repeat", 0, &_repeat, NULL },
{ '\0', nil, 0, NULL, NULL }
};
OFOptionsParser *optionsParser =
[OFOptionsParser parserWithOptions: options];
OFUnichar option;
OFMutableData *keyFile = nil;
OFString *prompt;
const char *promptCString;
char *passphraseCString;
size_t passphraseLength;
OFSecureData *passphrase;
while ((option = [optionsParser nextOption]) != '\0') {
switch (option) {
case 'h':
showHelp(OFStdOut, true);
[OFApplication terminate];
break;
case ':':
if (optionsParser.lastLongOption != nil)
[OFStdErr writeFormat:
@"%@: Argument for option --%@ missing\n",
[OFApplication programName],
optionsParser.lastLongOption];
else
[OFStdErr writeFormat:
@"%@: Argument for option -%C missing\n",
[OFApplication programName],
optionsParser.lastOption];
[OFApplication terminateWithStatus: 1];
break;
case '?':
if (optionsParser.lastLongOption != nil)
[OFStdErr writeFormat:
@"%@: Unknown option: --%@\n",
[OFApplication programName],
optionsParser.lastLongOption];
else
[OFStdErr writeFormat:
@"%@: Unknown option: -%C\n",
[OFApplication programName],
optionsParser.lastOption];
[OFApplication terminateWithStatus: 1];
break;
}
}
if (optionsParser.remainingArguments.count != 1) {
showHelp(OFStdErr, false);
[OFApplication terminateWithStatus: 1];
}
id <PasswordGenerator> generator = (_legacy
? [LegacyPasswordGenerator generator]
: [NewPasswordGenerator generator]);
generator.site = optionsParser.remainingArguments.firstObject;
if (lengthString != nil) {
bool invalid = false;
@try {
unsigned long long length =
lengthString.unsignedLongLongValue;
if (length > SIZE_MAX)
@throw [OFOutOfRangeException exception];
generator.length = (size_t)length;
} @catch (OFInvalidFormatException *e) {
invalid = true;
} @catch (OFOutOfRangeException *e) {
invalid = true;
}
if (invalid) {
[OFStdErr writeFormat:
@"%@: Invalid length: %@\n",
[OFApplication programName], lengthString];
[OFApplication terminateWithStatus: 1];
}
}
prompt = [OFString stringWithFormat: @"Passphrase for site \"%@\": ",
generator.site];
promptCString = [prompt cStringWithEncoding: [OFLocale encoding]];
if (keyFilePath != nil)
keyFile = [OFMutableData dataWithContentsOfFile: keyFilePath];
passphraseCString = getpass(promptCString);
passphraseLength = strlen(passphraseCString);
@try {
passphrase = [OFSecureData dataWithCount: passphraseLength + 1
allowsSwappableMemory: true];
memcpy(passphrase.mutableItems, passphraseCString,
passphraseLength + 1);
} @finally {
OFZeroMemory(passphraseCString, passphraseLength);
}
if (_repeat) {
OFStringEncoding encoding = [OFLocale encoding];
prompt = [OFString stringWithFormat:
@"Repeat passphrase for site \"%@\": ", generator.site];
passphraseCString =
getpass([prompt cStringWithEncoding: encoding]);
if (strcmp(passphraseCString, passphrase.items) != 0) {
[OFStdErr writeString: @"Passphrases do not match!\n"];
[OFApplication terminateWithStatus: 1];
}
OFZeroMemory(passphraseCString, strlen(passphraseCString));
}
generator.keyFile = keyFile;
generator.passphrase = passphrase;
[generator derivePassword];
[OFStdOut writeBuffer: generator.output.items length: generator.length];
[OFStdOut writeBuffer: "\n" length: 1];
[OFApplication terminate];
}
@end