MYCrypto_Private.h
author Jens Alfke <jens@mooseyard.com>
Thu Jun 04 18:36:30 2009 -0700 (2009-06-04)
changeset 19 f6c91b9da05b
parent 13 6fd9177eb6da
child 21 2c300b15b381
permissions -rw-r--r--
Whew! MYParsedCertificate can now generate certs from scratch. Also added improvements and fixes to the BER/DER codecs.
     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 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
    29 @interface MYKeychain (Private)
    30 - (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
    31 @property (readonly) SecKeychainRef keychainRef, keychainRefOrDefault;
    32 @property (readonly) CSSM_CSP_HANDLE CSPHandle;
    33 @property (readonly) NSString* path;
    34 @end
    35 #endif
    36 
    37 
    38 @interface MYKeychainItem (Private);
    39 - (id) initWithKeychainItemRef: (MYKeychainItemRef)itemRef;
    40 - (NSData*) _getContents: (OSStatus*)outError;
    41 - (NSString*) stringValueOfAttribute: (SecKeychainAttrType)attr;
    42 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
    43 + (NSData*) _getAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
    44 - (id) _attribute: (SecKeychainAttrType)attribute;
    45 + (NSString*) _getStringAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
    46 + (BOOL) _setAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item
    47            stringValue: (NSString*)stringValue;
    48 @end      
    49 
    50 
    51 @interface MYKey (Private)
    52 - (id) initWithKeyData: (NSData*)data;
    53 - (id) _initWithKeyData: (NSData*)data
    54             forKeychain: (SecKeychainRef)keychain;
    55 @property (readonly) SecExternalItemType keyType;
    56 @property (readonly) MYSHA1Digest* _keyDigest;
    57 - (NSData*) _crypt: (NSData *)data operation: (BOOL) op;    // YES to encrypt, NO to decrypt
    58 #if !MYCRYPTO_USE_IPHONE_API
    59 @property (readonly) const CSSM_KEY* cssmKey;
    60 @property (readonly) const CSSM_CSP_HANDLE cssmCSPHandle;
    61 - (CSSM_CC_HANDLE) _createSignatureContext: (CSSM_ALGORITHMS)algorithm;
    62 - (CSSM_CC_HANDLE) _createPassThroughContext;
    63 #endif
    64 @property (readonly) NSArray* _itemList;
    65 @end
    66 
    67 
    68 @interface MYSymmetricKey (Private)
    69 #if !MYCRYPTO_USE_IPHONE_API
    70 - (id) _initWithCSSMKey: (CSSM_KEY*)cssmKey;
    71 #endif
    72 + (MYSymmetricKey*) _generateSymmetricKeyOfSize: (unsigned)keySizeInBits
    73                                       algorithm: (CCAlgorithm)algorithm
    74                                      inKeychain: (MYKeychain*)keychain;
    75 @end
    76 
    77 
    78 @interface MYPublicKey (Private)
    79 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
    80 #if !TARGET_OS_IPHONE
    81 - (CSSM_WRAP_KEY*) _unwrappedCSSMKey;
    82 #endif
    83 @end
    84 
    85 
    86 @interface MYPrivateKey (Private)
    87 + (MYPrivateKey*) _generateRSAKeyPairOfSize: (unsigned)keySize
    88                                  inKeychain: (MYKeychain*)keychain;
    89 - (id) _initWithKeyRef: (SecKeyRef)privateKey
    90              publicKey: (MYPublicKey*)publicKey;
    91 - (id) _initWithKeyData: (NSData*)privKeyData 
    92           publicKeyData: (NSData*)pubKeyData
    93             forKeychain: (SecKeychainRef)keychain 
    94              alertTitle: (NSString*)title
    95             alertPrompt: (NSString*)prompt;
    96 - (id) _initWithKeyData: (NSData*)privKeyData 
    97           publicKeyData: (NSData*)pubKeyData
    98             forKeychain: (SecKeychainRef)keychain 
    99              passphrase: (NSString*)passphrase;
   100 #if !TARGET_OS_IPHONE
   101 - (NSData*) _exportKeyInFormat: (SecExternalFormat)format
   102                        withPEM: (BOOL)withPEM
   103                     passphrase: (NSString*)passphrase;
   104 #endif
   105 @end
   106 
   107 
   108 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
   109 @interface MYCertificate (Private)
   110 - (id) initWithCertificateData: (NSData*)data
   111                           type: (CSSM_CERT_TYPE) type
   112                       encoding: (CSSM_CERT_ENCODING) encoding;
   113 @end
   114 #endif
   115 
   116 
   117 #undef check
   118 BOOL check(OSStatus err, NSString *what);
   119 
   120 #define checksave(CALL) ({OSStatus err=(CALL); check(err,@""#CALL) || (_error=err, NO);})
   121 
   122 #if !MYCRYPTO_USE_IPHONE_API
   123 BOOL checkcssm(CSSM_RETURN err, NSString *what);
   124 
   125 SecKeyRef importKey(NSData *data, 
   126                     SecExternalItemType type,
   127                     SecKeychainRef keychain,
   128                     SecKeyImportExportParameters *params /*non-null*/);
   129 
   130 NSString* OIDAsString(CSSM_OID OID);
   131 CSSM_ALGORITHMS CSSMFromCCAlgorithm( CCAlgorithm ccAlgorithm );
   132 
   133 typedef struct {
   134     CSSM_ALGORITHMS algorithm;
   135     uint32 sizeInBits;
   136 } MYCryptoWrappedKeyDesc;
   137 
   138 #endif