MYCryptoTest.m
author Jens Alfke <jens@mooseyard.com>
Sun Apr 19 22:05:51 2009 -0700 (2009-04-19)
changeset 15 2ac5704e229f
parent 13 6fd9177eb6da
child 16 c409dbc4f068
permissions -rw-r--r--
Added tag v0.3 for changeset 3af1d1c0ceb5
snej@0
     1
//
snej@0
     2
//  MYCryptoTest.m
snej@0
     3
//  MYCrypto-iPhone
snej@0
     4
//
snej@0
     5
//  Created by Jens Alfke on 4/1/09.
snej@0
     6
//  Copyright 2009 Jens Alfke. All rights reserved.
snej@0
     7
//
snej@0
     8
snej@3
     9
#import "MYPublicKey.h"
snej@3
    10
#import "MYPrivateKey.h"
snej@0
    11
#import "MYKeychain.h"
snej@0
    12
#import "MYDigest.h"
snej@4
    13
#import "MYIdentity.h"
snej@12
    14
#if !TARGET_OS_IPHONE
snej@8
    15
#import "MYCrypto+Cocoa.h"
snej@12
    16
#endif
snej@0
    17
#import "MYCrypto_Private.h"
snej@0
    18
snej@0
    19
snej@0
    20
#if DEBUG
snej@0
    21
snej@11
    22
snej@11
    23
#define kTestCaseRSAKeySize 2048
snej@11
    24
snej@0
    25
#pragma mark -
snej@0
    26
#pragma mark KEYCHAIN:
snej@0
    27
snej@0
    28
snej@0
    29
TestCase(MYKeychain) {
snej@0
    30
    MYKeychain *kc = [MYKeychain defaultKeychain];
snej@0
    31
    Log(@"Default keychain = %@", kc);
snej@0
    32
    CAssert(kc);
snej@2
    33
#if !MYCRYPTO_USE_IPHONE_API
snej@0
    34
    CAssert(kc.path);
snej@0
    35
#endif
snej@0
    36
    
snej@0
    37
    kc = [MYKeychain allKeychains];
snej@0
    38
    Log(@"All-keychains = %@", kc);
snej@0
    39
    CAssert(kc);
snej@2
    40
#if !MYCRYPTO_USE_IPHONE_API
snej@0
    41
    CAssertEq(kc.path,nil);
snej@1
    42
#endif
snej@0
    43
}
snej@0
    44
snej@0
    45
snej@0
    46
TestCase(EnumerateKeys) {
snej@0
    47
    RequireTestCase(MYKeychain);
snej@0
    48
    NSEnumerator *e = [[MYKeychain allKeychains] enumeratePublicKeys];
snej@0
    49
    Log(@"Public Key Enumerator = %@", e);
snej@0
    50
    CAssert(e);
snej@0
    51
    for (MYPublicKey *key in e) {
snej@0
    52
        Log(@"Found %@ -- name=%@", key, key.name);
snej@0
    53
    }
snej@0
    54
    
snej@3
    55
    e = [[MYKeychain allKeychains] enumeratePrivateKeys];
snej@0
    56
    Log(@"Key-Pair Enumerator = %@", e);
snej@0
    57
    CAssert(e);
snej@3
    58
    for (MYPrivateKey *key in e) {
snej@0
    59
        Log(@"Found %@ -- name=%@", key, key.name);
snej@0
    60
    }
snej@0
    61
    
snej@0
    62
    e = [[MYKeychain allKeychains] enumerateSymmetricKeys];
snej@0
    63
    Log(@"Symmetric Key Enumerator = %@", e);
snej@0
    64
    CAssert(e);
snej@0
    65
    for (MYSymmetricKey *key in e) {
snej@0
    66
        Log(@"Found %@ -- name=%@", key, key.name);
snej@0
    67
    }
snej@0
    68
}
snej@0
    69
snej@0
    70
snej@0
    71
TestCase(EnumerateCerts) {
snej@0
    72
    RequireTestCase(MYKeychain);
snej@0
    73
    NSEnumerator *e = [[MYKeychain allKeychains] enumerateCertificates];
snej@0
    74
    Log(@"Enumerator = %@", e);
snej@0
    75
    CAssert(e);
snej@0
    76
    for (MYCertificate *cert in e) {
snej@0
    77
        //Log(@"Found %@ -- name=%@, email=%@", cert, cert.commonName, cert.emailAddresses);
snej@0
    78
    }
snej@0
    79
}
snej@0
    80
