snej@0
|
1 |
//
|
snej@0
|
2 |
// MYCertificate.m
|
snej@0
|
3 |
// MYCrypto
|
snej@0
|
4 |
//
|
snej@0
|
5 |
// Created by Jens Alfke on 3/26/09.
|
snej@0
|
6 |
// Copyright 2009 Jens Alfke. All rights reserved.
|
snej@0
|
7 |
//
|
snej@0
|
8 |
|
snej@0
|
9 |
#import "MYCertificate.h"
|
snej@0
|
10 |
#import "MYCrypto_Private.h"
|
snej@0
|
11 |
|
snej@2
|
12 |
#if !MYCRYPTO_USE_IPHONE_API
|
snej@0
|
13 |
|
snej@0
|
14 |
|
snej@0
|
15 |
@implementation MYCertificate
|
snej@0
|
16 |
|
snej@0
|
17 |
|
snej@0
|
18 |
/** Creates a MYCertificate object for an existing Keychain certificate reference. */
|
snej@0
|
19 |
- (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
|
snej@0
|
20 |
self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef];
|
snej@0
|
21 |
if (self) {
|
snej@0
|
22 |
_certificateRef = certificateRef; // superclass has already CFRetained it
|
snej@0
|
23 |
}
|
snej@0
|
24 |
return self;
|
snej@0
|
25 |
}
|
snej@0
|
26 |
|
snej@0
|
27 |
/** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */
|
snej@0
|
28 |
- (id) initWithCertificateData: (NSData*)data
|
snej@0
|
29 |
type: (CSSM_CERT_TYPE) type
|
snej@0
|
30 |
encoding: (CSSM_CERT_ENCODING) encoding
|
snej@0
|
31 |
{
|
snej@0
|
32 |
Assert(data);
|
snej@0
|
33 |
CSSM_DATA cssmData = {.Data=(void*)data.bytes, .Length=data.length};
|
snej@0
|
34 |
SecCertificateRef certificateRef = NULL;
|
snej@0
|
35 |
if (!check(SecCertificateCreateFromData(&cssmData, type, encoding, &certificateRef),
|
snej@0
|
36 |
@"SecCertificateCreateFromData")) {
|
snej@0
|
37 |
[self release];
|
snej@0
|
38 |
return nil;
|
snej@0
|
39 |
}
|
snej@0
|
40 |
self = [self initWithCertificateRef: certificateRef];
|
snej@0
|
41 |
CFRelease(certificateRef);
|
snej@0
|
42 |
return self;
|
snej@0
|
43 |
}
|
snej@0
|
44 |
|
snej@0
|
45 |
- (id) initWithCertificateData: (NSData*)data {
|
snej@0
|
46 |
return [self initWithCertificateData: data
|
snej@0
|
47 |
type: CSSM_CERT_X_509v3
|
snej@0
|
48 |
encoding: CSSM_CERT_ENCODING_BER];
|
snej@0
|
49 |
}
|
snej@0
|
50 |
|
snej@0
|
51 |
+ (MYCertificate*) preferredCertificateForName: (NSString*)name {
|
snej@0
|
52 |
SecCertificateRef certRef = NULL;
|
snej@0
|
53 |
if (!check(SecCertificateCopyPreference((CFStringRef)name, 0, &certRef),
|
snej@0
|
54 |
@"SecCertificateCopyPreference"))
|
snej@0
|
55 |
return nil;
|
snej@0
|
56 |
return [[[MYCertificate alloc] initWithCertificateRef: certRef] autorelease];
|
snej@0
|
57 |
}
|
snej@0
|
58 |
|
snej@0
|
59 |
- (BOOL) setPreferredCertificateForName: (NSString*)name {
|
snej@0
|
60 |
return check(SecCertificateSetPreference(_certificateRef, (CFStringRef)name, 0, NULL),
|
snej@0
|
61 |
@"SecCertificateSetPreference");
|
snej@0
|
62 |
}
|
snej@0
|
63 |
|
snej@0
|
64 |
@synthesize certificateRef=_certificateRef;
|
snej@0
|
65 |
|
snej@0
|
66 |
- (NSData*) certificateData {
|
snej@0
|
67 |
CSSM_DATA cssmData;
|
snej@0
|
68 |
if (!check(SecCertificateGetData(_certificateRef, &cssmData),
|
snej@0
|
69 |
@"SecCertificateGetData"))
|
snej@0
|
70 |
return nil;
|
snej@0
|
71 |
return [NSData dataWithBytes: cssmData.Data length: cssmData.Length];
|
snej@0
|
72 |
}
|
snej@0
|
73 |
|
snej@0
|
74 |
- (MYPublicKey*) publicKey {
|
snej@0
|
75 |
SecKeyRef keyRef = NULL;
|
snej@0
|
76 |
if (!check(SecCertificateCopyPublicKey(_certificateRef, &keyRef),
|
snej@0
|
77 |
@"SecCertificateCopyPublicKey") || !keyRef)
|
snej@0
|
78 |
return nil;
|
snej@0
|
79 |
MYPublicKey *key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease];
|
snej@0
|
80 |
CFRelease(keyRef);
|
snej@0
|
81 |
return key;
|
snej@0
|
82 |
}
|
snej@0
|
83 |
|
snej@0
|
84 |
- (NSString*) commonName {
|
snej@0
|
85 |
CFStringRef name = NULL;
|
snej@0
|
86 |
if (!check(SecCertificateCopyCommonName(_certificateRef, &name),
|
snej@0
|
87 |
@"SecCertificateCopyCommonName") || !name)
|
snej@0
|
88 |
return nil;
|
snej@0
|
89 |
return [(id)CFMakeCollectable(name) autorelease];
|
snej@0
|
90 |
}
|
snej@0
|
91 |
|
snej@0
|
92 |
- (NSArray*) emailAddresses {
|
snej@0
|
93 |
CFArrayRef addrs = NULL;
|
snej@0
|
94 |
if (!check(SecCertificateCopyEmailAddresses(_certificateRef, &addrs),
|
snej@0
|
95 |
@"SecCertificateCopyEmailAddresses") || !addrs)
|
snej@0
|
96 |
return nil;
|
snej@0
|
97 |
return [(id)CFMakeCollectable(addrs) autorelease];
|
snej@0
|
98 |
}
|
snej@0
|
99 |
|
snej@0
|
100 |
|
snej@0
|
101 |
@end
|
snej@0
|
102 |
|
snej@0
|
103 |
|
snej@2
|
104 |
#endif !MYCRYPTO_USE_IPHONE_API
|