MYCrypto_Private.h
author Jens Alfke <jens@mooseyard.com>
Sat Jun 06 15:01:28 2009 -0700 (2009-06-06)
changeset 21 2c300b15b381
parent 14 3af1d1c0ceb5
child 23 39fec79de6e8
permissions -rw-r--r--
* Created class MYCertificateRequest, factored out of MYCertificateInfo.
* Added method to create a MYIdentity directly from a MYCertificateRequest.
* Added raw modulus+exponent accessor and initializer for MYPublicKey.
* Removed obsolete MYCertGen code, and the MYPrivateKey identity-creation method that used it.
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
jens@21
    28
@interface MYKeychain (Private)
jens@21
    29
- (MYIdentity*) identityWithDigest: (MYSHA1Digest*)pubKeyDigest;
snej@2
    30
#if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
snej@1
    31
- (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
snej@1
    32
@property (readonly) SecKeychainRef keychainRef, keychainRefOrDefault;
snej@1
    33
@property (readonly) CSSM_CSP_HANDLE CSPHandle;
snej@1
    34
@property (readonly) NSString* path;
jens@21
    35
#endif
snej@1
    36
@end
snej@1
    37
snej@1
    38
snej@0
    39
@interface MYKeychainItem (Private);
snej@1
    40
- (id) initWithKeychainItemRef: (MYKeychainItemRef)itemRef;
snej@0
    41
- (NSData*) _getContents: (OSStatus*)outError;
snej@0
    42
- (NSString*) stringValueOfAttribute: (SecKeychainAttrType)attr;
snej@0
    43
- (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
snej@0
    44
+ (NSData*) _getAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
snej@0
    45
- (id) _attribute: (SecKeychainAttrType)attribute;
snej@0
    46
+ (NSString*) _getStringAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
snej@0
    47
+ (BOOL) _setAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item
snej@0
    48
           stringValue: (NSString*)stringValue;
snej@0
    49
@end      
snej@0
    50
snej@0
    51
snej@0
    52
@interface MYKey (Private)
snej@1
    53
- (id) initWithKeyData: (NSData*)data;
snej@0
    54
- (id) _initWithKeyData: (NSData*)data
snej@0
    55
            forKeychain: (SecKeychainRef)keychain;
snej@1
    56
@property (readonly) SecExternalItemType keyType;
snej@3
    57
@property (readonly) MYSHA1Digest* _keyDigest;
snej@3
    58
- (NSData*) _crypt: (NSData *)data operation: (BOOL) op;    // YES to encrypt, NO to decrypt
snej@2
    59
#if !MYCRYPTO_USE_IPHONE_API
snej@0
    60
@property (readonly) const CSSM_KEY* cssmKey;
snej@12
    61
@property (readonly) const CSSM_CSP_HANDLE cssmCSPHandle;
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@13
    70
#if !MYCRYPTO_USE_IPHONE_API
snej@13
    71
- (id) _initWithCSSMKey: (CSSM_KEY*)cssmKey;
snej@13
    72
#endif
snej@0
    73
+ (MYSymmetricKey*) _generateSymmetricKeyOfSize: (unsigned)keySizeInBits
snej@0
    74
                                      algorithm: (CCAlgorithm)algorithm
snej@0
    75
                                     inKeychain: (MYKeychain*)keychain;
snej@0
    76
@end
snej@0
    77
snej@0
    78
snej@0
    79
@interface MYPublicKey (Private)
snej@0
    80
- (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
snej@4
    81
#if !TARGET_OS_IPHONE
snej@4
    82
- (CSSM_WRAP_KEY*) _unwrappedCSSMKey;
snej@4
    83
#endif
snej@0
    84
@end
snej@0
    85
snej@0
    86
snej@3
    87
@interface MYPrivateKey (Private)
snej@3
    88
+ (MYPrivateKey*) _generateRSAKeyPairOfSize: (unsigned)keySize
snej@3
    89
                                 inKeychain: (MYKeychain*)keychain;
snej@4
    90
- (id) _initWithKeyRef: (SecKeyRef)privateKey
snej@4
    91
             publicKey: (MYPublicKey*)publicKey;
snej@3
    92
- (id) _initWithKeyData: (NSData*)privKeyData 
snej@3
    93
          publicKeyData: (NSData*)pubKeyData
snej@3
    94
            forKeychain: (SecKeychainRef)keychain 
snej@3
    95
             alertTitle: (NSString*)title
snej@3
    96
            alertPrompt: (NSString*)prompt;
snej@3
    97
- (id) _initWithKeyData: (NSData*)privKeyData 
snej@3
    98
          publicKeyData: (NSData*)pubKeyData
snej@3
    99
            forKeychain: (SecKeychainRef)keychain 
snej@3
   100
             passphrase: (NSString*)passphrase;
snej@0
   101
#if !TARGET_OS_IPHONE
snej@3
   102
- (NSData*) _exportKeyInFormat: (SecExternalFormat)format
snej@3
   103
                       withPEM: (BOOL)withPEM
snej@3
   104
                    passphrase: (NSString*)passphrase;
snej@0
   105
#endif
snej@0
   106
@end
snej@0
   107
snej@0
   108
snej@2
   109
#if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
snej@0
   110
@interface MYCertificate (Private)
snej@0
   111
- (id) initWithCertificateData: (NSData*)data
snej@0
   112
                          type: (CSSM_CERT_TYPE) type
snej@0
   113
                      encoding: (CSSM_CERT_ENCODING) encoding;
snej@0
   114
@end
snej@0
   115
#endif
snej@0
   116
snej@0
   117
snej@0
   118
#undef check
snej@0
   119
BOOL check(OSStatus err, NSString *what);
snej@0
   120
snej@8
   121
#define checksave(CALL) ({OSStatus err=(CALL); check(err,@""#CALL) || (_error=err, NO);})
snej@8
   122
snej@2
   123
#if !MYCRYPTO_USE_IPHONE_API
snej@0
   124
BOOL checkcssm(CSSM_RETURN err, NSString *what);
snej@0
   125
snej@0
   126
SecKeyRef importKey(NSData *data, 
snej@0
   127
                    SecExternalItemType type,
snej@0
   128
                    SecKeychainRef keychain,
snej@0
   129
                    SecKeyImportExportParameters *params /*non-null*/);
snej@8
   130
snej@8
   131
NSString* OIDAsString(CSSM_OID OID);
snej@13
   132
CSSM_ALGORITHMS CSSMFromCCAlgorithm( CCAlgorithm ccAlgorithm );
snej@8
   133
snej@14
   134
typedef struct {
snej@14
   135
    CSSM_ALGORITHMS algorithm;
snej@14
   136
    uint32 sizeInBits;
snej@14
   137
} MYCryptoWrappedKeyDesc;
snej@14
   138
snej@0
   139
#endif