snej@4
    81
TestCase(EnumerateIdentities) {
snej@4
    82
    RequireTestCase(MYKeychain);
snej@4
    83
    NSEnumerator *e = [[MYKeychain allKeychains] enumerateIdentities];
snej@4
    84
    Log(@"Enumerator = %@", e);
snej@4
    85
    CAssert(e);
snej@4
    86
    for (MYIdentity *ident in e) {
snej@8
    87
        Log(@"Found %@\n\tcommonName=%@\n\temails=(%@)\n\tkey=%@",
snej@4
    88
            ident, ident.commonName, 
snej@5
    89
#if TARGET_OS_IPHONE
snej@5
    90
            nil,
snej@5
    91
#else
snej@4
    92
            [ident.emailAddresses componentsJoinedByString: @", "],
snej@5
    93
#endif
snej@4
    94
            ident.privateKey);
snej@4
    95
    }
snej@4
    96
}
snej@4
    97
snej@0
    98
snej@0
    99
#pragma mark -
snej@0
   100
#pragma mark SYMMETRIC KEYS:
snej@0
   101
snej@0
   102
snej@12
   103
static void testSymmetricKey( CCAlgorithm algorithm, unsigned sizeInBits, MYKeychain *inKeychain ) {
snej@2
   104
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
snej@12
   105
    MYSymmetricKey *key = nil;
snej@12
   106
    @try{
snej@12
   107
        Log(@"--- Testing %3u-bit #%i %s", sizeInBits, (int)algorithm,
snej@12
   108
            (inKeychain ?", in keychain" :""));
snej@12
   109
        // Generate key:
snej@12
   110
        if (inKeychain)
snej@12
   111
            key = [inKeychain generateSymmetricKeyOfSize: sizeInBits algorithm: algorithm];
snej@12
   112
        else
snej@12
   113
            key = [MYSymmetricKey generateSymmetricKeyOfSize: sizeInBits algorithm: algorithm];
snej@12
   114
        Log(@"Created %@", key);
snej@2
   115
    CAssert(key);
snej@12
   116
        CAssertEq(key.algorithm, algorithm);
snej@12
   117
        CAssertEq(key.keySizeInBits, sizeInBits);
snej@12
   118
    #if !TARGET_OS_IPHONE
snej@12
   119
        CAssert(key.cssmKey != NULL);
snej@12
   120
    #endif
snej@12
   121
        
snej@12
   122
        NSData *keyData = key.keyData;
snej@12
   123
        Log(@"Key data = %@", keyData);
snej@12
   124
        CAssertEq(keyData.length, sizeInBits/8);
snej@12
   125
        
snej@12
   126
        // Encrypt a small amount of text:
snej@12
   127
        Log(@"Testing encryption / decryption ...");
snej@12
   128
        NSData *cleartext = [@"This is a test. This is only a test." dataUsingEncoding: NSUTF8StringEncoding];
snej@12
   129
        NSData *encrypted = [key encryptData: cleartext];
snej@12
   130
        Log(@"Encrypted = %u bytes: %@", encrypted.length, encrypted);
snej@12
   131
        CAssert(encrypted.length >= cleartext.length);
snej@12
   132
        NSData *decrypted = [key decryptData: encrypted];
snej@12
   133
        CAssertEqual(decrypted, cleartext);
snej@12
   134
        
snej@12
   135
        // Encrypt large binary data:
snej@12
   136
        cleartext = [NSData dataWithContentsOfFile: @"/Library/Desktop Pictures/Nature/Zen Garden.jpg"];
snej@12
   137
        CAssert(cleartext);
snej@12
   138
        encrypted = [key encryptData: cleartext];
snej@12
   139
        Log(@"Encrypted = %u bytes", encrypted.length);
snej@12
   140
        CAssert(encrypted.length >= cleartext.length);
snej@12
   141
        decrypted = [key decryptData: encrypted];
snej@12
   142
        CAssertEqual(decrypted, cleartext);
snej@12
   143
        
snej@12
   144
    #if 1
snej@12
   145
        Log(@"Testing initWithKeyData:...");
snej@12
   146
        MYSymmetricKey *key2 = [[MYSymmetricKey alloc] initWithKeyData: keyData algorithm: algorithm];
snej@12
   147
        CAssert(key2);
snej@12
   148
        Log(@"Key from data = %@",key2);
snej@12
   149
        CAssertEqual(key2.keyData, keyData);
snej@12
   150
        CAssertEq(key2.algorithm, algorithm);
snej@12
   151
        CAssertEq(key2.keySizeInBits, sizeInBits);
snej@2
   152
        decrypted = [key2 decryptData: encrypted];
snej@2
   153
        CAssertEqual(decrypted, cleartext);
snej@12
   154
        [key2 release];
snej@12
   155
    #endif
snej@12
   156
snej@12
   157
    #if !TARGET_OS_IPHONE
snej@13
   158
#if 0 // TEMPORARILY OUT OF ORDER
snej@12
   159
        // Try exporting and importing a wrapped key:
snej@12
   160
        Log(@"Testing export/import...");
snej@13
   161
        NSData *exported = [key exportWrappedKeyWithPassphrasePrompt: @"Export symmetric key with passphrase:"];
snej@12
   162
        Log(@"Exported key: %@", exported);
snej@13
   163
    #if 1
snej@12
   164
        CAssert(exported);
snej@12
   165
    #else
snej@12
   166
        //FIX: Exporting symmetric keys isn't working. Temporarily making this optional.
snej@12
   167
        if (!exported)
snej@12
   168
            Warn(@"Unable to export wrapped key");
snej@12
   169
        else
snej@12
   170
    #endif
snej@12
   171
        {
snej@12
   172
            CAssert(exported);
snej@12
   173
            MYSymmetricKey *key2 = [[MYSymmetricKey alloc] initWithKeyData: exported algorithm: algorithm];
snej@12
   174
            Log(@"Reconstituted as %@", key2);
snej@12
   175
            CAssertEqual(key2.keyData,key.keyData);
snej@12
   176
            decrypted = [key2 decryptData: encrypted];
snej@12
   177
            CAssertEqual(decrypted, cleartext);
snej@12
   178
        }
snej@13
   179
#endif 0
snej@12
   180
    #endif
snej@12
   181
    }@finally{
snej@12
   182
        [key removeFromKeychain];
snej@12
   183
    }
snej@2
   184
    [pool drain];
snej@2
   185
}
snej@2
   186
