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@4
|
52 |
/** Creates a self-signed identity certificate using this key-pair.
|
snej@4
|
53 |
The attributes describe the certificate's metadata, including its expiration date and the
|
snej@4
|
54 |
subject's name. Keys for the dictionary are given below; the only mandatory one is
|
snej@8
|
55 |
kMYIdentityCommonNameKey.
|
snej@8
|
56 |
The resulting identity certificate includes X.509 extended attributes allowing it to be
|
snej@8
|
57 |
used for SSL connections. (Plug: See my MYNetwork library for an easy way to run SSL
|
snej@8
|
58 |
servers and clients.) */
|
snej@4
|
59 |
- (MYIdentity*) createSelfSignedIdentityWithAttributes: (NSDictionary*)attributes;
|
snej@4
|
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 |
(This is a convenient shorthand for the full exportPrivateKeyInFormat... method.
|
snej@3
|
67 |
It uses OpenSSL format, wrapped with PEM, and a default title and prompt for the alert.) */
|
snej@3
|
68 |
- (NSData*) exportKey;
|
snej@3
|
69 |
|
snej@3
|
70 |
/** Exports the private key as a data blob, so that it can be stored as a backup, or transferred
|
snej@3
|
71 |
to another computer. Since the key is sensitive, it must be exported in encrypted form
|
snej@3
|
72 |
using a user-chosen passphrase. This method will display a standard alert panel, run by
|
snej@3
|
73 |
the Security agent, that prompts the user to enter a new passphrase for encrypting the key.
|
snej@3
|
74 |
The same passphrase must be re-entered when importing the key from the data blob.
|
snej@3
|
75 |
@param format The data format: kSecFormatOpenSSL, kSecFormatSSH, kSecFormatBSAFE or kSecFormatSSHv2.
|
snej@3
|
76 |
@param withPEM YES if the data should be encoded in PEM format, which converts into short lines
|
snej@3
|
77 |
of printable ASCII characters, suitable for sending in email.
|
snej@3
|
78 |
@param alertTitle An optional title for the alert panel. (Currently ignored by the OS?)
|
snej@3
|
79 |
@param prompt An optional prompt message to display in the alert panel. */
|
snej@3
|
80 |
- (NSData*) exportKeyInFormat: (SecExternalFormat)format
|
snej@3
|
81 |
withPEM: (BOOL)withPEM
|
snej@3
|
82 |
alertTitle: (NSString*)alertTitle
|
snej@3
|
83 |
alertPrompt: (NSString*)prompt;
|
snej@13
|
84 |
|
snej@13
|
85 |
/** Decrypts a session key that was wrapped (encrypted) using my matching public key.
|
snej@13
|
86 |
@param wrappedData The wrapped/encrypted session key
|
snej@13
|
87 |
@param algorithm The algorithm of the original session key
|
snej@13
|
88 |
@param sizeInBits The key size (in bits) of the original session key
|
snej@13
|
89 |
@return The reconstituted session key */
|
snej@13
|
90 |
- (MYSymmetricKey*) unwrapSessionKey: (NSData*)wrappedData
|
snej@13
|
91 |
withAlgorithm: (CCAlgorithm)algorithm
|
snej@13
|
92 |
sizeInBits: (unsigned)sizeInBits;
|
snej@3
|
93 |
#endif
|
snej@3
|
94 |
//@}
|
snej@3
|
95 |
|
snej@3
|
96 |
@end
|
snej@4
|
97 |
|
snej@4
|
98 |
|
snej@4
|
99 |
/* Attribute keys for creating identities: */
|
snej@8
|
100 |
|
snej@4
|
101 |
#define kMYIdentityCommonNameKey @"Common Name" // NSString. Required!
|
snej@4
|
102 |
#define kMYIdentityGivenNameKey @"Given Name"
|
snej@4
|
103 |
#define kMYIdentitySurnameKey @"Surname"
|
snej@4
|
104 |
#define kMYIdentityDescriptionKey @"Description"
|
snej@4
|
105 |
#define kMYIdentityEmailAddressKey @"Email Address"
|
snej@4
|
106 |
#define kMYIdentityValidFromKey @"Valid From" // NSDate. Defaults to the current date/time
|
snej@4
|
107 |
#define kMYIdentityValidToKey @"Valid To" // NSDate. Defaults to one year from ValidFrom
|
snej@4
|
108 |
#define kMYIdentitySerialNumberKey @"Serial Number" // NSNumber. Defaults to 1
|