-
Notifications
You must be signed in to change notification settings - Fork 4
/
GPGSubkey.m
323 lines (265 loc) · 8.25 KB
/
GPGSubkey.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
// GPGSubkey.m
// MacGPGME
//
// Created by davelopper at users.sourceforge.net on Jun 08 2003.
//
//
// Copyright (C) 2001-2006 Mac GPG Project.
//
// This code is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This code is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, visit <http://www.gnu.org/> or write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.
//
// More info at <http://macgpg.sourceforge.net/>
//
#include <MacGPGME/GPGSubkey.h>
#include <MacGPGME/GPGInternals.h>
#include <Foundation/Foundation.h>
#define _subkey ((gpgme_subkey_t)_internalRepresentation)
@implementation GPGSubkey
- (id) retain
{
// See GPGKey.m for more information
[_key retain];
_refCount++;
return self;
}
- (oneway void) release
{
// See GPGKey.m for more information
if(_refCount > 0){
_refCount--;
[_key release];
}
else{
if(_refCount < 0)
NSLog(@"### GPGSubkey: _refCount < 0! (%d)", _refCount);
[super release];
}
}
- (GPGKey *) key
{
return _key;
}
- (BOOL) isKeyRevoked
{
return !!_subkey->revoked;
}
- (BOOL) isKeyInvalid
{
return !!_subkey->invalid;
}
- (BOOL) hasKeyExpired
{
// There is a bug in gpg/gpgme: the hasKeyExpired status is wrong!
// We need to check the expiration date.
BOOL hasKeyExpired = !!_subkey->expired;
if(!hasKeyExpired){
NSCalendarDate *expirationDate = [self expirationDate];
if(expirationDate != nil)
hasKeyExpired = ([expirationDate compare:[NSCalendarDate calendarDate]] == NSOrderedAscending);
}
return hasKeyExpired;
}
- (BOOL) isKeyDisabled
{
return !!_subkey->disabled;
}
- (BOOL) canEncrypt
{
return !!_subkey->can_encrypt;
}
- (BOOL) canSign
{
return !!_subkey->can_sign;
}
- (BOOL) canCertify
{
return !!_subkey->can_certify;
}
- (BOOL) canAuthenticate
{
return !!_subkey->can_authenticate;
}
- (BOOL) isSecret
{
return !!_subkey->secret;
}
- (GPGPublicKeyAlgorithm) algorithm
{
return _subkey->pubkey_algo;
}
- (unsigned int) length
{
return _subkey->length;
}
- (NSString *) keyID
{
return GPGStringFromChars(_subkey->keyid);
}
- (NSString *) fingerprint
{
return GPGStringFromChars(_subkey->fpr);
}
- (NSCalendarDate *) creationDate
{
/* The creation timestamp, -1 if invalid, 0 if not available. */
long timestamp = _subkey->timestamp;
if(timestamp <= 0L)
return nil;
else
return [NSCalendarDate dateWithTimeIntervalSince1970:timestamp];
}
- (NSCalendarDate *) expirationDate
{
long timestamp = _subkey->expires;
if(timestamp == 0L)
// Does not expire; shouldn't we return futureDate?
return nil;
else
return [NSCalendarDate dateWithTimeIntervalSince1970:timestamp];
}
- (NSArray *) subkeys
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (GPGValidity) ownerTrust
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return -1;
}
- (NSString *) ownerTrustDescription
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (GPGUserID *) primaryUserID
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSString *) issuerSerial
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSString *) issuerName
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSString *) chainID
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSString *) userID
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSArray *) userIDs
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSString *) name
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSString *) email
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (NSString *) comment
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (GPGValidity) validity
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return -1;
}
- (NSString *) validityDescription
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (GPGProtocol) supportedProtocol
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return -1;
}
- (NSString *) supportedProtocolDescription
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (GPGKey *) publicKey
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
- (GPGKey *) secretKey
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return nil;
}
@end
@implementation GPGSubkey(GPGInternals)
+ (BOOL) usesReferencesCount
{
return NO;
}
- (id) initWithInternalRepresentation:(void *)aPtr key:(GPGKey *)key
{
if(self = [self initWithInternalRepresentation:aPtr])
((GPGSubkey *)self)->_key = key; // Not retained
return self;
}
- (gpgme_key_t) gpgmeKey
{
[NSException raise:NSInternalInconsistencyException format:@"### Subkeys do not respond to %@", NSStringFromSelector(_cmd)];
return NULL;
}
- (NSDictionary *) dictionaryRepresentation
{
NSMutableDictionary *dictionaryRepresentation = [NSMutableDictionary dictionaryWithCapacity:11];
NSCalendarDate *aDate;
NSString *aString;
[dictionaryRepresentation setObject:[NSNumber numberWithInt:[self algorithm]] forKey:@"algo"];
aDate = [self creationDate];
if(aDate != nil)
[dictionaryRepresentation setObject:aDate forKey:@"created"];
aDate = [self expirationDate];
if(aDate != nil)
[dictionaryRepresentation setObject:aDate forKey:@"expire"];
[dictionaryRepresentation setObject:[NSNumber numberWithBool:[self isKeyDisabled]] forKey:@"disabled"];
[dictionaryRepresentation setObject:[NSNumber numberWithBool:[self hasKeyExpired]] forKey:@"expired"];
aString = [self fingerprint];
if(aString != nil)
[dictionaryRepresentation setObject:aString forKey:@"fpr"];
[dictionaryRepresentation setObject:[NSNumber numberWithBool:[self isKeyInvalid]] forKey:@"invalid"];
[dictionaryRepresentation setObject:[self keyID] forKey:@"keyid"];
[dictionaryRepresentation setObject:[self shortKeyID] forKey:@"shortkeyid"];
[dictionaryRepresentation setObject:[NSNumber numberWithUnsignedInt:[self length]] forKey:@"len"];
[dictionaryRepresentation setObject:[NSNumber numberWithBool:[self isKeyRevoked]] forKey:@"revoked"];
return dictionaryRepresentation;
}
@end