snej@2
   187
snej@0
   188
TestCase(MYSymmetricKey) {
snej@0
   189
    #define kNTests 11
snej@0
   190
    static const CCAlgorithm kTestAlgorithms[kNTests] = {
snej@0
   191
        kCCAlgorithmAES128, kCCAlgorithmAES128, kCCAlgorithmAES128,
snej@0
   192
        kCCAlgorithmDES, kCCAlgorithm3DES,
snej@0
   193
        kCCAlgorithmCAST, kCCAlgorithmCAST, kCCAlgorithmCAST,
snej@0
   194
        kCCAlgorithmRC4, kCCAlgorithmRC4, kCCAlgorithmRC4};
snej@0
   195
    
snej@0
   196
    static const unsigned kTestBitSizes[kNTests] = {
snej@0
   197
        128, 192, 256,
snej@0
   198
        64, 3*64,
snej@0
   199
        40, 80, 128,
snej@0
   200
        32, 200, 512*8};
snej@0
   201
snej@12
   202
    for (int useKeychain=0; useKeychain<=1; useKeychain++)
snej@12
   203
        for (int testNo=0; testNo<kNTests; testNo++) 
snej@12
   204
            testSymmetricKey(kTestAlgorithms[testNo], 
snej@12
   205
                             kTestBitSizes[testNo],
snej@12
   206
                             useKeychain ?[MYKeychain defaultKeychain] :nil);
snej@12
   207
}
snej@12
   208
snej@12
   209
snej@14
   210
#if !TARGET_OS_IPHONE
snej@12
   211
