Changed the X.509 version number in generated certs from 1 to 3, so that SecCertificateCreateFromData on iPhone will accept them. :-/
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"
17 #import <Security/Security.h>
20 #if MYCRYPTO_USE_IPHONE_API
21 typedef CFTypeRef SecKeychainAttrType;
22 typedef CFTypeRef SecKeychainItemRef;
23 typedef CFTypeRef SecKeychainRef;
24 typedef CFTypeRef SecExternalItemType;
28 @interface MYKeychain (Private)
29 - (MYIdentity*) identityWithDigest: (MYSHA1Digest*)pubKeyDigest;
30 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
31 - (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
32 @property (readonly) SecKeychainRef keychainRef, keychainRefOrDefault;
33 @property (readonly) CSSM_CSP_HANDLE CSPHandle;
34 @property (readonly) NSString* path;
39 @interface MYKeychainItem (Private);
40 - (id) initWithKeychainItemRef: (MYKeychainItemRef)itemRef;
41 - (NSData*) _getContents: (OSStatus*)outError;
42 - (NSString*) stringValueOfAttribute: (SecKeychainAttrType)attr;
43 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
44 + (NSData*) _getAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
45 - (id) _attribute: (SecKeychainAttrType)attribute;
46 + (NSString*) _getStringAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
47 + (BOOL) _setAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item
48 stringValue: (NSString*)stringValue;
52 @interface MYKey (Private)
53 - (id) initWithKeyData: (NSData*)data;
54 - (id) _initWithKeyData: (NSData*)data
55 forKeychain: (SecKeychainRef)keychain;
56 @property (readonly) SecExternalItemType keyClass, keyType;
57 @property (readonly) MYSHA1Digest* _keyDigest;
58 - (NSData*) _crypt: (NSData *)data operation: (BOOL) op; // YES to encrypt, NO to decrypt
59 #if MYCRYPTO_USE_IPHONE_API
60 + (SecKeyRef) _addKeyWithInfo: (NSMutableDictionary*)info;
62 @property (readonly) const CSSM_KEY* cssmKey;
63 @property (readonly) const CSSM_CSP_HANDLE cssmCSPHandle;
64 - (CSSM_CC_HANDLE) _createSignatureContext: (CSSM_ALGORITHMS)algorithm;
65 - (CSSM_CC_HANDLE) _createPassThroughContext;
67 @property (readonly) NSArray* _itemList;
71 @interface MYSymmetricKey (Private)
72 #if !MYCRYPTO_USE_IPHONE_API
73 - (id) _initWithCSSMKey: (CSSM_KEY*)cssmKey;
75 + (MYSymmetricKey*) _generateSymmetricKeyOfSize: (unsigned)keySizeInBits
76 algorithm: (CCAlgorithm)algorithm
77 inKeychain: (MYKeychain*)keychain;
81 @interface MYPublicKey (Private)
82 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
84 - (CSSM_WRAP_KEY*) _unwrappedCSSMKey;
89 @interface MYPrivateKey (Private)
90 + (MYPrivateKey*) _generateRSAKeyPairOfSize: (unsigned)keySize
91 inKeychain: (MYKeychain*)keychain;
92 - (id) _initWithKeyRef: (SecKeyRef)privateKey
93 publicKey: (MYPublicKey*)publicKey;
94 - (id) _initWithKeyData: (NSData*)privKeyData
95 publicKeyData: (NSData*)pubKeyData
96 forKeychain: (SecKeychainRef)keychain
97 alertTitle: (NSString*)title
98 alertPrompt: (NSString*)prompt;
99 - (id) _initWithKeyData: (NSData*)privKeyData
100 publicKeyData: (NSData*)pubKeyData
101 forKeychain: (SecKeychainRef)keychain
102 passphrase: (NSString*)passphrase;
103 #if !TARGET_OS_IPHONE
104 - (NSData*) _exportKeyInFormat: (SecExternalFormat)format
105 withPEM: (BOOL)withPEM
106 passphrase: (NSString*)passphrase;
111 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
112 @interface MYCertificate (Private)
113 - (id) initWithCertificateData: (NSData*)data
114 type: (CSSM_CERT_TYPE) type
115 encoding: (CSSM_CERT_ENCODING) encoding;
121 BOOL check(OSStatus err, NSString *what);
123 #define checksave(CALL) ({OSStatus err=(CALL); check(err,@""#CALL) || (_error=err, NO);})
125 #if !MYCRYPTO_USE_IPHONE_API
126 BOOL checkcssm(CSSM_RETURN err, NSString *what);
128 SecKeyRef importKey(NSData *data,
129 SecExternalItemType type,
130 SecKeychainRef keychain,
131 SecKeyImportExportParameters *params /*non-null*/);
133 NSString* OIDAsString(CSSM_OID OID);
134 CSSM_ALGORITHMS CSSMFromCCAlgorithm( CCAlgorithm ccAlgorithm );
137 CSSM_ALGORITHMS algorithm;
139 } MYCryptoWrappedKeyDesc;