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 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
29 @interface MYKeychain (Private)
30 - (id) initWithKeychainRef: (SecKeychainRef)keychainRef;
31 @property (readonly) SecKeychainRef keychainRef, keychainRefOrDefault;
32 @property (readonly) CSSM_CSP_HANDLE CSPHandle;
33 @property (readonly) NSString* path;
38 @interface MYKeychainItem (Private);
39 - (id) initWithKeychainItemRef: (MYKeychainItemRef)itemRef;
40 - (NSData*) _getContents: (OSStatus*)outError;
41 - (NSString*) stringValueOfAttribute: (SecKeychainAttrType)attr;
42 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
43 + (NSData*) _getAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
44 - (id) _attribute: (SecKeychainAttrType)attribute;
45 + (NSString*) _getStringAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item;
46 + (BOOL) _setAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item
47 stringValue: (NSString*)stringValue;
51 @interface MYKey (Private)
52 - (id) initWithKeyData: (NSData*)data;
53 - (id) _initWithKeyData: (NSData*)data
54 forKeychain: (SecKeychainRef)keychain;
55 @property (readonly) SecExternalItemType keyType;
56 @property (readonly) MYSHA1Digest* _keyDigest;
57 - (NSData*) _crypt: (NSData *)data operation: (BOOL) op; // YES to encrypt, NO to decrypt
58 #if !MYCRYPTO_USE_IPHONE_API
59 @property (readonly) const CSSM_KEY* cssmKey;
60 - (NSData*) exportKeyInFormat: (SecExternalFormat)format withPEM: (BOOL)withPEM;
61 - (CSSM_CC_HANDLE) _createSignatureContext: (CSSM_ALGORITHMS)algorithm;
62 - (CSSM_CC_HANDLE) _createPassThroughContext;
64 @property (readonly) NSArray* _itemList;
68 @interface MYSymmetricKey (Private)
69 + (MYSymmetricKey*) _generateSymmetricKeyOfSize: (unsigned)keySizeInBits
70 algorithm: (CCAlgorithm)algorithm
71 inKeychain: (MYKeychain*)keychain;
75 @interface MYPublicKey (Private)
76 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr;
78 - (CSSM_WRAP_KEY*) _unwrappedCSSMKey;
83 @interface MYPrivateKey (Private)
84 + (MYPrivateKey*) _generateRSAKeyPairOfSize: (unsigned)keySize
85 inKeychain: (MYKeychain*)keychain;
86 - (id) _initWithKeyRef: (SecKeyRef)privateKey
87 publicKey: (MYPublicKey*)publicKey;
88 - (id) _initWithKeyData: (NSData*)privKeyData
89 publicKeyData: (NSData*)pubKeyData
90 forKeychain: (SecKeychainRef)keychain
91 alertTitle: (NSString*)title
92 alertPrompt: (NSString*)prompt;
93 - (id) _initWithKeyData: (NSData*)privKeyData
94 publicKeyData: (NSData*)pubKeyData
95 forKeychain: (SecKeychainRef)keychain
96 passphrase: (NSString*)passphrase;
98 - (NSData*) _exportKeyInFormat: (SecExternalFormat)format
99 withPEM: (BOOL)withPEM
100 passphrase: (NSString*)passphrase;
105 #if TARGET_OS_IPHONE && !MYCRYPTO_USE_IPHONE_API
106 @interface MYCertificate (Private)
107 - (id) initWithCertificateData: (NSData*)data
108 type: (CSSM_CERT_TYPE) type
109 encoding: (CSSM_CERT_ENCODING) encoding;
115 BOOL check(OSStatus err, NSString *what);
117 #define checksave(CALL) ({OSStatus err=(CALL); check(err,@""#CALL) || (_error=err, NO);})
119 #if !MYCRYPTO_USE_IPHONE_API
120 BOOL checkcssm(CSSM_RETURN err, NSString *what);
122 SecKeyRef importKey(NSData *data,
123 SecExternalItemType type,
124 SecKeychainRef keychain,
125 SecKeyImportExportParameters *params /*non-null*/);
127 NSString* OIDAsString(CSSM_OID OID);