TestCase(MYSymmetricKeyPassphrase) {
snej@12
   212
    Log(@"Prompting for raw passphrase --");
snej@12
   213
    NSString *rawPassphrase = [MYSymmetricKey promptForPassphraseWithAlertTitle: @"Raw Passphrase Test" 
snej@12
   214
                                                                    alertPrompt: @"Enter the passphrase 'Testing':"
snej@12
   215
                                                                       creating: YES];
snej@12
   216
    Log(@"You entered: '%@'", rawPassphrase);
snej@12
   217
    CAssertEqual(rawPassphrase, @"Testing");
snej@12
   218
    
snej@12
   219
    Log(@"Prompting for passphrase for key --");
snej@12
   220
    MYSymmetricKey *key = [MYSymmetricKey generateFromUserPassphraseWithAlertTitle: @"Symmetric Key Passphrase Test Case" 
snej@12
   221
                                                                       alertPrompt: @"Please enter a passphrase to generate a key:"
snej@12
   222
                                                                          creating: YES
snej@12
   223
                                                                              salt: @"wahooma"];
snej@12
   224
    Log(@"Key from passphrase = %@", key);
snej@12
   225
    CAssert(key);
snej@12
   226
snej@12
   227
    // Encrypt a small amount of text:
snej@12
   228
    Log(@"Testing encryption / decryption ...");
snej@12
   229
    NSData *cleartext = [@"This is a test. This is only a test." dataUsingEncoding: NSUTF8StringEncoding];
snej@12
   230
    NSData *encrypted = [key encryptData: cleartext];
snej@12
   231
    Log(@"Encrypted = %u bytes: %@", encrypted.length, encrypted);
snej@12
   232
    CAssert(encrypted.length >= cleartext.length);
snej@12
   233
    NSData *decrypted = [key decryptData: encrypted];
snej@12
   234
    CAssertEqual(decrypted, cleartext);
snej@12
   235
    
snej@12
   236
    // Now test decryption by re-entered passphrase:
snej@12
   237
    Log(@"Testing decryption using re-entered passphrase...");
snej@12
   238
    MYSymmetricKey *key2 = [MYSymmetricKey generateFromUserPassphraseWithAlertTitle: @"Symmetric Key Passphrase Test Case" 
snej@12
   239
                                                                        alertPrompt: @"Please re-enter the same passphrase:" 
snej@12
   240
                                                                           creating: NO
snej@12
   241
                                                                               salt: @"wahooma"];
snej@12
   242
    Log(@"Key from passphrase = %@", key2);
snej@12
   243
    CAssert(key2);
snej@12
   244
    decrypted = [key2 decryptData: encrypted];
snej@12
   245
    CAssertEqual(decrypted, cleartext);
snej@0
   246
}
snej@14
   247
#endif
snej@0
   248
snej@0
   249
snej@0
   250
#pragma mark -
snej@0
   251
#pragma mark KEY-PAIRS:
snej@0
   252
snej@0
   253
snej@8
   254
static void TestUseKeyPair(MYPrivateKey *pair) {
snej@8
   255
    Log(@"---- TestUseKeyPair { %@ , %@ }.", pair, pair.publicKey);
snej@0
   256
    CAssert(pair);
snej@0
   257
    CAssert(pair.keyRef);
snej@3
   258
    MYPublicKey *publicKey = pair.publicKey;
snej@3
   259
    CAssert(publicKey.keyRef);
snej@0
   260
    
snej@8
   261
    NSData *pubKeyData = publicKey.keyData;
snej@8
   262
    Log(@"Public key = %@ (%u bytes)",pubKeyData,pubKeyData.length);
snej@8
   263
    CAssert(pubKeyData);
snej@8
   264
    
snej@8
   265
    MYSHA1Digest *pubKeyDigest = publicKey.publicKeyDigest;
snej@8
   266
    Log(@"Public key digest = %@",pubKeyDigest);
snej@8
   267
    CAssertEqual(pair.publicKeyDigest, pubKeyDigest);
snej@8
   268
    
snej@8
   269
    Log(@"SHA1 of pub key = %@", pubKeyData.my_SHA1Digest.asData);
snej@14
   270
    CAssertEqual(pubKeyData.my_SHA1Digest,pubKeyDigest);
snej@8
   271
    
snej@8
   272
    // Let's sign data:
snej@8
   273
    NSData *data = [@"This is a test. This is only a test!" dataUsingEncoding: NSUTF8StringEncoding];
snej@8
   274
    NSData *sig = [pair signData: data];
snej@8
   275
    Log(@"Signature = %@ (%u bytes)",sig,sig.length);
snej@8
   276
    CAssert(sig);
snej@8
   277
    CAssert( [publicKey verifySignature: sig ofData: data] );
snej@8
   278
    
snej@8
   279
    // Now let's encrypt...
snej@13
   280
    NSData *crypted = [publicKey rawEncryptData: data];
snej@8
   281
    Log(@"Encrypted = %@ (%u bytes)",crypted,crypted.length);
snej@8
   282
    CAssert(crypted);
snej@13
   283
    CAssertEqual([pair rawDecryptData: crypted], data);
snej@8
   284
    Log(@"Verified decryption.");
snej@8
   285
    
snej@8
   286
    // Test creating a standalone public key:
snej@8
   287
    MYPublicKey *pub = [[MYPublicKey alloc] initWithKeyRef: publicKey.keyRef];
snej@8
   288
    CAssert( [pub verifySignature: sig ofData: data] );
snej@8
   289
    Log(@"Verified signature.");
snej@8
   290
    
snej@8
   291
    // Test creating a public key from data:
snej@8
   292
    Log(@"Reconstituting public key from data...");
snej@8
   293
    pub = [[MYPublicKey alloc] initWithKeyData: pubKeyData];
snej@8
   294
    CAssert(pub);
snej@8
   295
    CAssertEqual(pub.keyData, pubKeyData);
snej@8
   296
    CAssertEqual(pub.publicKeyDigest, pubKeyDigest);
snej@8
   297
    CAssert( [pub verifySignature: sig ofData: data] );
snej@8
   298
    Log(@"Verified signature from reconstituted key.");
snej@8
   299
}
snej@8
   300
