MYCertificate now checks validity of self-signed certs loaded from the keychain (because the Security framework doesn't validate self-signed certs.)
5 // Created by Jens Alfke on 3/23/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import "MYCryptoConfig.h"
10 #import "MYKeychain.h"
12 #import "MYSymmetricKey.h"
13 #import "MYPublicKey.h"
14 #import "MYPrivateKey.h"
15 #import "MYCertificate.h"
16 #import "MYCertificateInfo.h"
19 #import <Security/Security.h>
22 #if MYCRYPTO_USE_IPHONE_API
23 typedef CFTypeRef SecKeychainAttrType;
24 typedef CFTypeRef SecKeychainItemRef;
25 typedef CFTypeRef SecKeychainRef;
26 typedef CFTypeRef SecExternalItemType;
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;
38 #if MYCRYPTO_USE_IPHONE_API
39 + (CFTypeRef) _addItemWithInfo: (NSMutableDictionary*)info;
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;
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;
70 @property (readonly) NSArray* _itemList;
74 @interface MYSymmetricKey (Private)
75 #if !MYCRYPTO_USE_IPHONE_API
76 - (id) _initWithCSSMKey: (CSSM_KEY*)cssmKey;
78 + (MYSymmetricKey*) _generateSymmetricKeyOfSize: (unsigned)keySizeInBits
79 algorithm: (CCAlgorithm)algorithm
80 inKeychain: (MYKeychain*)keychain;
84 @interface MYPublicKey (Private)
85 @property (retain) MYCertificate *certificate;
86 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
88 - (CSSM_WRAP_KEY*) _unwrappedCSSMKey;
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;
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;
124 @interface MYCertificateInfo (Private)
125 - (NSData*) subjectPublicKeyData;
126 - (MYPublicKey*) subjectPublicKey;
127 - (NSData*) signedData;
128 - (MYOID*) signatureAlgorithmID;
129 - (NSData*) signature;
134 BOOL check(OSStatus err, NSString *what);
136 #define checksave(CALL) ({OSStatus err=(CALL); check(err,@""#CALL) || (_error=err, NO);})
138 #if !MYCRYPTO_USE_IPHONE_API
139 BOOL checkcssm(CSSM_RETURN err, NSString *what);
141 SecKeyRef importKey(NSData *data,
142 SecExternalItemType type,
143 SecKeychainRef keychain,
144 SecKeyImportExportParameters *params /*non-null*/);
146 NSString* OIDAsString(CSSM_OID OID);
147 CSSM_ALGORITHMS CSSMFromCCAlgorithm( CCAlgorithm ccAlgorithm );
150 CSSM_ALGORITHMS algorithm;
152 } MYCryptoWrappedKeyDesc;