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  */
     31 public class DigitalSignatureTest extends TestCase {
     32 
     33     private PrivateKey key;
     34     private Certificate cert;
     35 
     36     @Override
     37     public void setUp() throws Exception {
     38 
     39         char[] pwd = JSSETestData.KS_PASSWORD;
     40         KeyStore ks = JSSETestData.getKeyStore();
     41         KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) ks
     42                 .getEntry("ssl_test_store",
     43                         new KeyStore.PasswordProtection(pwd));
     44         key = entry.getPrivateKey();
     45         cert = entry.getCertificate();
     46     }
     47 
     48     public void testDigitalSignature_1() throws Exception {
     49 
     50         MessageDigest md5 = null;
     51         MessageDigest sha = null;
     52 
     53         md5 = MessageDigest.getInstance("MD5");
     54         sha = MessageDigest.getInstance("SHA-1");
     55 
     56         DigitalSignature ds_sign = new DigitalSignature(
     57                 CipherSuite.KeyExchange_RSA_EXPORT);
     58         DigitalSignature ds_verify = new DigitalSignature(
     59                 CipherSuite.KeyExchange_RSA_EXPORT);
     60         ds_sign.init(key);
     61         // use pivateKeyEncoding as signed data
     62         byte[] pivateKeyEncoding = key.getEncoded();
     63         ds_sign.update(pivateKeyEncoding);
     64         byte[] hash = ds_sign.sign();
     65 
     66         // verify
     67         byte[] md5_hash = new byte[16];
     68         byte[] sha_hash = new byte[20];
     69         sha.update(pivateKeyEncoding);
     70         md5.update(pivateKeyEncoding);
     71 
     72         sha.digest(sha_hash, 0, sha_hash.length);
     73         md5.digest(md5_hash, 0, md5_hash.length);
     74 
     75         ds_verify.init(cert);
     76         ds_verify.setMD5(md5_hash);
     77         ds_verify.setSHA(sha_hash);
     78 
     79         assertTrue(ds_verify.verifySignature(hash));
     80     }
     81 
     82     public void testDigitalSignature_2() throws Exception {
     83 
     84         DigitalSignature ds_sign = new DigitalSignature(
     85                 CipherSuite.KeyExchange_RSA_EXPORT);
     86         DigitalSignature ds_verify = new DigitalSignature(
     87                 CipherSuite.KeyExchange_RSA_EXPORT);
     88         ds_sign.init(key);
     89 
     90         byte[] md5_hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
     91                 14, 15, 16 };
     92         byte[] sha_hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
     93                 14, 15, 16, 17, 18, 19, 20 };
     94         ds_sign.setMD5(md5_hash);
     95         ds_sign.setSHA(sha_hash);
     96         byte[] hash = ds_sign.sign();
     97 
     98         // verify
     99         ds_verify.init(cert);
    100         ds_verify.setMD5(md5_hash);
    101         ds_verify.setSHA(sha_hash);
    102         assertTrue(ds_verify.verifySignature(hash));
    103     }
    104 
    105 }