snej@8
   301
snej@14
   302
static void TestWrapSessionKey( MYPrivateKey *privateKey ) {
snej@14
   303
#if !TARGET_OS_IPHONE
snej@13
   304
    MYSymmetricKey *sessionKey = [MYSymmetricKey generateSymmetricKeyOfSize: 128 algorithm:kCCAlgorithmAES128];
snej@13
   305
    CAssert(sessionKey);
snej@14
   306
    NSData *cleartext = [@"This is a test. This is only a test." dataUsingEncoding: NSUTF8StringEncoding];
snej@14
   307
    NSData *encrypted = [sessionKey encryptData: cleartext];
snej@14
   308
snej@13
   309
    Log(@"Wrapping session key %@, %@", sessionKey, sessionKey.keyData);
snej@13
   310
    NSData *wrapped = [privateKey.publicKey wrapSessionKey: sessionKey];
snej@13
   311
    Log(@"Wrapped session key = %u bytes: %@", wrapped.length,wrapped);
snej@13
   312
    CAssert(wrapped.length >= 128/8);
snej@13
   313
    
snej@13
   314
    MYSymmetricKey *unwrappedKey = [privateKey unwrapSessionKey: wrapped
snej@13
   315
                                                  withAlgorithm: kCCAlgorithmAES128
snej@13
   316
                                                     sizeInBits: 128];
snej@13
   317
    Log(@"Unwrapped session key = %@, %@", unwrappedKey, unwrappedKey.keyData);
snej@13
   318
    CAssert(unwrappedKey);
snej@14
   319
    CAssertEq(unwrappedKey.algorithm, sessionKey.algorithm);
snej@14
   320
    CAssertEq(unwrappedKey.keySizeInBits, sessionKey.keySizeInBits);
snej@13
   321
    CAssertEqual(unwrappedKey.keyData, sessionKey.keyData);
snej@14
   322
snej@14
   323
    Log(@"Verifying that unwrapped key works");
snej@14
   324
    NSData *decrypted = [unwrappedKey decryptData: encrypted];
snej@14
   325
    CAssertEqual(decrypted, cleartext);
snej@14
   326
#endif
snej@13
   327
}
snej@13
   328
snej@13
   329
snej@8
   330
