MYCrypto_Private.h
author Jens Alfke <jens@mooseyard.com>
Sun Jun 07 21:53:56 2009 -0700 (2009-06-07)
changeset 23 39fec79de6e8
parent 21 2c300b15b381
child 26 d9c2a06d4e4e
permissions -rw-r--r--
A snapshot taken during the long, agonizing crawl toward getting everything running on iPhone.
     1 //
     2 //  MYCrypto_Private.h
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 3/23/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYCryptoConfig.h"
    10 #import "MYKeychain.h"
    11 #import "MYKey.h"
    12 #import "MYSymmetricKey.h"
    13 #import "MYPublicKey.h"
    14 #import "MYPrivateKey.h"
    15 #import "MYCertificate.h"
    16 #import "Test.h"
    17 #import <Security/Security.h>
    18 
    19 
    20 #if MYCRYPTO_USE_IPHONE_API
    21 typedef CFTypeRef SecKeychainAttrType;
    22 typedef CFTypeRef SecKeychainItemRef;
    23 typedef CFTypeRef SecKeychainRef;
    24 typedef CFTypeRef SecExternalItemType;
    25 #endif
    26 
    27 
    28 @interface MYKeychain (Private)
    29 - (MYIdentity*) identityWithDigest: (MYSHA1Digest*)pubKeyDigest;
    30 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
    31 - (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
    32 @property (readonly) SecKeychainRef keychainRef, keychainRefOrDefault;
    33 @property (readonly) CSSM_CSP_HANDLE CSPHandle;
    34 @property (readonly) NSString* path;
    35 #endif
    36 @end
    37 
    38 
    39 @interface MYKeychainItem (Private);
    40 - (id) initWithKeychainItemRef: (MYKeychainItemRef)itemRef;
    41 - (NSData*) _getContents: (OSStatus*)outError;
    42 - (NSString*) stringValueOfAttribute: (SecKeychainAttrType)attr;
    43 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
    44 + (NSData*) _getAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
    45 - (id) _attribute: (SecKeychainAttrType)attribute;
    46 + (NSString*) _getStringAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
    47 + (BOOL) _setAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item
    48            stringValue: (NSString*)stringValue;
    49 @end      
    50 
    51 
    52 @interface MYKey (Private)
    53 - (id) initWithKeyData: (NSData*)data;
    54 - (id) _initWithKeyData: (NSData*)data
    55             forKeychain: (SecKeychainRef)keychain;
    56 @property (readonly) SecExternalItemType keyClass, keyType;
    57 @property (readonly) MYSHA1Digest* _keyDigest;
    58 - (NSData*) _crypt: (NSData *)data operation: (BOOL) op;    // YES to encrypt, NO to decrypt
    59 #if MYCRYPTO_USE_IPHONE_API
    60 + (SecKeyRef) _addKeyWithInfo: (NSMutableDictionary*)info;
    61 #else
    62 @property (readonly) const CSSM_KEY* cssmKey;
    63 @property (readonly) const CSSM_CSP_HANDLE cssmCSPHandle;
    64 - (CSSM_CC_HANDLE) _createSignatureContext: (CSSM_ALGORITHMS)algorithm;
    65 - (CSSM_CC_HANDLE) _createPassThroughContext;
    66 #endif
    67 @property (readonly) NSArray* _itemList;
    68 @end
    69 
    70 
    71 @interface MYSymmetricKey (Private)
    72 #if !MYCRYPTO_USE_IPHONE_API
    73 - (id) _initWithCSSMKey: (CSSM_KEY*)cssmKey;
    74 #endif
    75 + (MYSymmetricKey*) _generateSymmetricKeyOfSize: (unsigned)keySizeInBits
    76                                       algorithm: (CCAlgorithm)algorithm
    77                                      inKeychain: (MYKeychain*)keychain;
    78 @end
    79 
    80 
    81 @interface MYPublicKey (Private)
    82 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
    83 #if !TARGET_OS_IPHONE
    84 - (CSSM_WRAP_KEY*) _unwrappedCSSMKey;
    85 #endif
    86 @end
    87 
    88 
    89 @interface MYPrivateKey (Private)
    90 + (MYPrivateKey*) _generateRSAKeyPairOfSize: (unsigned)keySize
    91                                  inKeychain: (MYKeychain*)keychain;
    92 - (id) _initWithKeyRef: (SecKeyRef)privateKey
    93              publicKey: (MYPublicKey*)publicKey;
    94 - (id) _initWithKeyData: (NSData*)privKeyData 
    95           publicKeyData: (NSData*)pubKeyData
    96             forKeychain: (SecKeychainRef)keychain 
    97              alertTitle: (NSString*)title
    98             alertPrompt: (NSString*)prompt;
    99 - (id) _initWithKeyData: (NSData*)privKeyData 
   100           publicKeyData: (NSData*)pubKeyData
   101             forKeychain: (SecKeychainRef)keychain 
   102              passphrase: (NSString*)passphrase;
   103 #if !TARGET_OS_IPHONE
   104 - (NSData*) _exportKeyInFormat: (SecExternalFormat)format
   105                        withPEM: (BOOL)withPEM
   106                     passphrase: (NSString*)passphrase;
   107 #endif
   108 @end
   109 
   110 
   111 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
   112 @interface MYCertificate (Private)
   113 - (id) initWithCertificateData: (NSData*)data
   114                           type: (CSSM_CERT_TYPE) type
   115                       encoding: (CSSM_CERT_ENCODING) encoding;
   116 @end
   117 #endif
   118 
   119 
   120 #undef check
   121 BOOL check(OSStatus err, NSString *what);
   122 
   123 #define checksave(CALL) ({OSStatus err=(CALL); check(err,@""#CALL) || (_error=err, NO);})
   124 
   125 #if !MYCRYPTO_USE_IPHONE_API
   126 BOOL checkcssm(CSSM_RETURN err, NSString *what);
   127 
   128 SecKeyRef importKey(NSData *data, 
   129                     SecExternalItemType type,
   130                     SecKeychainRef keychain,
   131                     SecKeyImportExportParameters *params /*non-null*/);
   132 
   133 NSString* OIDAsString(CSSM_OID OID);
   134 CSSM_ALGORITHMS CSSMFromCCAlgorithm( CCAlgorithm ccAlgorithm );
   135 
   136 typedef struct {
   137     CSSM_ALGORITHMS algorithm;
   138     uint32 sizeInBits;
   139 } MYCryptoWrappedKeyDesc;
   140 
   141 #endif