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