Home | History | Annotate | Download | only in jsse
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.xnet.provider.jsse;
     19 
     20 import java.security.KeyStore;
     21 import java.security.MessageDigest;
     22 import java.security.PrivateKey;
     23 import java.security.cert.Certificate;
     24 
     25 import junit.framework.TestCase;
     26 
     27 /**
     28  * Tests for <code>DigitalSignature</code> constructor and methods
     29  */
     30 public class DigitalSignatureTest extends TestCase {
     31 
     32     private PrivateKey key;
     33     private Certificate cert;
     34 
     35     @Override
     36     public void setUp() throws Exception {
     37 
     38         char[] pwd = JSSETestData.KS_PASSWORD;
     39         KeyStore ks = JSSETestData.getKeyStore();
     40         KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) ks
     41                 .getEntry("ssl_test_store",
     42                         new KeyStore.PasswordProtection(pwd));
     43         key = entry.getPrivateKey();
     44         cert = entry.getCertificate();
     45     }
     46 
     47     public void testDigitalSignature_1() throws Exception {
     48 
     49         MessageDigest md5 = null;
     50         MessageDigest sha = null;
     51 
     52         md5 = MessageDigest.getInstance("MD5");
     53         sha = MessageDigest.getInstance("SHA-1");
     54 
     55         DigitalSignature ds_sign = new DigitalSignature(
     56                 CipherSuite.KeyExchange_RSA_EXPORT);
     57         DigitalSignature ds_verify = new DigitalSignature(
     58                 CipherSuite.KeyExchange_RSA_EXPORT);
     59         ds_sign.init(key);
     60         // use pivateKeyEncoding as signed data
     61         byte[] pivateKeyEncoding = key.getEncoded();
     62         ds_sign.update(pivateKeyEncoding);
     63         byte[] hash = ds_sign.sign();
     64 
     65         // verify
     66         byte[] md5_hash = new byte[16];
     67         byte[] sha_hash = new byte[20];
     68         sha.update(pivateKeyEncoding);
     69         md5.update(pivateKeyEncoding);
     70 
     71         sha.digest(sha_hash, 0, sha_hash.length);
     72         md5.digest(md5_hash, 0, md5_hash.length);
     73 
     74         ds_verify.init(cert);
     75         ds_verify.setMD5(md5_hash);
     76         ds_verify.setSHA(sha_hash);
     77 
     78         assertTrue(ds_verify.verifySignature(hash));
     79     }
     80 
     81     public void testDigitalSignature_2() throws Exception {
     82 
     83         DigitalSignature ds_sign = new DigitalSignature(
     84                 CipherSuite.KeyExchange_RSA_EXPORT);
     85         DigitalSignature ds_verify = new DigitalSignature(
     86                 CipherSuite.KeyExchange_RSA_EXPORT);
     87         ds_sign.init(key);
     88 
     89         byte[] md5_hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
     90                 14, 15, 16 };
     91         byte[] sha_hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
     92                 14, 15, 16, 17, 18, 19, 20 };
     93         ds_sign.setMD5(md5_hash);
     94         ds_sign.setSHA(sha_hash);
     95         byte[] hash = ds_sign.sign();
     96 
     97         // verify
     98         ds_verify.init(cert);
     99         ds_verify.setMD5(md5_hash);
    100         ds_verify.setSHA(sha_hash);
    101         assertTrue(ds_verify.verifySignature(hash));
    102     }
    103 
    104 }