MYPublicKey.m
author snej@snej.local
Thu Apr 09 22:46:48 2009 -0700 (2009-04-09)
changeset 6 2d7692f9b6b4
parent 3 1dfe820d7ebe
child 13 6fd9177eb6da
permissions -rw-r--r--
Updated the README for the 0.1 release.
     1 //
     2 //  MYPublicKey.m
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 3/21/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYPublicKey.h"
    10 #import "MYCrypto_Private.h"
    11 #import "MYDigest.h"
    12 #import "MYErrorUtils.h"
    13 #import <CommonCrypto/CommonDigest.h>
    14 
    15 
    16 #pragma mark -
    17 @implementation MYPublicKey
    18 
    19 
    20 - (void) dealloc
    21 {
    22     [_digest release];
    23     [super dealloc];
    24 }
    25 
    26 - (SecExternalItemType) keyType {
    27 #if MYCRYPTO_USE_IPHONE_API
    28     return kSecAttrKeyClassPublic;
    29 #else
    30     return kSecItemTypePublicKey;
    31 #endif
    32 }
    33 
    34 - (NSUInteger)hash {
    35     return self.publicKeyDigest.hash;
    36 }
    37 
    38 - (NSString*) description {
    39     return $sprintf(@"%@[%@]", [self class], self.publicKeyDigest.abbreviatedHexString);
    40 }
    41 
    42 - (MYSHA1Digest*) publicKeyDigest {
    43     if (!_digest)
    44         _digest = [[self _keyDigest] retain];
    45     return _digest;
    46 }
    47 
    48 #if !MYCRYPTO_USE_IPHONE_API
    49 - (NSData*) keyData {
    50     return [self exportKeyInFormat: kSecFormatOpenSSL withPEM: NO];
    51 }
    52 #endif
    53 
    54 
    55 - (NSData*) encryptData: (NSData*)data {
    56     return [self _crypt: data operation: YES];
    57 }
    58 
    59 
    60 - (BOOL) verifySignature: (NSData*)signature ofData: (NSData*)data {
    61     Assert(data);
    62     Assert(signature);
    63     
    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);
    70     return err==noErr;
    71     
    72 #else
    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)
    80         return YES;
    81     if (cssmErr != CSSMERR_CSP_VERIFY_FAILED)
    82         Warn(@"CSSM error verifying signature: %u", MYErrorName(MYCSSMErrorDomain,cssmErr));
    83     return NO;
    84 #endif
    85 }
    86 
    87 
    88 #if !TARGET_OS_IPHONE
    89 - (CSSM_WRAP_KEY*) _unwrappedCSSMKey {
    90     const CSSM_KEY *key = self.cssmKey;
    91     
    92     if (key->KeyHeader.BlobType == CSSM_KEYBLOB_WRAPPED) {
    93         Warn(@"Key is already wrapped.\n");
    94         return NULL;
    95     }
    96     
    97     if (key->KeyHeader.KeyClass != CSSM_KEYCLASS_PUBLIC_KEY)
    98         Warn(@"Warning: Null wrapping a non-public key - this is a dangerous operation.\n");
    99     
   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, 
   106                                                    NULL, NULL, NULL, 
   107                                                    CSSM_PADDING_NONE, NULL, 
   108                                                    &ccHandle),
   109                    @"CSSM_CSP_CreateSymmetricContext"))
   110         return NULL;
   111                    
   112     CSSM_WRAP_KEY *result = malloc(sizeof(CSSM_WRAP_KEY));
   113     if (!checkcssm(CSSM_WrapKey(ccHandle, credentials, key, NULL, result),
   114                       @"CSSM_WrapKey")) {
   115         free(result);
   116         result = NULL;
   117     }
   118     CSSM_DeleteContext(ccHandle);
   119     return result;
   120 }
   121 #endif
   122 
   123 
   124 @end
   125 
   126 
   127 
   128 /*
   129  Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   130  
   131  Redistribution and use in source and binary forms, with or without modification, are permitted
   132  provided that the following conditions are met:
   133  
   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
   138  distribution.
   139  
   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.
   148  */