snej@0
|
1 |
//
|
snej@0
|
2 |
// MYKeychain.h
|
snej@0
|
3 |
// MYCrypto
|
snej@0
|
4 |
//
|
snej@0
|
5 |
// Created by Jens Alfke on 3/23/09.
|
snej@0
|
6 |
// Copyright 2009 Jens Alfke. All rights reserved.
|
snej@0
|
7 |
//
|
snej@0
|
8 |
|
snej@0
|
9 |
#import <Foundation/Foundation.h>
|
snej@2
|
10 |
#import "MYCryptoConfig.h"
|
snej@3
|
11 |
@class MYSymmetricKey, MYPublicKey, MYPrivateKey, MYCertificate, MYSHA1Digest;
|
snej@0
|
12 |
|
snej@0
|
13 |
|
snej@0
|
14 |
/** A Keychain, a secure database of cryptographic keys.
|
snej@0
|
15 |
This class wraps the Security framework's SecKeychain API. */
|
snej@0
|
16 |
@interface MYKeychain : NSObject
|
snej@0
|
17 |
{
|
snej@1
|
18 |
@private
|
snej@2
|
19 |
#if !MYCRYPTO_USE_IPHONE_API
|
snej@0
|
20 |
SecKeychainRef _keychain;
|
snej@0
|
21 |
#endif
|
snej@0
|
22 |
}
|
snej@0
|
23 |
|
snej@0
|
24 |
/** Returns a MYKeychain instance representing the user's default keychain.
|
snej@0
|
25 |
This is the instance you'll usually want to add keys to. */
|
snej@0
|
26 |
+ (MYKeychain*) defaultKeychain;
|
snej@0
|
27 |
|
snej@0
|
28 |
/** Returns a MYKeychain instance representing the aggregate of all open keychains.
|
snej@0
|
29 |
This is the instance you'll usually want to search for keys with. */
|
snej@0
|
30 |
+ (MYKeychain*) allKeychains;
|
snej@0
|
31 |
|
snej@0
|
32 |
#pragma mark SYMMETRIC KEYS:
|
snej@0
|
33 |
|
snej@1
|
34 |
/** Randomly generates a new symmetric key, using the given algorithm and key-size in bits.
|
snej@1
|
35 |
The key is persistently added to this keychain. */
|
snej@0
|
36 |
- (MYSymmetricKey*) generateSymmetricKeyOfSize: (unsigned)keySizeInBits
|
snej@0
|
37 |
algorithm: (uint32_t/*CCAlgorithm*/)algorithm;
|
snej@0
|
38 |
|
snej@1
|
39 |
/** Enumerates all of the symmetric keys in the keychain, as MYSymmetricKey objects. */
|
snej@0
|
40 |
- (NSEnumerator*) enumerateSymmetricKeys;
|
snej@0
|
41 |
|
snej@0
|
42 |
#pragma mark PUBLIC KEYS:
|
snej@0
|
43 |
|
snej@2
|
44 |
/** Imports a public key into the keychain, given its external representation
|
snej@2
|
45 |
(as generated by -[MYPublicKey keyData].) */
|
snej@2
|
46 |
- (MYPublicKey*) importPublicKey: (NSData*)keyData;
|
snej@2
|
47 |
|
snej@0
|
48 |
/** Looks up an existing public key with the given digest.
|
snej@0
|
49 |
Returns nil if there is no such key in the keychain.
|
snej@0
|
50 |
(This method does not look for keys embedded in certificates, only 'bare' keys.) */
|
snej@0
|
51 |
- (MYPublicKey*) publicKeyWithDigest: (MYSHA1Digest*)pubKeyDigest;
|
snej@0
|
52 |
|
snej@0
|
53 |
/** Enumerates all public keys in the keychain.
|
snej@0
|
54 |
(This method does not find keys embedded in certificates, only 'bare' keys.) */
|
snej@0
|
55 |
- (NSEnumerator*) enumeratePublicKeys;
|
snej@0
|
56 |
|
snej@2
|
57 |
#pragma mark CERTIFICATES:
|
snej@0
|
58 |
|
snej@2
|
59 |
/** Imports a certificate into the keychain, given its external representation. */
|
snej@2
|
60 |
- (MYCertificate*) importCertificate: (NSData*)data;
|
snej@0
|
61 |
|
snej@0
|
62 |
/** Looks up an existing certificate with the given public-key digest.
|
snej@1
|
63 |
Returns nil if there is no such certificate in the keychain.
|
snej@1
|
64 |
(This method only looks for keys embedded in certificates, not 'bare' public keys.) */
|
snej@0
|
65 |
- (MYCertificate*) certificateWithDigest: (MYSHA1Digest*)pubKeyDigest;
|
snej@0
|
66 |
|
snej@0
|
67 |
/** Enumerates all certificates in the keychain. */
|
snej@0
|
68 |
- (NSEnumerator*) enumerateCertificates;
|
snej@0
|
69 |
|
snej@4
|
70 |
/** Enumerates all identities in the keychain. */
|
snej@4
|
71 |
- (NSEnumerator*) enumerateIdentities;
|
snej@4
|
72 |
|
snej@0
|
73 |
#pragma mark KEY-PAIRS:
|
snej@0
|
74 |
|
snej@0
|
75 |
/** Generates a new RSA key-pair and adds both keys to the keychain.
|
snej@1
|
76 |
This is very slow -- it may take seconds, depending on the key size, CPU speed,
|
snej@1
|
77 |
and other random factors. You may want to start some kind of progress indicator before
|
snej@0
|
78 |
calling this method, so the user doesn't think the app has locked up!
|
snej@0
|
79 |
@param keySize The RSA key length in bits. Must be a power of two. Longer keys are harder
|
snej@0
|
80 |
to break, but operate more slowly and generate larger signatures.
|
snej@0
|
81 |
2048 is a good default choice. You could use 1024 if the data and signatures won't need
|
snej@0
|
82 |
to stay secure for years; or you could use 4096 if you're extremely paranoid. */
|
snej@3
|
83 |
- (MYPrivateKey*) generateRSAKeyPairOfSize: (unsigned)keySize;
|
snej@0
|
84 |
|
snej@2
|
85 |
/** Looks up an existing key-pair whose public key has the given digest.
|
snej@2
|
86 |
Returns nil if there is no such key-pair in the keychain.
|
snej@2
|
87 |
(This method does not look for public keys embedded in certificates, only 'bare' keys.) */
|
snej@3
|
88 |
- (MYPrivateKey*) privateKeyWithDigest: (MYSHA1Digest*)pubKeyDigest;
|
snej@0
|
89 |
|
snej@2
|
90 |
/** Enumerates all key-pairs in the keychain.
|
snej@2
|
91 |
(This method does not find keys embedded in certificates, only 'bare' keys.) */
|
snej@3
|
92 |
- (NSEnumerator*) enumeratePrivateKeys;
|
snej@0
|
93 |
|
snej@0
|
94 |
|
snej@0
|
95 |
#pragma mark -
|
snej@0
|
96 |
#pragma mark METHODS NOT SUPPORTED ON IPHONE:
|
snej@0
|
97 |
|
snej@0
|
98 |
|
snej@2
|
99 |
/** @name Mac-Only
|
snej@2
|
100 |
* Functionality not available on iPhone.
|
snej@2
|
101 |
*/
|
snej@2
|
102 |
//@{
|
snej@0
|
103 |
#if !TARGET_OS_IPHONE
|
snej@0
|
104 |
|
snej@2
|
105 |
/** Enumerates all public keys in the keychain that have the given alias string. */
|
snej@2
|
106 |
- (NSEnumerator*) symmetricKeysWithAlias: (NSString*)alias;
|
snej@2
|
107 |
|
snej@2
|
108 |
/** Enumerates all public keys in the keychain that have the given alias string. */
|
snej@2
|
109 |
- (NSEnumerator*) publicKeysWithAlias: (NSString*)alias;
|
snej@2
|
110 |
|
snej@2
|
111 |
/** Imports a key-pair into the keychain, given the external representations
|
snej@2
|
112 |
of both the public and private keys.
|
snej@2
|
113 |
Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
|
snej@2
|
114 |
the passphrase. */
|
snej@3
|
115 |
- (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData
|
snej@4
|
116 |
privateKey: (NSData*)privKeyData;
|
snej@2
|
117 |
|
snej@2
|
118 |
/** Imports a key-pair into the keychain, given the external representations
|
snej@2
|
119 |
of both the public and private keys.
|
snej@2
|
120 |
Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
|
snej@2
|
121 |
the passphrase. You can specify the title and prompt message for this alert panel. */
|
snej@3
|
122 |
- (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData
|
snej@4
|
123 |
privateKey: (NSData*)privKeyData
|
snej@4
|
124 |
alertTitle: (NSString*)title
|
snej@4
|
125 |
alertPrompt: (NSString*)prompt;
|
snej@2
|
126 |
|
snej@5
|
127 |
/** Adds a certificate to this keychain. (It must not already belong to a keychain.) */
|
snej@5
|
128 |
- (BOOL) addCertificate: (MYCertificate*)certificate;
|
snej@5
|
129 |
|
snej@2
|
130 |
/** Imports a certificate into the keychain, given its external representation. */
|
snej@2
|
131 |
- (MYCertificate*) importCertificate: (NSData*)data
|
snej@2
|
132 |
type: (CSSM_CERT_TYPE) type
|
snej@2
|
133 |
encoding: (CSSM_CERT_ENCODING) encoding;
|
snej@2
|
134 |
|
snej@2
|
135 |
//@}
|
snej@2
|
136 |
#endif
|
snej@2
|
137 |
|
snej@2
|
138 |
|
snej@2
|
139 |
|
snej@2
|
140 |
/** @name Expert (Mac-Only)
|
snej@2
|
141 |
* Advanced functionality, not available on iPhone.
|
snej@2
|
142 |
*/
|
snej@2
|
143 |
//@{
|
snej@2
|
144 |
#if !TARGET_OS_IPHONE
|
snej@0
|
145 |
|
snej@0
|
146 |
/** Creates a MYKeychain for an existing SecKeychainRef. */
|
snej@0
|
147 |
- (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
|
snej@0
|
148 |
|
snej@0
|
149 |
/** Opens a keychain file. */
|
snej@0
|
150 |
+ (MYKeychain*) openKeychainAtPath: (NSString*)path;
|
snej@0
|
151 |
|
snej@0
|
152 |
/** Creates a new keychain file. */
|
snej@0
|
153 |
+ (MYKeychain*) createKeychainAtPath: (NSString*)path
|
snej@0
|
154 |
withPassword: (NSString*)password;
|
snej@0
|
155 |
|
snej@0
|
156 |
/** Closes and deletes the keychain's file. You should not use this object any more. */
|
snej@0
|
157 |
- (BOOL) deleteKeychainFile;
|
snej@0
|
158 |
|
snej@0
|
159 |
/** Returns the underlying SecKeychainRef for this keychain.
|
snej@0
|
160 |
This will be NULL for the special allKeychains instance, because it doesn't
|
snej@0
|
161 |
represent a single specific keychain. To handle that case, use the
|
snej@0
|
162 |
keychainRefOrDefault property instead. */
|
snej@0
|
163 |
@property (readonly) SecKeychainRef keychainRef;
|
snej@0
|
164 |
|
snej@0
|
165 |
/** Returns the underlying SecKeychainRef for this keychain.
|
snej@0
|
166 |
The special allKeychains instance returns a reference to the default keychain,
|
snej@0
|
167 |
as a convenience. */
|
snej@0
|
168 |
@property (readonly) SecKeychainRef keychainRefOrDefault;
|
snej@0
|
169 |
|
snej@0
|
170 |
/** The path of this keychain's file. */
|
snej@0
|
171 |
@property (readonly) NSString* path;
|
snej@0
|
172 |
|
snej@0
|
173 |
/** The underlying CSSM storage handle; used when calling CSSM APIs. */
|
snej@0
|
174 |
@property (readonly) CSSM_CSP_HANDLE CSPHandle;
|
snej@0
|
175 |
|
snej@2
|
176 |
#endif
|
snej@2
|
177 |
//@}
|
snej@0
|
178 |
|
snej@0
|
179 |
@end
|
snej@0
|
180 |
|