MYCryptoTest.m
changeset 27 d0aadddb9c64
parent 23 39fec79de6e8
     1.1 --- a/MYCryptoTest.m	Sun Jun 07 21:53:56 2009 -0700
     1.2 +++ b/MYCryptoTest.m	Tue Jul 21 10:13:08 2009 -0700
     1.3 @@ -43,6 +43,16 @@
     1.4  }
     1.5  
     1.6  
     1.7 +#if MYCRYPTO_USE_IPHONE_API
     1.8 +TestCase(RemoveAll) {
     1.9 +    RequireTestCase(MYKeychain);
    1.10 +    MYKeychain *kc = [MYKeychain defaultKeychain];
    1.11 +    CAssert([kc removeAllCertificates]);
    1.12 +    CAssert([kc removeAllKeys]);
    1.13 +}
    1.14 +#endif
    1.15 +
    1.16 +
    1.17  TestCase(Enumerate) {
    1.18      RequireTestCase(EnumeratePublicKeys);
    1.19      RequireTestCase(EnumeratePrivateKeys);
    1.20 @@ -95,8 +105,26 @@
    1.21      Log(@"Enumerator = %@", e);
    1.22      CAssert(e);
    1.23      for (MYCertificate *cert in e) {
    1.24 -        Log(@"Found %@ -- name=%@, email=%@", cert, cert.commonName, cert.emailAddresses);
    1.25 +        Log(@"Found %@ -- key=%@, name=%@, email=%@", cert,
    1.26 +            cert.publicKey, cert.commonName, cert.emailAddresses);
    1.27          CAssert(cert.publicKey);
    1.28 +        
    1.29 +#if MYCRYPTO_USE_IPHONE_API
    1.30 +        // Verify that cert has public-key-hash attribute:
    1.31 +        NSData *digestData = [MYKeychainItem _getAttribute: kSecAttrPublicKeyHash 
    1.32 +                                                    ofItem: cert.certificateRef];
    1.33 +        CAssert(digestData, @"SecCertificateRef missing kSecAttrPublicKeyHash");
    1.34 +        MYSHA1Digest *digest = [MYSHA1Digest digestFromDigestData: digestData];
    1.35 +        CAssertEqual(digest, cert.publicKey.publicKeyDigest);
    1.36 +        Log(@"kSecAttrPublicKeyHash matches: %@", digest);
    1.37 +#else
    1.38 +        MYSHA1Digest *digest = cert.publicKey.publicKeyDigest;
    1.39 +#endif
    1.40 +        // Verify that certificateWithDigest will find it:
    1.41 +        MYCertificate *cert2 = [[MYKeychain allKeychains] certificateWithDigest: digest];
    1.42 +        Log(@"certificateWithDigest(%@) returned %@", digest,cert2);
    1.43 +        CAssert(cert2);
    1.44 +        CAssertEqual(cert2.certificateData, cert.certificateData);
    1.45      }
    1.46  }
    1.47  
    1.48 @@ -108,12 +136,15 @@
    1.49      for (MYIdentity *ident in e) {
    1.50          Log(@"Found %@\n\tcommonName=%@\n\temails=(%@)\n\tkey=%@",
    1.51              ident, ident.commonName, 
    1.52 -#if TARGET_OS_IPHONE
    1.53 -            nil,
    1.54 -#else
    1.55              [ident.emailAddresses componentsJoinedByString: @", "],
    1.56 -#endif
    1.57              ident.privateKey);
    1.58 +        
    1.59 +        // Verify that identityWithDigest will find it:
    1.60 +        MYSHA1Digest *digest = ident.publicKey.publicKeyDigest;
    1.61 +        MYIdentity *ident2 = [[MYKeychain allKeychains] identityWithDigest:digest];
    1.62 +        Log(@"identityWithDigest(%@) returned %@", digest,ident2);
    1.63 +        CAssert(ident2);
    1.64 +        CAssertEqual(ident2.certificateData, ident.certificateData);
    1.65      }
    1.66  }
    1.67  
    1.68 @@ -177,7 +208,6 @@
    1.69      #endif
    1.70  
    1.71      #if !TARGET_OS_IPHONE
    1.72 -#if 1 // TEMP-ORARILY OUT OF ORDER
    1.73          // Try exporting and importing a wrapped key:
    1.74          Log(@"Testing export/import...");
    1.75          NSData *exported = [key exportWrappedKeyWithPassphrasePrompt: @"Export symmetric key with passphrase:"];
    1.76 @@ -197,7 +227,6 @@
    1.77              decrypted = [key2 decryptData: encrypted];
    1.78              CAssertEqual(decrypted, cleartext);
    1.79          }
    1.80 -#endif 0
    1.81      #endif
    1.82      }@finally{
    1.83          [key removeFromKeychain];