001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.xnet.provider.jsse;
019:
020: import java.security.KeyStore;
021: import java.security.MessageDigest;
022: import java.security.PrivateKey;
023: import java.security.cert.Certificate;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for <code>DigitalSignature</code> constructor and methods
029: *
030: */
031: public class DigitalSignatureTest extends TestCase {
032:
033: private PrivateKey key;
034: private Certificate cert;
035:
036: public void setUp() throws Exception {
037:
038: char[] pwd = JSSETestData.KS_PASSWORD;
039: KeyStore ks = JSSETestData.getKeyStore();
040: KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) ks
041: .getEntry("ssl_test_store",
042: new KeyStore.PasswordProtection(pwd));
043: key = entry.getPrivateKey();
044: cert = entry.getCertificate();
045: }
046:
047: public void testDigitalSignature_1() throws Exception {
048:
049: MessageDigest md5 = null;
050: MessageDigest sha = null;
051:
052: md5 = MessageDigest.getInstance("MD5");
053: sha = MessageDigest.getInstance("SHA-1");
054:
055: DigitalSignature ds_sign = new DigitalSignature(
056: CipherSuite.KeyExchange_RSA_EXPORT);
057: DigitalSignature ds_verify = new DigitalSignature(
058: CipherSuite.KeyExchange_RSA_EXPORT);
059: ds_sign.init(key);
060: // use pivateKeyEncoding as signed data
061: byte[] pivateKeyEncoding = key.getEncoded();
062: ds_sign.update(pivateKeyEncoding);
063: byte[] hash = ds_sign.sign();
064:
065: // verify
066: byte[] md5_hash = new byte[16];
067: byte[] sha_hash = new byte[20];
068: sha.update(pivateKeyEncoding);
069: md5.update(pivateKeyEncoding);
070:
071: sha.digest(sha_hash, 0, sha_hash.length);
072: md5.digest(md5_hash, 0, md5_hash.length);
073:
074: ds_verify.init(cert);
075: ds_verify.setMD5(md5_hash);
076: ds_verify.setSHA(sha_hash);
077:
078: assertTrue(ds_verify.verifySignature(hash));
079: }
080:
081: public void testDigitalSignature_2() throws Exception {
082:
083: DigitalSignature ds_sign = new DigitalSignature(
084: CipherSuite.KeyExchange_RSA_EXPORT);
085: DigitalSignature ds_verify = new DigitalSignature(
086: CipherSuite.KeyExchange_RSA_EXPORT);
087: ds_sign.init(key);
088:
089: byte[] md5_hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
090: 11, 12, 13, 14, 15, 16 };
091: byte[] sha_hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
092: 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
093: ds_sign.setMD5(md5_hash);
094: ds_sign.setSHA(sha_hash);
095: byte[] hash = ds_sign.sign();
096:
097: // verify
098: ds_verify.init(cert);
099: ds_verify.setMD5(md5_hash);
100: ds_verify.setSHA(sha_hash);
101: assertTrue(ds_verify.verifySignature(hash));
102: }
103:
104: }
|