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