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