* More work on iPhone compatibility.
* Restored the signature-verification code to MYCertInfo, which I'd removed earlier. I now need it to verify self-signed certs, since the Security framework won't do it for me.
* Merged MYCertificate-iPhone.m into MYCertificate.m since there's more shared code now.
5 // Created by Jens Alfke on 3/22/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
11 #import "MYCrypto_Private.h"
12 #import <CommonCrypto/CommonDigest.h>
15 #if MYCRYPTO_USE_IPHONE_API
18 @implementation MYKeyPair
21 + (MYKeyPair*) _generateRSAKeyPairOfSize: (unsigned)keySize inKeychain: (MYKeychain*)keychain {
22 Assert( keySize == 512 || keySize == 1024 || keySize == 2048, @"Unsupported key size %u", keySize );
23 SecKeyRef pubKey=NULL, privKey=NULL;
25 NSDictionary *pubKeyAttrs = $dict({(id)kSecAttrIsPermanent, $true});
26 NSDictionary *privKeyAttrs = $dict({(id)kSecAttrIsPermanent, $true});
27 NSDictionary *keyAttrs = $dict( {(id)kSecAttrKeyType, (id)kSecAttrKeyTypeRSA},
28 {(id)kSecAttrKeySizeInBits, $object(keySize)},
29 {(id)kSecPublicKeyAttrs, pubKeyAttrs},
30 {(id)kSecPrivateKeyAttrs, privKeyAttrs} );
31 err = SecKeyGeneratePair((CFDictionaryRef)keyAttrs,&pubKey,&privKey);
33 Warn(@"Failed to create key-pair: %i", err);
36 return [[[self alloc] initWithPublicKeyRef: pubKey privateKeyRef: privKey] autorelease];
39 - (id) initWithPublicKeyRef: (SecKeyRef)publicKey privateKeyRef: (SecKeyRef)privateKey {
40 self = [super initWithKeyRef: publicKey];
42 NSParameterAssert(privateKey);
43 _privateKey = (SecKeyRef) CFRetain(privateKey);
49 - (NSArray*) _itemList {
50 return $array((id)_privateKey,(id)self.keyRef);
54 - (SecKeyRef) privateKeyRef {
59 - (NSData*) decryptData: (NSData*)data {
60 return _crypt(_privateKey,data,kCCDecrypt);
64 - (NSData*) signData: (NSData*)data {
66 uint8_t digest[CC_SHA1_DIGEST_LENGTH];
67 CC_SHA1(data.bytes,data.length, digest);
70 uint8_t sigBuf[sigLen];
71 OSStatus err = SecKeyRawSign(_privateKey, kSecPaddingPKCS1SHA1,
72 digest,sizeof(digest), //data.bytes, data.length,
75 Warn(@"SecKeyRawSign failed: %i",err);
78 return [NSData dataWithBytes: sigBuf length: sigLen];
85 #endif MYCRYPTO_USE_IPHONE_API