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