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