jdk/test/java/security/KeyPairGenerator/Failover.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10057 03ae05289550
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4894125
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test that failover for KeyPairGenerator works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Failover {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        Security.insertProviderAt(new ProviderFail(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        Security.addProvider(new ProviderPass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        System.out.println(Arrays.asList(Security.getProviders()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        KeyPairGenerator kpg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        kpg.initialize(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        kpg.initialize(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        kpg.initialize(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        kpg.initialize(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // the SUN DSA KeyPairGenerator implementation extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // KeyPairGenerator (in order to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // java.security.interfaces.DSAKeyPairGenerator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // failover cannot work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        kpg = KeyPairGenerator.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            kpg.initialize(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new Exception("no exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (InvalidParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        KeyPair kp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        kpg.initialize(768);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            kpg.initialize(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new Exception("no exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (InvalidParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static class ProviderPass extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ProviderPass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            super("Pass", 1.0d, "Pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorPass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static class ProviderFail extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        ProviderFail() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            super("Fail", 1.0d, "Fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            put("KeyPairGenerator.DSA" , "Failover$KeyPairGeneratorFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            put("KeyPairGenerator.RSA" , "Failover$KeyPairGeneratorFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static class KeyPairGeneratorPass extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public void initialize(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("KeyPairGeneratorPass.initialize(" + keysize + ", " + random + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public void initialize(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.out.println("KeyPairGeneratorPass.initialize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println("KeyPairGeneratorPass.generateKeyPair()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static class KeyPairGeneratorFail extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public void initialize(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (keysize != 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                System.out.println("KeyPairGeneratorFail.initialize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.out.println("KeyPairGeneratorFail.initialize() PASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public void initialize(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.out.println("KeyPairGeneratorFail.initialize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println("KeyPairGeneratorFail.generateKeyPair()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}