jdk/test/sun/security/pkcs11/KeyAgreement/TestInterop.java
changeset 12201 d77ed23f4992
child 35379 1e8e336ef66b
equal deleted inserted replaced
12200:d935c2f4aeae 12201:d77ed23f4992
       
     1 /*
       
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 7146728
       
    27  * @summary Interop test for DH with secret that has a leading 0x00 byte
       
    28  * @library ..
       
    29  */
       
    30 import java.math.BigInteger;
       
    31 import java.util.*;
       
    32 
       
    33 import java.security.*;
       
    34 
       
    35 import javax.crypto.*;
       
    36 import javax.crypto.spec.*;
       
    37 
       
    38 public class TestInterop extends PKCS11Test {
       
    39 
       
    40     private final static BigInteger p = new BigInteger
       
    41     ("171718397966129586011229151993178480901904202533705695869569760169920539"
       
    42     + "80807543778874708672297590042574075430109846864794139516459381007417046"
       
    43     + "27996080624930219892858374168155487210358743785481212360509485282294161"
       
    44     + "39585571568998066586304075565145536350296006867635076744949977849997684"
       
    45     + "222020336013226588207303");
       
    46 
       
    47     private final static BigInteger g = new BigInteger("2");
       
    48 
       
    49     private final static BigInteger ya = new BigInteger
       
    50     ("687709211571508809414670982463565909269384277848448625781941269577397703"
       
    51     + "73675199968849153119146758339814638228795348558483510369322822476757204"
       
    52     + "22158455966026517829008713407587339322132253724742557954802911059639161"
       
    53     + "24827916158465757962384625410294483756242900146397201260757102085985457"
       
    54     + "09397033481077351036224");
       
    55 
       
    56     private final static BigInteger xa = new BigInteger
       
    57     ("104917367119952955556289227181599819745346393858545449202252025137706135"
       
    58     + "98100778613457655440586438263591136003106529323555991109623536177695714"
       
    59     + "66884181531401472902830508361532232717792847436112280721439936797741371"
       
    60     + "245140912614191507");
       
    61 
       
    62     private final static BigInteger yb  = new BigInteger
       
    63     ("163887874871842952463100699681506173424091615364591742415764095471629919"
       
    64     + "08421025296419917755446931473037086355546823601999684501737493240373415"
       
    65     + "65608293667837249198973539289354492348897732633852665609611113031379864"
       
    66     + "58514616034107537409230452318065341748503347627733368519091332060477528"
       
    67     + "173423377887175351037810");
       
    68 
       
    69     private final static BigInteger xb = new BigInteger
       
    70     ("127757517533485947079959908591028646859165238853082197617179368337276371"
       
    71     + "51601819447716934542027725311863797141734616730248519214531856941516613"
       
    72     + "30313414180008978013330410484011186019824874948204261839391153650949864"
       
    73     + "429505597086564709");
       
    74 
       
    75     public void main(Provider prov) throws Exception {
       
    76         if (prov.getService("KeyAgreement", "DH") == null) {
       
    77             System.out.println("DH not supported, skipping");
       
    78             return;
       
    79         }
       
    80         try {
       
    81             System.out.println("testing generateSecret()");
       
    82 
       
    83             DHPublicKeySpec publicSpec;
       
    84             DHPrivateKeySpec privateSpec;
       
    85             KeyFactory kf = KeyFactory.getInstance("DH");
       
    86             KeyAgreement ka = KeyAgreement.getInstance("DH", prov);
       
    87             KeyAgreement kbSunJCE = KeyAgreement.getInstance("DH", "SunJCE");
       
    88             DHPrivateKeySpec privSpecA = new DHPrivateKeySpec(xa, p, g);
       
    89             DHPublicKeySpec pubSpecA = new DHPublicKeySpec(ya, p, g);
       
    90             PrivateKey privA = kf.generatePrivate(privSpecA);
       
    91             PublicKey pubA = kf.generatePublic(pubSpecA);
       
    92 
       
    93             DHPrivateKeySpec privSpecB = new DHPrivateKeySpec(xb, p, g);
       
    94             DHPublicKeySpec pubSpecB = new DHPublicKeySpec(yb, p, g);
       
    95             PrivateKey privB = kf.generatePrivate(privSpecB);
       
    96             PublicKey pubB = kf.generatePublic(pubSpecB);
       
    97 
       
    98             ka.init(privA);
       
    99             ka.doPhase(pubB, true);
       
   100             byte[] n1 = ka.generateSecret();
       
   101 
       
   102             kbSunJCE.init(privB);
       
   103             kbSunJCE.doPhase(pubA, true);
       
   104             byte[] n2 = kbSunJCE.generateSecret();
       
   105 
       
   106             if (Arrays.equals(n1, n2) == false) {
       
   107                 throw new Exception("values mismatch!");
       
   108             } else {
       
   109                 System.out.println("values: same");
       
   110             }
       
   111 
       
   112             System.out.println("testing generateSecret(byte[], int)");
       
   113             byte[] n3 = new byte[n1.length];
       
   114             ka.init(privB);
       
   115             ka.doPhase(pubA, true);
       
   116             int n3Len = ka.generateSecret(n3, 0);
       
   117             if (n3Len != n3.length) {
       
   118                 throw new Exception("PKCS11 Length mismatch!");
       
   119             } else System.out.println("PKCS11 Length: ok");
       
   120             byte[] n4 = new byte[n2.length];
       
   121             kbSunJCE.init(privA);
       
   122             kbSunJCE.doPhase(pubB, true);
       
   123             int n4Len = kbSunJCE.generateSecret(n4, 0);
       
   124             if (n4Len != n4.length) {
       
   125                 throw new Exception("SunJCE Length mismatch!");
       
   126             } else System.out.println("SunJCE Length: ok");
       
   127 
       
   128             if (Arrays.equals(n3, n4) == false) {
       
   129                 throw new Exception("values mismatch! ");
       
   130             } else {
       
   131                 System.out.println("values: same");
       
   132             }
       
   133         } catch (Exception ex) {
       
   134             System.out.println("Unexpected ex: " + ex);
       
   135             ex.printStackTrace();
       
   136             throw ex;
       
   137         }
       
   138     }
       
   139 
       
   140     public static void main(String[] args) throws Exception {
       
   141         main(new TestInterop());
       
   142     }
       
   143 }