TestCase(MYGenerateKeyPair) {
snej@8
   331
    RequireTestCase(MYKeychain);
snej@8
   332
    
snej@8
   333
    Log(@"Generating key pair...");
snej@11
   334
    MYPrivateKey *pair = [[MYKeychain defaultKeychain] generateRSAKeyPairOfSize: kTestCaseRSAKeySize];
snej@8
   335
    MYPublicKey *publicKey = pair.publicKey;
snej@8
   336
    Log(@"...created { %@ , %@ }.", pair, publicKey);
snej@8
   337
    
snej@0
   338
    @try{
snej@8
   339
        TestUseKeyPair(pair);
snej@14
   340
        TestWrapSessionKey(pair);
snej@3
   341
        
snej@3
   342
        [pair setName: @"Test KeyPair Label"];
snej@3
   343
        CAssertEqual(pair.name, @"Test KeyPair Label");
snej@3
   344
        CAssertEqual(publicKey.name, @"Test KeyPair Label");
snej@3
   345
#if !TARGET_OS_IPHONE
snej@3
   346
        [pair setComment: @"This key-pair was generated automatically by a test case."];
snej@3
   347
        CAssertEqual(pair.comment, @"This key-pair was generated automatically by a test case.");
snej@3
   348
        CAssertEqual(publicKey.comment, @"This key-pair was generated automatically by a test case.");
snej@3
   349
#endif
snej@3
   350
        [pair setAlias: @"TestCase@mooseyard.com"];
snej@3
   351
        CAssertEqual(pair.alias, @"TestCase@mooseyard.com");
snej@3
   352
        CAssertEqual(publicKey.alias, @"TestCase@mooseyard.com");
snej@0
   353
        
snej@0
   354
        CAssert([pair removeFromKeychain]);
snej@0
   355
        Log(@"Removed key-pair.");
snej@0
   356
        pair = nil;
snej@0
   357
        
snej@0
   358
    }@finally {
snej@0
   359
        if (pair) {
snej@0
   360
            if ([pair removeFromKeychain])
snej@0
   361
                Log(@"Removed key-pair from keychain.");
snej@0
   362
            else
snej@0
   363
                Warn(@"Unable to remove test key-pair from keychain");
snej@0
   364
        }
snej@0
   365
    }
snej@0
   366
}
snej@0
   367
snej@0
   368
snej@12
   369
#if !TARGET_OS_IPHONE
snej@8
   370
TestCase(MYUseIdentity) {
snej@8
   371
    MYIdentity *me = nil;//[MYIdentity preferredIdentityForName: @"MYCryptoTest"];
snej@8
   372
    if (!me) {
snej@8
   373
        NSArray *idents = [[[MYKeychain allKeychains] enumerateIdentities] allObjects];
snej@8
   374
        SFChooseIdentityPanel *panel = [SFChooseIdentityPanel sharedChooseIdentityPanel];
snej@8
   375
        [panel setAlternateButtonTitle: @"Cancel"];
snej@8
   376
        if ([panel my_runModalForIdentities: idents 
snej@8
   377
                                    message: @"Choose an identity for the MYEncoder test case:"]
snej@8
   378
            != NSOKButton) {
snej@8
   379
            [NSException raise: NSGenericException format: @"User canceled"];
snej@8
   380
        }
snej@8
   381
        me = [panel my_identity];
snej@8
   382
        [me makePreferredIdentityForName: @"MYCryptoTest"];
snej@8
   383
    }
snej@8
   384
    CAssert(me,@"No default identity has been set up in the Keychain");
snej@8
   385
    TestUseKeyPair(me.privateKey);
snej@8
   386
}
snej@12
   387
#endif
snej@8
   388
snej@0
   389
snej@0
   390
#pragma mark -
snej@0
   391
#pragma mark KEYPAIR EXPORT:
snej@0
   392
snej@0
   393
snej@0
   394
