MYPrivateKey.h
author Jens Alfke <jens@mooseyard.com>
Fri Jun 05 08:57:18 2009 -0700 (2009-06-05)
changeset 20 df9da0f6b358
parent 8 4c0eafa7b233
child 21 2c300b15b381
permissions -rw-r--r--
Factored out the name accessors of MYParsedCertificate into a new class MYCertificateName, so that both subject and issuer can be accessed. A bit of other cleanup too.
     1 //
     2 //  MYPrivateKey.h
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 4/7/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYKey.h"
    10 #import <CommonCrypto/CommonCryptor.h>
    11 @class MYPublicKey, MYSHA1Digest, MYIdentity, MYSymmetricKey;
    12 
    13 
    14 /** A private key, used for signing and decrypting data.
    15     Always paired with a matching public key in a "key-pair".
    16     MYPublicKeys are instantiated by MYKeychain: either by generating a new key-pair, by
    17     looking up a key-pair by its attributes, or by importing a key-pair from data. */
    18 @interface MYPrivateKey : MYKey
    19 {
    20     @private
    21     MYPublicKey *_publicKey;
    22 }
    23 
    24 /** The matching public key. Always non-nil. */
    25 @property (readonly) MYPublicKey *publicKey;
    26 
    27 /** The public key's SHA-1 digest. 
    28     This is a convenient short (20-byte) identifier for the key pair. You can store it in your
    29     application data, and then later look up either key using MYKeychain methods. */
    30 @property (readonly) MYSHA1Digest *publicKeyDigest;
    31 
    32 
    33 /** Decrypts data that was encrypted using the public key.
    34     See the description of -[MYPublicKey encryptData:] for warnings and caveats.
    35     This method is usually used only to decrypt a symmetric session key, which then decrypts the
    36     rest of the data. */
    37 - (NSData*) rawDecryptData: (NSData*)data;
    38 
    39 /** Generates a signature of data.
    40     (What's actually signed using RSA is the SHA-256 digest of the data.)
    41     The resulting signature can be verified using the matching MYPublicKey's
    42     verifySignature:ofData: method. */
    43 - (NSData*) signData: (NSData*)data;
    44 
    45 
    46 /** @name Mac-Only
    47  *  Functionality not available on iPhone. 
    48  */
    49 //@{
    50 #if !TARGET_OS_IPHONE
    51 
    52 /** Creates a self-signed identity certificate using this key-pair.
    53     The attributes describe the certificate's metadata, including its expiration date and the
    54     subject's name. Keys for the dictionary are given below; the only mandatory one is
    55     kMYIdentityCommonNameKey.
    56     The resulting identity certificate includes X.509 extended attributes allowing it to be
    57     used for SSL connections. (Plug: See my MYNetwork library for an easy way to run SSL
    58     servers and clients.) */
    59 - (MYIdentity*) createSelfSignedIdentityWithAttributes: (NSDictionary*)attributes;
    60 
    61 /** Exports the private key as a data blob, so that it can be stored as a backup, or transferred
    62     to another computer. Since the key is sensitive, it must be exported in encrypted form
    63     using a user-chosen passphrase. This method will display a standard alert panel, run by
    64     the Security agent, that prompts the user to enter a new passphrase for encrypting the key.
    65     The same passphrase must be re-entered when importing the key from the data blob.
    66     (This is a convenient shorthand for the full exportPrivateKeyInFormat... method.
    67     It uses OpenSSL format, wrapped with PEM, and a default title and prompt for the alert.) */
    68 - (NSData*) exportKey;
    69 
    70 /** Exports the private key as a data blob, so that it can be stored as a backup, or transferred
    71     to another computer. Since the key is sensitive, it must be exported in encrypted form
    72     using a user-chosen passphrase. This method will display a standard alert panel, run by
    73     the Security agent, that prompts the user to enter a new passphrase for encrypting the key.
    74     The same passphrase must be re-entered when importing the key from the data blob.
    75     @param format  The data format: kSecFormatOpenSSL, kSecFormatSSH, kSecFormatBSAFE or kSecFormatSSHv2.
    76     @param withPEM  YES if the data should be encoded in PEM format, which converts into short lines
    77         of printable ASCII characters, suitable for sending in email.
    78     @param alertTitle  An optional title for the alert panel. (Currently ignored by the OS?)
    79     @param prompt  An optional prompt message to display in the alert panel. */
    80 - (NSData*) exportKeyInFormat: (SecExternalFormat)format
    81                       withPEM: (BOOL)withPEM
    82                    alertTitle: (NSString*)alertTitle
    83                   alertPrompt: (NSString*)prompt;
    84 
    85 /** Decrypts a session key that was wrapped (encrypted) using my matching public key.
    86     @param wrappedData  The wrapped/encrypted session key
    87     @param algorithm  The algorithm of the original session key
    88     @param sizeInBits  The key size (in bits) of the original session key
    89     @return  The reconstituted session key */
    90 - (MYSymmetricKey*) unwrapSessionKey: (NSData*)wrappedData
    91                        withAlgorithm: (CCAlgorithm)algorithm
    92                           sizeInBits: (unsigned)sizeInBits;
    93 #endif
    94 //@}
    95 
    96 @end
    97 
    98 
    99 /* Attribute keys for creating identities: */
   100 
   101 #define kMYIdentityCommonNameKey    @"Common Name"      // NSString. Required!
   102 #define kMYIdentityGivenNameKey     @"Given Name"
   103 #define kMYIdentitySurnameKey       @"Surname"
   104 #define kMYIdentityDescriptionKey   @"Description"
   105 #define kMYIdentityEmailAddressKey  @"Email Address"
   106 #define kMYIdentityValidFromKey     @"Valid From"       // NSDate. Defaults to the current date/time
   107 #define kMYIdentityValidToKey       @"Valid To"         // NSDate. Defaults to one year from ValidFrom
   108 #define kMYIdentitySerialNumberKey  @"Serial Number"    // NSNumber. Defaults to 1