MYPrivateKey.h
author snej@snej.local
Thu Apr 09 22:46:48 2009 -0700 (2009-04-09)
changeset 6 2d7692f9b6b4
parent 3 1dfe820d7ebe
child 8 4c0eafa7b233
permissions -rw-r--r--
Updated the README for the 0.1 release.
     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 @class MYPublicKey, MYSHA1Digest, MYIdentity;
    11 
    12 
    13 /** A private key, used for signing and decrypting data.
    14     Always paired with a matching public key in a "key-pair".
    15     MYPublicKeys are instantiated by MYKeychain: either by generating a new key-pair, by
    16     looking up a key-pair by its attributes, or by importing a key-pair from data. */
    17 @interface MYPrivateKey : MYKey <MYDecryption>
    18 {
    19     @private
    20     MYPublicKey *_publicKey;
    21 }
    22 
    23 /** The matching public key. Always non-nil. */
    24 @property (readonly) MYPublicKey *publicKey;
    25 
    26 /** The public key's SHA-1 digest. 
    27     This is a convenient short (20-byte) identifier for the key pair. You can store it in your
    28     application data, and then later look up either key using MYKeychain methods. */
    29 @property (readonly) MYSHA1Digest *publicKeyDigest;
    30 
    31 
    32 /** Decrypts data that was encrypted using the public key.
    33     See the description of -[MYPublicKey encryptData:] for warnings and caveats.
    34     This method is usually used only to decrypt a symmetric session key, which then decrypts the
    35     rest of the data. */
    36 - (NSData*) decryptData: (NSData*)data;
    37 
    38 /** Generates a signature of data.
    39     (What's actually signed using RSA is the SHA-256 digest of the data.)
    40     The resulting signature can be verified using the matching MYPublicKey's
    41     verifySignature:ofData: method. */
    42 - (NSData*) signData: (NSData*)data;
    43 
    44 
    45 /** @name Mac-Only
    46  *  Functionality not available on iPhone. 
    47  */
    48 //@{
    49 #if !TARGET_OS_IPHONE
    50 
    51 /** Creates a self-signed identity certificate using this key-pair.
    52     The attributes describe the certificate's metadata, including its expiration date and the
    53     subject's name. Keys for the dictionary are given below; the only mandatory one is
    54     kMYIdentityCommonNameKey. */
    55 - (MYIdentity*) createSelfSignedIdentityWithAttributes: (NSDictionary*)attributes;
    56 
    57 /** Exports the private key as a data blob, so that it can be stored as a backup, or transferred
    58     to another computer. Since the key is sensitive, it must be exported in encrypted form
    59     using a user-chosen passphrase. This method will display a standard alert panel, run by
    60     the Security agent, that prompts the user to enter a new passphrase for encrypting the key.
    61     The same passphrase must be re-entered when importing the key from the data blob.
    62     (This is a convenient shorthand for the full exportPrivateKeyInFormat... method.
    63     It uses OpenSSL format, wrapped with PEM, and a default title and prompt for the alert.) */
    64 - (NSData*) exportKey;
    65 
    66 /** Exports the private key as a data blob, so that it can be stored as a backup, or transferred
    67     to another computer. Since the key is sensitive, it must be exported in encrypted form
    68     using a user-chosen passphrase. This method will display a standard alert panel, run by
    69     the Security agent, that prompts the user to enter a new passphrase for encrypting the key.
    70     The same passphrase must be re-entered when importing the key from the data blob.
    71     @param format  The data format: kSecFormatOpenSSL, kSecFormatSSH, kSecFormatBSAFE or kSecFormatSSHv2.
    72     @param withPEM  YES if the data should be encoded in PEM format, which converts into short lines
    73         of printable ASCII characters, suitable for sending in email.
    74     @param alertTitle  An optional title for the alert panel. (Currently ignored by the OS?)
    75     @param prompt  An optional prompt message to display in the alert panel. */
    76 - (NSData*) exportKeyInFormat: (SecExternalFormat)format
    77                       withPEM: (BOOL)withPEM
    78                    alertTitle: (NSString*)alertTitle
    79                   alertPrompt: (NSString*)prompt;
    80 #endif
    81 //@}
    82 
    83 @end
    84 
    85 
    86 /* Attribute keys for creating identities: */
    87 #define kMYIdentityCommonNameKey    @"Common Name"      // NSString. Required!
    88 #define kMYIdentityGivenNameKey     @"Given Name"
    89 #define kMYIdentitySurnameKey       @"Surname"
    90 #define kMYIdentityDescriptionKey   @"Description"
    91 #define kMYIdentityEmailAddressKey  @"Email Address"
    92 #define kMYIdentityValidFromKey     @"Valid From"       // NSDate. Defaults to the current date/time
    93 #define kMYIdentityValidToKey       @"Valid To"         // NSDate. Defaults to one year from ValidFrom
    94 #define kMYIdentitySerialNumberKey  @"Serial Number"    // NSNumber. Defaults to 1