Updated the README for the 0.1 release.
5 // Created by Jens Alfke on 3/21/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import "MYPublicKey.h"
10 #import "MYCrypto_Private.h"
12 #import "MYErrorUtils.h"
13 #import <CommonCrypto/CommonDigest.h>
17 @implementation MYPublicKey
26 - (SecExternalItemType) keyType {
27 #if MYCRYPTO_USE_IPHONE_API
28 return kSecAttrKeyClassPublic;
30 return kSecItemTypePublicKey;
35 return self.publicKeyDigest.hash;
38 - (NSString*) description {
39 return $sprintf(@"%@[%@]", [self class], self.publicKeyDigest.abbreviatedHexString);
42 - (MYSHA1Digest*) publicKeyDigest {
44 _digest = [[self _keyDigest] retain];
48 #if !MYCRYPTO_USE_IPHONE_API
50 return [self exportKeyInFormat: kSecFormatOpenSSL withPEM: NO];
55 - (NSData*) encryptData: (NSData*)data {
56 return [self _crypt: data operation: YES];
60 - (BOOL) verifySignature: (NSData*)signature ofData: (NSData*)data {
64 #if MYCRYPTO_USE_IPHONE_API
65 uint8_t digest[CC_SHA1_DIGEST_LENGTH];
66 CC_SHA1(data.bytes,data.length, digest);
67 OSStatus err = SecKeyRawVerify(self.keyRef, kSecPaddingPKCS1SHA1,
68 digest,sizeof(digest), //data.bytes, data.length,
69 signature.bytes, signature.length);
73 CSSM_CC_HANDLE ccHandle = [self _createSignatureContext: CSSM_ALGID_SHA256WithRSA];
74 if (!ccHandle) return NO;
75 CSSM_DATA original = {data.length, (void*)data.bytes};
76 CSSM_DATA sig = {signature.length, (void*)signature.bytes};
77 CSSM_RETURN cssmErr = CSSM_VerifyData(ccHandle, &original, 1, CSSM_ALGID_NONE, &sig);
78 CSSM_DeleteContext(ccHandle);
79 if (cssmErr == CSSM_OK)
81 if (cssmErr != CSSMERR_CSP_VERIFY_FAILED)
82 Warn(@"CSSM error verifying signature: %u", MYErrorName(MYCSSMErrorDomain,cssmErr));
89 - (CSSM_WRAP_KEY*) _unwrappedCSSMKey {
90 const CSSM_KEY *key = self.cssmKey;
92 if (key->KeyHeader.BlobType == CSSM_KEYBLOB_WRAPPED) {
93 Warn(@"Key is already wrapped.\n");
97 if (key->KeyHeader.KeyClass != CSSM_KEYCLASS_PUBLIC_KEY)
98 Warn(@"Warning: Null wrapping a non-public key - this is a dangerous operation.\n");
100 const CSSM_ACCESS_CREDENTIALS* credentials;
101 credentials = [self cssmCredentialsForOperation: CSSM_ACL_AUTHORIZATION_EXPORT_WRAPPED
102 type: kSecCredentialTypeDefault error: nil];
103 CSSM_CC_HANDLE ccHandle;
104 if (!checkcssm(CSSM_CSP_CreateSymmetricContext(self.cssmCSPHandle,
105 CSSM_ALGID_NONE, CSSM_ALGMODE_WRAP,
107 CSSM_PADDING_NONE, NULL,
109 @"CSSM_CSP_CreateSymmetricContext"))
112 CSSM_WRAP_KEY *result = malloc(sizeof(CSSM_WRAP_KEY));
113 if (!checkcssm(CSSM_WrapKey(ccHandle, credentials, key, NULL, result),
118 CSSM_DeleteContext(ccHandle);
129 Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
131 Redistribution and use in source and binary forms, with or without modification, are permitted
132 provided that the following conditions are met:
134 * Redistributions of source code must retain the above copyright notice, this list of conditions
135 and the following disclaimer.
136 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
137 and the following disclaimer in the documentation and/or other materials provided with the
140 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
141 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
142 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
143 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
144 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
145 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
146 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
147 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.