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