MYKeychain.h
author Jens Alfke <jens@mooseyard.com>
Sat Jun 06 15:01:28 2009 -0700 (2009-06-06)
changeset 21 2c300b15b381
parent 5 b2e360b78189
child 26 d9c2a06d4e4e
permissions -rw-r--r--
* Created class MYCertificateRequest, factored out of MYCertificateInfo.
* Added method to create a MYIdentity directly from a MYCertificateRequest.
* Added raw modulus+exponent accessor and initializer for MYPublicKey.
* Removed obsolete MYCertGen code, and the MYPrivateKey identity-creation method that used it.
     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 /** Imports a key-pair into the keychain, given the external representations
   117     of both the public and private keys.
   118     Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
   119     the passphrase. */
   120 - (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
   121                        privateKey: (NSData*)privKeyData;
   122 
   123 /** Imports a key-pair into the keychain, given the external representations
   124     of both the public and private keys.
   125     Since the private key data is wrapped (encrypted), the Security agent will prompt the user to enter
   126     the passphrase. You can specify the title and prompt message for this alert panel. */
   127 - (MYPrivateKey*) importPublicKey: (NSData*)pubKeyData 
   128                        privateKey: (NSData*)privKeyData
   129                        alertTitle: (NSString*)title
   130                       alertPrompt: (NSString*)prompt;
   131 
   132 /** Adds a certificate to this keychain. (It must not already belong to a keychain.) */
   133 - (BOOL) addCertificate: (MYCertificate*)certificate;
   134 
   135 /** Imports a certificate into the keychain, given its external representation. */
   136 - (MYCertificate*) importCertificate: (NSData*)data
   137                                 type: (CSSM_CERT_TYPE) type
   138                             encoding: (CSSM_CERT_ENCODING) encoding;
   139 
   140 //@}
   141 #endif
   142 
   143 
   144 
   145 /** @name Expert (Mac-Only)
   146  *  Advanced functionality, not available on iPhone. 
   147  */
   148 //@{
   149 #if !TARGET_OS_IPHONE
   150 
   151 /** Creates a MYKeychain for an existing SecKeychainRef. */
   152 - (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
   153 
   154 /** Opens a keychain file. */
   155 + (MYKeychain*) openKeychainAtPath: (NSString*)path;
   156 
   157 /** Creates a new keychain file. */
   158 + (MYKeychain*) createKeychainAtPath: (NSString*)path
   159                         withPassword: (NSString*)password;
   160 
   161 /** Closes and deletes the keychain's file. You should not use this object any more. */
   162 - (BOOL) deleteKeychainFile;
   163 
   164 /** Returns the underlying SecKeychainRef for this keychain.
   165     This will be NULL for the special allKeychains instance, because it doesn't
   166     represent a single specific keychain. To handle that case, use the
   167     keychainRefOrDefault property instead. */
   168 @property (readonly) SecKeychainRef keychainRef;
   169 
   170 /** Returns the underlying SecKeychainRef for this keychain.
   171     The special allKeychains instance returns a reference to the default keychain,
   172     as a convenience. */
   173 @property (readonly) SecKeychainRef keychainRefOrDefault;
   174 
   175 /** The path of this keychain's file. */
   176 @property (readonly) NSString* path;
   177 
   178 /** The underlying CSSM storage handle; used when calling CSSM APIs. */
   179 @property (readonly) CSSM_CSP_HANDLE CSPHandle;
   180 
   181 #endif
   182 //@}
   183 
   184 @end
   185