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)
     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 /** 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 /** Enumerates all identities in the keychain. */
    71 - (NSEnumerator*) enumerateIdentities;
    72 
    73 #pragma mark KEY-PAIRS:
    74 
    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;
    84 
    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;
    89 
    90 /** Enumerates all key-pairs in the keychain.
    91     (This method does not find keys embedded in certificates, only 'bare' keys.) */
    92 - (NSEnumerator*) enumeratePrivateKeys;
    93 
    94 
    95 #pragma mark -
    96 #pragma mark METHODS NOT SUPPORTED ON IPHONE:
    97 
    98 
    99 /** @name Mac-Only
   100  *  Functionality not available on iPhone. 
   101  */
   102 //@{
   103 #if !TARGET_OS_IPHONE
   104 
   105 /** Sets whether the keychain is allowed to pop up panels to interact with the user,
   106     for example to ask for permission to access keys. If user interaction is not
   107     allowed, all such requests will fail. */
   108 + (void) setUserInteractionAllowed: (BOOL)allowed;
   109 
   110 /** Enumerates all public keys in the keychain that have the given alias string. */
   111 - (NSEnumerator*) symmetricKeysWithAlias: (NSString*)alias;
   112 
   113 /** Enumerates all public keys in the keychain that have the given alias string. */
   114 - (NSEnumerator*) publicKeysWithAlias: (NSString*)alias;
   115 
   116 - (NSEnumerator*) enumerateIdentitiesWithKeyUsage: (CSSM_KEYUSE)keyUsage;
   117 
   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. */
   122 - (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
   123                        privateKey: (NSData*)privKeyData;
   124 
   125 /** Imports a key-pair into the keychain, given the external representations
   126     of both the public and private keys.
   127     Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
   128     the passphrase. You can specify the title and prompt message for this alert panel. */
   129 - (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
   130                        privateKey: (NSData*)privKeyData
   131                        alertTitle: (NSString*)title
   132                       alertPrompt: (NSString*)prompt;
   133 
   134 /** Adds a certificate to this keychain. (It must not already belong to a keychain.) */
   135 - (BOOL) addCertificate: (MYCertificate*)certificate;
   136 
   137 /** Imports a certificate into the keychain, given its external representation. */
   138 - (MYCertificate*) importCertificate: (NSData*)data
   139                                 type: (CSSM_CERT_TYPE) type
   140                             encoding: (CSSM_CERT_ENCODING) encoding;
   141 
   142 //@}
   143 #else
   144 /** @name iPhone-Only
   145  *  Functionality only available on iPhone. 
   146  */
   147 //@{
   148 
   149 - (BOOL) removeAllCertificates;
   150 - (BOOL) removeAllKeys;
   151 
   152 //@}
   153 #endif
   154 
   155 
   156 
   157 /** @name Expert (Mac-Only)
   158  *  Advanced functionality, not available on iPhone. 
   159  */
   160 //@{
   161 #if !TARGET_OS_IPHONE
   162 
   163 /** Creates a MYKeychain for an existing SecKeychainRef. */
   164 - (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
   165 
   166 /** Opens a keychain file. */
   167 + (MYKeychain*) openKeychainAtPath: (NSString*)path;
   168 
   169 /** Creates a new keychain file. */
   170 + (MYKeychain*) createKeychainAtPath: (NSString*)path
   171                         withPassword: (NSString*)password;
   172 
   173 /** Closes and deletes the keychain's file. You should not use this object any more. */
   174 - (BOOL) deleteKeychainFile;
   175 
   176 /** Returns the underlying SecKeychainRef for this keychain.
   177     This will be NULL for the special allKeychains instance, because it doesn't
   178     represent a single specific keychain. To handle that case, use the
   179     keychainRefOrDefault property instead. */
   180 @property (readonly) SecKeychainRef keychainRef;
   181 
   182 /** Returns the underlying SecKeychainRef for this keychain.
   183     The special allKeychains instance returns a reference to the default keychain,
   184     as a convenience. */
   185 @property (readonly) SecKeychainRef keychainRefOrDefault;
   186 
   187 /** The path of this keychain's file. */
   188 @property (readonly) NSString* path;
   189 
   190 /** The underlying CSSM storage handle; used when calling CSSM APIs. */
   191 @property (readonly) CSSM_CSP_HANDLE CSPHandle;
   192 
   193 #endif
   194 //@}
   195 
   196 @end
   197