static void testKeyPairExportWithPrompt(BOOL withPrompt) {
snej@0
   395
    MYKeychain *keychain = [MYKeychain allKeychains];
snej@0
   396
    Log(@"Generating key pair...");
snej@11
   397
    MYPrivateKey *pair = [keychain generateRSAKeyPairOfSize: kTestCaseRSAKeySize];
snej@0
   398
    CAssert(pair);
snej@0
   399
    CAssert(pair.keyRef);
snej@3
   400
    CAssert(pair.publicKey.keyRef);
snej@0
   401
    Log(@"...created pair.");
snej@0
   402
    
snej@0
   403
    @try{
snej@3
   404
        NSData *pubKeyData = pair.publicKey.keyData;
snej@11
   405
        CAssert(pubKeyData.length >= kTestCaseRSAKeySize/8);
snej@0
   406
        [pair setName: @"Test KeyPair Label"];
snej@0
   407
        CAssertEqual(pair.name, @"Test KeyPair Label");
snej@0
   408
#if !TARGET_OS_IPHONE
snej@0
   409
        [pair setComment: @"This key-pair was generated automatically by a test case."];
snej@0
   410
        CAssertEqual(pair.comment, @"This key-pair was generated automatically by a test case.");
snej@0
   411
#endif
snej@0
   412
        [pair setAlias: @"TestCase@mooseyard.com"];
snej@0
   413
        CAssertEqual(pair.alias, @"TestCase@mooseyard.com");
snej@0
   414
        
snej@0
   415
#if !TARGET_OS_IPHONE
snej@0
   416
        Log(@"Exporting key-pair...");
snej@0
   417
        NSString *passphrase = @"passphrase";
snej@0
   418
        NSData *privKeyData;
snej@0
   419
        if (withPrompt)
snej@3
   420
            privKeyData = [pair exportKey];
snej@0
   421
        else
snej@3
   422
            privKeyData = [pair _exportKeyInFormat: kSecFormatWrappedOpenSSL
snej@3
   423
                                          withPEM: YES
snej@3
   424
                                       passphrase: passphrase];
snej@0
   425
        Log(@"Exported data = %@ (%u bytes)", privKeyData,privKeyData.length);
snej@0
   426
        CAssert(privKeyData);
snej@0
   427
        [privKeyData writeToFile: @"ExportedPrivKey" atomically: YES];
snej@0
   428
#endif
snej@0
   429
        
snej@0
   430
        // Check key lookup:
snej@0
   431
        Log(@"Looking up public key of pair in keychain...");
snej@0
   432
        MYSHA1Digest *digest = pair.publicKeyDigest;
snej@0
   433
        MYPublicKey *foundKey = [keychain publicKeyWithDigest: digest];
snej@3
   434
        CAssertEqual(foundKey, pair.publicKey);
snej@3
   435
        CAssert([keychain.enumeratePublicKeys.allObjects containsObject: pair.publicKey]);
snej@3
   436
        MYPrivateKey *foundPair = [keychain privateKeyWithDigest: digest];
snej@0
   437
        CAssertEqual(foundPair, pair);
snej@3
   438
        CAssert([keychain.enumeratePrivateKeys.allObjects containsObject: pair]);
snej@0
   439
        
snej@0
   440
        Log(@"Removing key-pair from keychain...");
snej@0
   441
        CAssert([pair removeFromKeychain]);
snej@0
   442
        pair = nil;
snej@0
   443
        CAssert([keychain publicKeyWithDigest: digest] == nil);
snej@0
   444
        
snej@0
   445
#if !TARGET_OS_IPHONE
snej@0
   446
        Log(@"Importing key-pair...");
snej@0
   447
        if (withPrompt) {
snej@0
   448
            pair = [keychain importPublicKey: pubKeyData 
snej@0
   449
                                  privateKey: privKeyData];
snej@0
   450
        } else {
snej@3
   451
            pair = [[[MYPrivateKey alloc] _initWithKeyData: privKeyData
snej@3
   452
                                             publicKeyData: pubKeyData
snej@3
   453
                                               forKeychain: keychain.keychainRefOrDefault
snej@3
   454
                                                passphrase: passphrase]
snej@0
   455
                    autorelease];
snej@0
   456
        }
snej@0
   457
        CAssert(pair);
snej@3
   458
        CAssertEqual(pair.publicKey.keyData, pubKeyData);
snej@0
   459
#endif
snej@0
   460
    }@finally {
snej@0
   461
        if (pair) {
snej@0
   462
            if ([pair removeFromKeychain])
snej@0
   463
                Log(@"Removed key-pair from keychain.");
snej@0
   464
            else
snej@0
   465
                Warn(@"Unable to remove test key-pair from keychain");
snej@0
   466
        }
snej@0
   467
    }
snej@0
   468
}
snej@0
   469
snej@0
   470
TestCase(KeyPairExport) {
snej@0
   471
    RequireTestCase(MYKeychain);
snej@12
   472
    RequireTestCase(MYGenerateKeyPair);
snej@0
   473
    testKeyPairExportWithPrompt(NO);
snej@0
   474
}
snej@0
   475
snej@0
   476
TestCase(KeyPairExportWithUI) {
snej@0
   477
    RequireTestCase(KeyPairExport);
snej@0
   478
    testKeyPairExportWithPrompt(YES);
snej@0
   479
}
snej@0
   480
snej@0
   481
snej@0
   482
#endif DEBUG
snej@0
   483