* Some cleanup. Got the test cases to pass again.
* Added some missing copyright notices.
5 // Created by Jens Alfke on 4/7/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
10 #import <CommonCrypto/CommonCryptor.h>
11 @class MYPublicKey, MYSHA1Digest, MYIdentity, MYSymmetricKey;
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
21 MYPublicKey *_publicKey;
24 /** The matching public key. Always non-nil. */
25 @property (readonly) MYPublicKey *publicKey;
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;
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
37 - (NSData*) rawDecryptData: (NSData*)data;
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;
47 * Functionality not available on iPhone.
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;
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;
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;
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;
99 /* Attribute keys for creating identities: */
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