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