snej@0
|
1 |
//
|
snej@0
|
2 |
// MYKey.h
|
snej@0
|
3 |
// MYCrypto
|
snej@0
|
4 |
//
|
snej@0
|
5 |
// Created by Jens Alfke on 3/30/09.
|
snej@0
|
6 |
// Copyright 2009 Jens Alfke. All rights reserved.
|
snej@0
|
7 |
//
|
snej@0
|
8 |
|
snej@0
|
9 |
#import "MYKeychainItem.h"
|
snej@0
|
10 |
|
snej@0
|
11 |
|
snej@1
|
12 |
@protocol MYEncryption <NSObject>
|
snej@0
|
13 |
|
snej@1
|
14 |
/** Encrypts data using this key, returning the raw encrypted result. */
|
snej@1
|
15 |
- (NSData*) encryptData: (NSData*)data;
|
snej@1
|
16 |
|
snej@1
|
17 |
@end
|
snej@1
|
18 |
|
snej@1
|
19 |
@protocol MYDecryption <NSObject>
|
snej@1
|
20 |
|
snej@1
|
21 |
/** Decrypts data using this key, returning the original data. */
|
snej@1
|
22 |
- (NSData*) decryptData: (NSData*)data;
|
snej@1
|
23 |
|
snej@1
|
24 |
@end
|
snej@1
|
25 |
|
snej@1
|
26 |
|
snej@1
|
27 |
|
snej@1
|
28 |
/** Abstract superclass for keys.
|
snej@1
|
29 |
Concrete subclasses are MYSymmetricKey and MYPublicKey. */
|
snej@0
|
30 |
@interface MYKey : MYKeychainItem
|
snej@2
|
31 |
{ }
|
snej@0
|
32 |
|
snej@1
|
33 |
/** The key's raw data. */
|
snej@0
|
34 |
@property (readonly) NSData *keyData;
|
snej@0
|
35 |
|
snej@0
|
36 |
/** The user-visible name (kSecKeyPrintName) associated with this key in the Keychain.
|
snej@0
|
37 |
The user can edit this, so don't expect it to be immutable. */
|
snej@0
|
38 |
@property (copy) NSString *name;
|
snej@0
|
39 |
|
snej@0
|
40 |
/** An application-specific string (kSecKeyAlias) associated with this key in the Keychain.
|
snej@0
|
41 |
Not visible to or editable by the user.
|
snej@0
|
42 |
If you own this key, you can store any associated metadata you like here, although be aware
|
snej@0
|
43 |
that it can be read and modified by any other app that can access this key. */
|
snej@0
|
44 |
@property (copy) NSString *alias;
|
snej@0
|
45 |
|
snej@2
|
46 |
|
snej@2
|
47 |
/** @name Mac-Only
|
snej@2
|
48 |
* Functionality not available on iPhone.
|
snej@2
|
49 |
*/
|
snej@2
|
50 |
//@{
|
snej@0
|
51 |
#if !TARGET_OS_IPHONE
|
snej@2
|
52 |
|
snej@0
|
53 |
/** The user-visible comment (kSecKeyApplicationTag) associated with this key in the Keychain.
|
snej@2
|
54 |
The user can edit this, so don't expect it to be immutable. */
|
snej@0
|
55 |
@property (copy) NSString *comment;
|
snej@2
|
56 |
|
snej@2
|
57 |
/** Converts the key into a data blob in one of several standard formats, suitable for storing in
|
snej@2
|
58 |
a file or sending over the network.
|
snej@2
|
59 |
@param format The data format: kSecFormatOpenSSL, kSecFormatSSH, kSecFormatBSAFE or kSecFormatSSHv2.
|
snej@2
|
60 |
@param withPEM YES if the data should be encoded in PEM format, which converts into short lines
|
snej@2
|
61 |
of printable ASCII characters, suitable for sending in email. */
|
snej@2
|
62 |
- (NSData*) exportKeyInFormat: (SecExternalFormat)format withPEM: (BOOL)withPEM;
|
snej@2
|
63 |
|
snej@0
|
64 |
#endif
|
snej@2
|
65 |
//@}
|
snej@0
|
66 |
|
snej@0
|
67 |
|
snej@2
|
68 |
/** @name Expert
|
snej@2
|
69 |
* Advanced methods.
|
snej@2
|
70 |
*/
|
snej@2
|
71 |
//@{
|
snej@0
|
72 |
|
snej@1
|
73 |
/** Creates a MYKey object for an existing Keychain key reference.
|
snej@1
|
74 |
This is abstract -- must be called on a MYSymmetricKey or MYPublicKey, as appropriate. */
|
snej@1
|
75 |
- (id) initWithKeyRef: (SecKeyRef)keyRef;
|
snej@1
|
76 |
|
snej@1
|
77 |
/** The Keychain object reference for this key. */
|
snej@1
|
78 |
@property (readonly) SecKeyRef keyRef;
|
snej@1
|
79 |
|
snej@1
|
80 |
#if !TARGET_OS_IPHONE
|
snej@1
|
81 |
/** The underlying CSSM_KEY structure; used with low-level crypto APIs. */
|
snej@1
|
82 |
@property (readonly) const struct cssm_key* cssmKey;
|
snej@1
|
83 |
|
snej@2
|
84 |
/** The underlying CSSM_CSP_HANDLE structure; used with low-level crypto APIs. */
|
snej@2
|
85 |
@property (readonly) intptr_t /*CSSM_CSP_HANDLE*/ cssmCSPHandle;
|
snej@2
|
86 |
|
snej@2
|
87 |
/** Gets CSSM authorization credentials for a specified operation, such as
|
snej@2
|
88 |
CSSM_ACL_AUTHORIZATION_ENCRYPT. This pointer is necessary for creating some CSSM operation
|
snej@2
|
89 |
contexts.
|
snej@2
|
90 |
@param operation The type of operation you are going to perform (see the enum values in
|
snej@2
|
91 |
cssmType.h.)
|
snej@2
|
92 |
@param type Specifies whether the operation should be allowed to present a UI. You'll usually
|
snej@2
|
93 |
want to pass kSecCredentialTypeDefault.
|
snej@2
|
94 |
@param outError Will be set to point to an NSError on failure, or nil on success.
|
snej@2
|
95 |
Pass nil if you don't care about the specific error.
|
snej@2
|
96 |
@return The access credentials, or NULL on failure.
|
snej@2
|
97 |
This pointer is valid for as long as you have a reference
|
snej@2
|
98 |
to the key object. Do not free or delete it. */
|
snej@2
|
99 |
- (const CSSM_ACCESS_CREDENTIALS*) cssmCredentialsForOperation: (CSSM_ACL_AUTHORIZATION_TAG)operation
|
snej@2
|
100 |
type: (SecCredentialType)type
|
snej@2
|
101 |
error: (NSError**)outError;
|
snej@2
|
102 |
|
snej@1
|
103 |
#endif
|
snej@2
|
104 |
//@}
|
snej@0
|
105 |
|
snej@0
|
106 |
@end
|