MYKeychain.h
author Jens Alfke <jens@mooseyard.com>
Fri Aug 07 11:24:53 2009 -0700 (2009-08-07)
changeset 28 54b373aa65ab
parent 16 c409dbc4f068
permissions -rw-r--r--
Fixed iPhone OS build. (issue 3)
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
jens@16
   105
/** Sets whether the keychain is allowed to pop up panels to interact with the user,
jens@16
   106
    for example to ask for permission to access keys. If user interaction is not
jens@16
   107
    allowed, all such requests will fail. */
jens@16
   108
+ (void) setUserInteractionAllowed: (BOOL)allowed;
jens@16
   109
snej@2
   110
/** Enumerates all public keys in the keychain that have the given alias string. */
snej@2
   111
- (NSEnumerator*) symmetricKeysWithAlias: (NSString*)alias;
snej@2
   112
snej@2
   113
/** Enumerates all public keys in the keychain that have the given alias string. */
snej@2
   114
- (NSEnumerator*) publicKeysWithAlias: (NSString*)alias;
snej@2
   115
jens@26
   116
- (NSEnumerator*) enumerateIdentitiesWithKeyUsage: (CSSM_KEYUSE)keyUsage;
jens@26
   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. */
snej@3
   122
- (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
snej@4
   123
                       privateKey: (NSData*)privKeyData;
snej@2
   124
snej@2
   125
/** Imports a key-pair into the keychain, given the external representations
snej@2
   126
    of both the public and private keys.
snej@2
   127
    Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
snej@2
   128
    the passphrase. You can specify the title and prompt message for this alert panel. */
snej@3
   129
- (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
snej@4
   130
                       privateKey: (NSData*)privKeyData
snej@4
   131
                       alertTitle: (NSString*)title
snej@4
   132
                      alertPrompt: (NSString*)prompt;
snej@2
   133
snej@5
   134
/** Adds a certificate to this keychain. (It must not already belong to a keychain.) */
snej@5
   135
- (BOOL) addCertificate: (MYCertificate*)certificate;
snej@5
   136
snej@2
   137
/** Imports a certificate into the keychain, given its external representation. */
snej@2
   138
- (MYCertificate*) importCertificate: (NSData*)data
snej@2
   139
                                type: (CSSM_CERT_TYPE) type
snej@2
   140
                            encoding: (CSSM_CERT_ENCODING) encoding;
snej@2
   141
snej@2
   142
//@}
jens@26
   143
#else
jens@26
   144
/** @name iPhone-Only
jens@26
   145
 *  Functionality only available on iPhone. 
jens@26
   146
 */
jens@26
   147
//@{
jens@26
   148
jens@26
   149
- (BOOL) removeAllCertificates;
jens@26
   150
- (BOOL) removeAllKeys;
jens@26
   151
jens@26
   152
//@}
snej@2
   153
#endif
snej@2
   154
snej@2
   155
snej@2
   156
snej@2
   157
/** @name Expert (Mac-Only)
snej@2
   158
 *  Advanced functionality, not available on iPhone. 
snej@2
   159
 */
snej@2
   160
//@{
snej@2
   161
#if !TARGET_OS_IPHONE
snej@0
   162
snej@0
   163
/** Creates a MYKeychain for an existing SecKeychainRef. */
snej@0
   164
- (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
snej@0
   165
snej@0
   166
/** Opens a keychain file. */
snej@0
   167
+ (MYKeychain*) openKeychainAtPath: (NSString*)path;
snej@0
   168
snej@0
   169
/** Creates a new keychain file. */
snej@0
   170
+ (MYKeychain*) createKeychainAtPath: (NSString*)path
snej@0
   171
                        withPassword: (NSString*)password;
snej@0
   172
snej@0
   173
/** Closes and deletes the keychain's file. You should not use this object any more. */
snej@0
   174
- (BOOL) deleteKeychainFile;
snej@0
   175
snej@0
   176
/** Returns the underlying SecKeychainRef for this keychain.
snej@0
   177
    This will be NULL for the special allKeychains instance, because it doesn't
snej@0
   178
    represent a single specific keychain. To handle that case, use the
snej@0
   179
    keychainRefOrDefault property instead. */
snej@0
   180
@property (readonly) SecKeychainRef keychainRef;
snej@0
   181
snej@0
   182
/** Returns the underlying SecKeychainRef for this keychain.
snej@0
   183
    The special allKeychains instance returns a reference to the default keychain,
snej@0
   184
    as a convenience. */
snej@0
   185
@property (readonly) SecKeychainRef keychainRefOrDefault;
snej@0
   186
snej@0
   187
/** The path of this keychain's file. */
snej@0
   188
@property (readonly) NSString* path;
snej@0
   189
snej@0
   190
/** The underlying CSSM storage handle; used when calling CSSM APIs. */
snej@0
   191
@property (readonly) CSSM_CSP_HANDLE CSPHandle;
snej@0
   192
snej@2
   193
#endif
snej@2
   194
//@}
snej@0
   195
snej@0
   196
@end
snej@0
   197