MYKeychain.h
author snej@snej-mbp.mtv.corp.google.com
Tue Apr 07 10:56:58 2009 -0700 (2009-04-07)
changeset 2 8982b8fada63
parent 1 60e4cbbb5128
child 3 1dfe820d7ebe
permissions -rw-r--r--
More work, mostly on documentation.
     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, MYKeyPair, 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 /** Imports a certificate into the keychain, given its external representation. */
    60 - (MYCertificate*) importCertificate: (NSData*)data;
    61 
    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;
    66 
    67 /** Enumerates all certificates in the keychain. */
    68 - (NSEnumerator*) enumerateCertificates;
    69 
    70 #pragma mark KEY-PAIRS:
    71 
    72 /** Generates a new RSA key-pair and adds both keys to the keychain.
    73     This is very slow -- it may take seconds, depending on the key size, CPU speed,
    74     and other random factors. You may want to start some kind of progress indicator before
    75     calling this method, so the user doesn't think the app has locked up!
    76     @param keySize  The RSA key length in bits. Must be a power of two. Longer keys are harder
    77         to break, but operate more slowly and generate larger signatures.
    78         2048 is a good default choice. You could use 1024 if the data and signatures won't need
    79         to stay secure for years; or you could use 4096 if you're extremely paranoid. */
    80 - (MYKeyPair*) generateRSAKeyPairOfSize: (unsigned)keySize;
    81 
    82 /** Looks up an existing key-pair whose public key has the given digest.
    83     Returns nil if there is no such key-pair in the keychain.
    84     (This method does not look for public keys embedded in certificates, only 'bare' keys.) */
    85 - (MYKeyPair*) keyPairWithDigest: (MYSHA1Digest*)pubKeyDigest;
    86 
    87 /** Enumerates all key-pairs in the keychain.
    88     (This method does not find keys embedded in certificates, only 'bare' keys.) */
    89 - (NSEnumerator*) enumerateKeyPairs;
    90 
    91 
    92 #pragma mark -
    93 #pragma mark METHODS NOT SUPPORTED ON IPHONE:
    94 
    95 
    96 /** @name Mac-Only
    97  *  Functionality not available on iPhone. 
    98  */
    99 //@{
   100 #if !TARGET_OS_IPHONE
   101 
   102 /** Enumerates all public keys in the keychain that have the given alias string. */
   103 - (NSEnumerator*) symmetricKeysWithAlias: (NSString*)alias;
   104 
   105 /** Enumerates all public keys in the keychain that have the given alias string. */
   106 - (NSEnumerator*) publicKeysWithAlias: (NSString*)alias;
   107 
   108 /** Imports a key-pair into the keychain, given the external representations
   109     of both the public and private keys.
   110     Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
   111     the passphrase. */
   112 - (MYKeyPair*) importPublicKey: (NSData*)pubKeyData 
   113                     privateKey: (NSData*)privKeyData;
   114 
   115 /** Imports a key-pair into the keychain, given the external representations
   116     of both the public and private keys.
   117     Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
   118     the passphrase. You can specify the title and prompt message for this alert panel. */
   119 - (MYKeyPair*) importPublicKey: (NSData*)pubKeyData 
   120                     privateKey: (NSData*)privKeyData
   121                     alertTitle: (NSString*)title
   122                    alertPrompt: (NSString*)prompt;
   123 
   124 /** Imports a certificate into the keychain, given its external representation. */
   125 - (MYCertificate*) importCertificate: (NSData*)data
   126                                 type: (CSSM_CERT_TYPE) type
   127                             encoding: (CSSM_CERT_ENCODING) encoding;
   128 
   129 //@}
   130 #endif
   131 
   132 
   133 
   134 /** @name Expert (Mac-Only)
   135  *  Advanced functionality, not available on iPhone. 
   136  */
   137 //@{
   138 #if !TARGET_OS_IPHONE
   139 
   140 /** Creates a MYKeychain for an existing SecKeychainRef. */
   141 - (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
   142 
   143 /** Opens a keychain file. */
   144 + (MYKeychain*) openKeychainAtPath: (NSString*)path;
   145 
   146 /** Creates a new keychain file. */
   147 + (MYKeychain*) createKeychainAtPath: (NSString*)path
   148                         withPassword: (NSString*)password;
   149 
   150 /** Closes and deletes the keychain's file. You should not use this object any more. */
   151 - (BOOL) deleteKeychainFile;
   152 
   153 /** Returns the underlying SecKeychainRef for this keychain.
   154     This will be NULL for the special allKeychains instance, because it doesn't
   155     represent a single specific keychain. To handle that case, use the
   156     keychainRefOrDefault property instead. */
   157 @property (readonly) SecKeychainRef keychainRef;
   158 
   159 /** Returns the underlying SecKeychainRef for this keychain.
   160     The special allKeychains instance returns a reference to the default keychain,
   161     as a convenience. */
   162 @property (readonly) SecKeychainRef keychainRefOrDefault;
   163 
   164 /** The path of this keychain's file. */
   165 @property (readonly) NSString* path;
   166 
   167 /** The underlying CSSM storage handle; used when calling CSSM APIs. */
   168 @property (readonly) CSSM_CSP_HANDLE CSPHandle;
   169 
   170 #endif
   171 //@}
   172 
   173 @end
   174