MYPrivateKey.h
author Jens Alfke <jens@mooseyard.com>
Wed Jun 10 09:02:18 2009 -0700 (2009-06-10)
changeset 25 38c3c3923e1f
parent 13 6fd9177eb6da
permissions -rw-r--r--
Changed the X.509 version number in generated certs from 1 to 3, so that SecCertificateCreateFromData on iPhone will accept them. :-/
snej@3
     1
//
snej@3
     2
//  MYPrivateKey.h
snej@3
     3
//  MYCrypto
snej@3
     4
//
snej@3
     5
//  Created by Jens Alfke on 4/7/09.
snej@3
     6
//  Copyright 2009 Jens Alfke. All rights reserved.
snej@3
     7
//
snej@3
     8
snej@3
     9
#import "MYKey.h"
snej@13
    10
#import <CommonCrypto/CommonCryptor.h>
snej@13
    11
@class MYPublicKey, MYSHA1Digest, MYIdentity, MYSymmetricKey;
snej@3
    12
snej@3
    13
snej@3
    14
/** A private key, used for signing and decrypting data.
snej@3
    15
    Always paired with a matching public key in a "key-pair".
snej@3
    16
    MYPublicKeys are instantiated by MYKeychain: either by generating a new key-pair, by
snej@3
    17
    looking up a key-pair by its attributes, or by importing a key-pair from data. */
snej@13
    18
@interface MYPrivateKey : MYKey
snej@3
    19
{
snej@3
    20
    @private
snej@3
    21
    MYPublicKey *_publicKey;
snej@3
    22
}
snej@3
    23
snej@3
    24
/** The matching public key. Always non-nil. */
snej@3
    25
@property (readonly) MYPublicKey *publicKey;
snej@3
    26
snej@3
    27
/** The public key's SHA-1 digest. 
snej@3
    28
    This is a convenient short (20-byte) identifier for the key pair. You can store it in your
snej@3
    29
    application data, and then later look up either key using MYKeychain methods. */
snej@3
    30
@property (readonly) MYSHA1Digest *publicKeyDigest;
snej@3
    31
snej@3
    32
snej@3
    33
/** Decrypts data that was encrypted using the public key.
snej@3
    34
    See the description of -[MYPublicKey encryptData:] for warnings and caveats.
snej@3
    35
    This method is usually used only to decrypt a symmetric session key, which then decrypts the
snej@3
    36
    rest of the data. */
snej@13
    37
- (NSData*) rawDecryptData: (NSData*)data;
snej@3
    38
snej@3
    39
/** Generates a signature of data.
snej@3
    40
    (What's actually signed using RSA is the SHA-256 digest of the data.)
snej@3
    41
    The resulting signature can be verified using the matching MYPublicKey's
snej@3
    42
    verifySignature:ofData: method. */
snej@3
    43
- (NSData*) signData: (NSData*)data;
snej@3
    44
snej@3
    45
snej@3
    46
/** @name Mac-Only
snej@3
    47
 *  Functionality not available on iPhone. 
snej@3
    48
 */
snej@3
    49
//@{
snej@3
    50
#if !TARGET_OS_IPHONE
snej@3
    51
snej@3
    52
/** Exports the private key as a data blob, so that it can be stored as a backup, or transferred
snej@3
    53
    to another computer. Since the key is sensitive, it must be exported in encrypted form
snej@3
    54
    using a user-chosen passphrase. This method will display a standard alert panel, run by
snej@3
    55
    the Security agent, that prompts the user to enter a new passphrase for encrypting the key.
snej@3
    56
    The same passphrase must be re-entered when importing the key from the data blob.
snej@3
    57
    (This is a convenient shorthand for the full exportPrivateKeyInFormat... method.
snej@3
    58
    It uses OpenSSL format, wrapped with PEM, and a default title and prompt for the alert.) */
snej@3
    59
- (NSData*) exportKey;
snej@3
    60
snej@3
    61
/** Exports the private key as a data blob, so that it can be stored as a backup, or transferred
snej@3
    62
    to another computer. Since the key is sensitive, it must be exported in encrypted form
snej@3
    63
    using a user-chosen passphrase. This method will display a standard alert panel, run by
snej@3
    64
    the Security agent, that prompts the user to enter a new passphrase for encrypting the key.
snej@3
    65
    The same passphrase must be re-entered when importing the key from the data blob.
snej@3
    66
    @param format  The data format: kSecFormatOpenSSL, kSecFormatSSH, kSecFormatBSAFE or kSecFormatSSHv2.
snej@3
    67
    @param withPEM  YES if the data should be encoded in PEM format, which converts into short lines
snej@3
    68
        of printable ASCII characters, suitable for sending in email.
snej@3
    69
    @param alertTitle  An optional title for the alert panel. (Currently ignored by the OS?)
snej@3
    70
    @param prompt  An optional prompt message to display in the alert panel. */
snej@3
    71
- (NSData*) exportKeyInFormat: (SecExternalFormat)format
snej@3
    72
                      withPEM: (BOOL)withPEM
snej@3
    73
                   alertTitle: (NSString*)alertTitle
snej@3
    74
                  alertPrompt: (NSString*)prompt;
snej@13
    75
snej@13
    76
/** Decrypts a session key that was wrapped (encrypted) using my matching public key.
snej@13
    77
    @param wrappedData  The wrapped/encrypted session key
snej@13
    78
    @param algorithm  The algorithm of the original session key
snej@13
    79
    @param sizeInBits  The key size (in bits) of the original session key
snej@13
    80
    @return  The reconstituted session key */
snej@13
    81
- (MYSymmetricKey*) unwrapSessionKey: (NSData*)wrappedData
snej@13
    82
                       withAlgorithm: (CCAlgorithm)algorithm
snej@13
    83
                          sizeInBits: (unsigned)sizeInBits;
snej@3
    84
#endif
snej@3
    85
//@}
snej@3
    86
snej@3
    87
@end