MYKey.h
author Jens Alfke <jens@mooseyard.com>
Sun Jun 07 21:53:56 2009 -0700 (2009-06-07)
changeset 23 39fec79de6e8
parent 17 90a70925562b
child 24 6856e071d25a
permissions -rw-r--r--
A snapshot taken during the long, agonizing crawl toward getting everything running on iPhone.
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
jens@23
    31
{ 
jens@23
    32
    NSData *_keyData;
jens@23
    33
}
snej@0
    34
jens@17
    35
/** Creates a key from encoded data (but does not add it to any keychain.) */
jens@17
    36
- (id) initWithKeyData: (NSData*)data;
jens@17
    37
snej@1
    38
/** The key's raw data. */
snej@0
    39
@property (readonly) NSData *keyData;
snej@0
    40
snej@0
    41
/** The user-visible name (kSecKeyPrintName) associated with this key in the Keychain.
snej@0
    42
    The user can edit this, so don't expect it to be immutable. */
snej@0
    43
@property (copy) NSString *name;
snej@0
    44
snej@0
    45
/** An application-specific string (kSecKeyAlias) associated with this key in the Keychain.
snej@0
    46
    Not visible to or editable by the user.
snej@0
    47
    If you own this key, you can store any associated metadata you like here, although be aware
snej@0
    48
    that it can be read and modified by any other app that can access this key. */
snej@0
    49
@property (copy) NSString *alias;
snej@0
    50
snej@2
    51
snej@2
    52
/** @name Mac-Only
snej@2
    53
 *  Functionality not available on iPhone. 
snej@2
    54
 */
snej@2
    55
//@{
snej@0
    56
#if !TARGET_OS_IPHONE
snej@2
    57
snej@0
    58
/** The user-visible comment (kSecKeyApplicationTag) associated with this key in the Keychain.
snej@2
    59
 The user can edit this, so don't expect it to be immutable. */
snej@0
    60
@property (copy) NSString *comment;
snej@2
    61
snej@0
    62
#endif
snej@2
    63
//@}
snej@0
    64
snej@0
    65
snej@2
    66
/** @name Expert
snej@2
    67
 *  Advanced methods. 
snej@2
    68
 */
snej@2
    69
//@{
snej@0
    70
snej@1
    71
/** Creates a MYKey object for an existing Keychain key reference.
snej@1
    72
    This is abstract -- must be called on a MYSymmetricKey or MYPublicKey, as appropriate. */
snej@1
    73
- (id) initWithKeyRef: (SecKeyRef)keyRef;
snej@1
    74
snej@1
    75
/** The Keychain object reference for this key. */
snej@1
    76
@property (readonly) SecKeyRef keyRef;
snej@1
    77
snej@1
    78
#if !TARGET_OS_IPHONE
snej@1
    79
/** The underlying CSSM_KEY structure; used with low-level crypto APIs. */
snej@1
    80
@property (readonly) const struct cssm_key* cssmKey;
snej@1
    81
snej@2
    82
/** The underlying CSSM_CSP_HANDLE structure; used with low-level crypto APIs. */
snej@2
    83
@property (readonly) intptr_t /*CSSM_CSP_HANDLE*/ cssmCSPHandle;
snej@2
    84
snej@13
    85
@property (readonly) CSSM_ALGORITHMS cssmAlgorithm;
snej@13
    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