MYKey.h
author Jens Alfke <jens@mooseyard.com>
Tue Jul 21 10:13:08 2009 -0700 (2009-07-21)
changeset 27 d0aadddb9c64
parent 24 6856e071d25a
permissions -rw-r--r--
MYCertificate now checks validity of self-signed certs loaded from the keychain (because the Security framework doesn't validate self-signed certs.)
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@24
    32
    @private
jens@23
    33
    NSData *_keyData;
jens@23
    34
}
snej@0
    35
jens@17
    36
/** Creates a key from encoded data (but does not add it to any keychain.) */
jens@17
    37
- (id) initWithKeyData: (NSData*)data;
jens@17
    38
snej@1
    39
/** The key's raw data. */
snej@0
    40
@property (readonly) NSData *keyData;
snej@0
    41
jens@26
    42
/** The key's size/length, in bits. */
jens@26
    43
@property (readonly) unsigned keySizeInBits;
jens@26
    44
snej@0
    45
/** The user-visible name (kSecKeyPrintName) associated with this key in the Keychain.
snej@0
    46
    The user can edit this, so don't expect it to be immutable. */
snej@0
    47
@property (copy) NSString *name;
snej@0
    48
snej@0
    49
/** An application-specific string (kSecKeyAlias) associated with this key in the Keychain.
snej@0
    50
    Not visible to or editable by the user.
snej@0
    51
    If you own this key, you can store any associated metadata you like here, although be aware
snej@0
    52
    that it can be read and modified by any other app that can access this key. */
snej@0
    53
@property (copy) NSString *alias;
snej@0
    54
snej@2
    55
snej@2
    56
/** @name Mac-Only
snej@2
    57
 *  Functionality not available on iPhone. 
snej@2
    58
 */
snej@2
    59
//@{
snej@0
    60
#if !TARGET_OS_IPHONE
snej@2
    61
snej@0
    62
/** The user-visible comment (kSecKeyApplicationTag) associated with this key in the Keychain.
snej@2
    63
 The user can edit this, so don't expect it to be immutable. */
snej@0
    64
@property (copy) NSString *comment;
snej@2
    65
snej@0
    66
#endif
snej@2
    67
//@}
snej@0
    68
snej@0
    69
snej@2
    70
/** @name Expert
snej@2
    71
 *  Advanced methods. 
snej@2
    72
 */
snej@2
    73
//@{
snej@0
    74
snej@1
    75
/** Creates a MYKey object for an existing Keychain key reference.
snej@1
    76
    This is abstract -- must be called on a MYSymmetricKey or MYPublicKey, as appropriate. */
snej@1
    77
- (id) initWithKeyRef: (SecKeyRef)keyRef;
snej@1
    78
snej@1
    79
/** The Keychain object reference for this key. */
snej@1
    80
@property (readonly) SecKeyRef keyRef;
snej@1
    81
snej@1
    82
#if !TARGET_OS_IPHONE
snej@1
    83
/** The underlying CSSM_KEY structure; used with low-level crypto APIs. */
snej@1
    84
@property (readonly) const struct cssm_key* cssmKey;
snej@1
    85
snej@2
    86
/** The underlying CSSM_CSP_HANDLE structure; used with low-level crypto APIs. */
snej@2
    87
@property (readonly) intptr_t /*CSSM_CSP_HANDLE*/ cssmCSPHandle;
snej@2
    88
snej@13
    89
@property (readonly) CSSM_ALGORITHMS cssmAlgorithm;
snej@13
    90
snej@2
    91
/** Gets CSSM authorization credentials for a specified operation, such as
snej@2
    92
    CSSM_ACL_AUTHORIZATION_ENCRYPT. This pointer is necessary for creating some CSSM operation
snej@2
    93
    contexts.
snej@2
    94
    @param operation  The type of operation you are going to perform (see the enum values in
snej@2
    95
            cssmType.h.)
snej@2
    96
    @param type  Specifies whether the operation should be allowed to present a UI. You'll usually
snej@2
    97
            want to pass kSecCredentialTypeDefault.
snej@2
    98
    @param outError  Will be set to point to an NSError on failure, or nil on success.
snej@2
    99
            Pass nil if you don't care about the specific error.
snej@2
   100
    @return  The access credentials, or NULL on failure. 
snej@2
   101
            This pointer is valid for as long as you have a reference
snej@2
   102
            to the key object. Do not free or delete it. */
snej@2
   103
- (const CSSM_ACCESS_CREDENTIALS*) cssmCredentialsForOperation: (CSSM_ACL_AUTHORIZATION_TAG)operation
snej@2
   104
                                                          type: (SecCredentialType)type
snej@2
   105
                                                         error: (NSError**)outError;
snej@2
   106
snej@1
   107
#endif
snej@2
   108
//@}
snej@0
   109
snej@0
   110
@end