MYKeychain.h
author snej@snej.local
Thu Apr 09 21:36:21 2009 -0700 (2009-04-09)
changeset 4 f4709533c816
parent 3 1dfe820d7ebe
child 5 b2e360b78189
permissions -rw-r--r--
* Certificate signing!!!
* MYIdentity class.
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@4
    59
/** Adds a certificate to this keychain. (It must not already belong to a keychain.) */
snej@4
    60
- (BOOL) addCertificate: (MYCertificate*)certificate;
snej@4
    61
snej@2
    62
/** Imports a certificate into the keychain, given its external representation. */
snej@2
    63
- (MYCertificate*) importCertificate: (NSData*)data;
snej@0
    64
snej@0
    65
/** Looks up an existing certificate with the given public-key digest.
snej@1
    66
    Returns nil if there is no such certificate in the keychain.
snej@1
    67
    (This method only looks for keys embedded in certificates, not 'bare' public keys.) */
snej@0
    68
- (MYCertificate*) certificateWithDigest: (MYSHA1Digest*)pubKeyDigest;
snej@0
    69
snej@0
    70
/** Enumerates all certificates in the keychain. */
snej@0
    71
- (NSEnumerator*) enumerateCertificates;
snej@0
    72
snej@4
    73
/** Enumerates all identities in the keychain. */
snej@4
    74
- (NSEnumerator*) enumerateIdentities;
snej@4
    75
snej@0
    76
#pragma mark KEY-PAIRS:
snej@0
    77
snej@0
    78
/** Generates a new RSA key-pair and adds both keys to the keychain.
snej@1
    79
    This is very slow -- it may take seconds, depending on the key size, CPU speed,
snej@1
    80
    and other random factors. You may want to start some kind of progress indicator before
snej@0
    81
    calling this method, so the user doesn't think the app has locked up!
snej@0
    82
    @param keySize  The RSA key length in bits. Must be a power of two. Longer keys are harder
snej@0
    83
        to break, but operate more slowly and generate larger signatures.
snej@0
    84
        2048 is a good default choice. You could use 1024 if the data and signatures won't need
snej@0
    85
        to stay secure for years; or you could use 4096 if you're extremely paranoid. */
snej@3
    86
- (MYPrivateKey*) generateRSAKeyPairOfSize: (unsigned)keySize;
snej@0
    87
snej@2
    88
/** Looks up an existing key-pair whose public key has the given digest.
snej@2
    89
    Returns nil if there is no such key-pair in the keychain.
snej@2
    90
    (This method does not look for public keys embedded in certificates, only 'bare' keys.) */
snej@3
    91
- (MYPrivateKey*) privateKeyWithDigest: (MYSHA1Digest*)pubKeyDigest;
snej@0
    92
snej@2
    93
/** Enumerates all key-pairs in the keychain.
snej@2
    94
    (This method does not find keys embedded in certificates, only 'bare' keys.) */
snej@3
    95
- (NSEnumerator*) enumeratePrivateKeys;
snej@0
    96
snej@0
    97
snej@0
    98
#pragma mark -
snej@0
    99
#pragma mark METHODS NOT SUPPORTED ON IPHONE:
snej@0
   100
snej@0
   101
snej@2
   102
/** @name Mac-Only
snej@2
   103
 *  Functionality not available on iPhone. 
snej@2
   104
 */
snej@2
   105
//@{
snej@0
   106
#if !TARGET_OS_IPHONE
snej@0
   107
snej@2
   108
/** Enumerates all public keys in the keychain that have the given alias string. */
snej@2
   109
- (NSEnumerator*) symmetricKeysWithAlias: (NSString*)alias;
snej@2
   110
snej@2
   111
/** Enumerates all public keys in the keychain that have the given alias string. */
snej@2
   112
- (NSEnumerator*) publicKeysWithAlias: (NSString*)alias;
snej@2
   113
snej@2
   114
/** Imports a key-pair into the keychain, given the external representations
snej@2
   115
    of both the public and private keys.
snej@2
   116
    Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
snej@2
   117
    the passphrase. */
snej@3
   118
- (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
snej@4
   119
                       privateKey: (NSData*)privKeyData;
snej@2
   120
snej@2
   121
/** Imports a key-pair into the keychain, given the external representations
snej@2
   122
    of both the public and private keys.
snej@2
   123
    Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
snej@2
   124
    the passphrase. You can specify the title and prompt message for this alert panel. */
snej@3
   125
- (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
snej@4
   126
                       privateKey: (NSData*)privKeyData
snej@4
   127
                       alertTitle: (NSString*)title
snej@4
   128
                      alertPrompt: (NSString*)prompt;
snej@2
   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