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