-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMDSAKeyGenerator.m
162 lines (114 loc) · 3.85 KB
/
XMDSAKeyGenerator.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
//
// CodexFab
//
// XMDSAKeyGenerator.m
//
// Licensed under CC Attribution License 3.0 <http://creativecommons.org/licenses/by/3.0/>
//
// Based on CocoaFob by Gleb Dolgich
// <http://github.com/gbd/cocoafob/tree/master>
//
// Created by Alex Clarke on 10/06/09.
// Copyright 2009 MachineCodex Software Australia. All rights reserved.
// <http://www.machinecodex.com>
#import "XMDSAKeyGenerator.h"
#import "NSString+PECrypt.h"
#import "NSData+PECrypt.h"
#import "NSString-Base64Extensions.h"
#import "XMArgumentKeys.h"
@implementation XMDSAKeyGenerator
@synthesize workingDirectory;
- (id) initWithWorkingDirectory:(NSString *)path
{
self = [super init];
if (self != nil) {
self.workingDirectory = path;
}
return self;
}
- (NSString *)uuid
{
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
NSString *uuid = [NSString stringWithString:(NSString *)
uuidStringRef];
CFRelease(uuidStringRef);
return uuid;
}
- (id) init {
// an alternative to the NSTemporaryDirectory
NSString *path = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
if ([paths count])
{
NSString *bundleName =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
NSString * guid = [self uuid];
path = [[[paths objectAtIndex:0] stringByAppendingPathComponent:bundleName] stringByAppendingPathComponent:guid];
}
return [self initWithWorkingDirectory:path];
}
- (void) opensslTaskWithArguments:(NSArray *)arguments {
NSString * launchPath = @"/usr/bin/";
NSString * taskName = @"openssl";
launchPath = [launchPath stringByAppendingString:taskName];
NSTask *task=[[[NSTask alloc] init]autorelease];
NSPipe *helpPipe=[NSPipe pipe];
NSPipe *outPipe=[NSPipe pipe];
[task setCurrentDirectoryPath:self.workingDirectory];
NSLog(@"%@", [task currentDirectoryPath]);
[task setStandardError:helpPipe];
[task setStandardOutput:outPipe];
[task setLaunchPath:launchPath];
[task setArguments:arguments];
NSLog(@"%@", task);
[task launch];
[task waitUntilExit];
int status = [task terminationStatus];
if (status ==0)
NSLog(@"Task succeeded.");
else
NSLog(@"Task failed.");
}
- (id) DSAParameters {
// openssl dsaparam -out dsaparam.pem 512
NSString * outFilePath = [self.workingDirectory stringByAppendingPathComponent:@"dsaparam.pem"];
NSArray * arguments = [NSArray arrayWithObjects:
@"dsaparam",
@"-out",
outFilePath,
@"512",
nil ];
[self opensslTaskWithArguments:arguments];
return [NSString stringWithContentsOfFile:outFilePath];
}
- (id) unencryptedDSAPrivateKeyFromParameters:(NSString *)parameters {
// openssl gendsa -out privkey.pem dsaparam.pem
NSString * inFilePath = [self.workingDirectory stringByAppendingPathComponent:@"dsaparam.pem"];
NSString * outFilePath = [self.workingDirectory stringByAppendingPathComponent:@"privkey.pem"];
NSArray * arguments = [NSArray arrayWithObjects:
@"gendsa",
@"-out",
outFilePath,
inFilePath,
nil ];
[self opensslTaskWithArguments:arguments];
return [NSString stringWithContentsOfFile:outFilePath];
}
- (id) DSAPublicKeyFromPrivateKey:(NSString *)privateKey {
// openssl dsa -in privkey.pem -pubout -out pubkey.pem
NSString * inFilePath = [self.workingDirectory stringByAppendingPathComponent:@"privkey.pem"];
NSString * outFilePath = [self.workingDirectory stringByAppendingPathComponent:@"pubkey.pem"];
NSArray * arguments = [NSArray arrayWithObjects:
@"dsa",
@"-in",
inFilePath,
@"-pubout",
@"-out",
outFilePath,
nil ];
[self opensslTaskWithArguments:arguments];
return [NSString stringWithContentsOfFile:outFilePath];
}
@end