jdk/src/share/classes/com/sun/crypto/provider/PBEKeyFactory.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 14405 e7fff80005c1
child 25402 0c24d9aa8fb9
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
     2
 * Copyright (c) 1997, 2012, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.SecretKeyFactorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.spec.PBEKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements a key factory for PBE keys according to PKCS#5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * meaning that the password must consist of printable ASCII characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (values 32 to 126 decimal inclusive) and only the low order 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of each password character are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
abstract class PBEKeyFactory extends SecretKeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static HashSet<String> validTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
3353
ddbd63234844 6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents: 2
diff changeset
    51
     * Simple constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private PBEKeyFactory(String keytype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        type = keytype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    58
        validTypes = new HashSet<String>(17);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        validTypes.add("PBEWithMD5AndDES".toUpperCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        validTypes.add("PBEWithSHA1AndDESede".toUpperCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        validTypes.add("PBEWithSHA1AndRC2_40".toUpperCase());
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    62
        validTypes.add("PBEWithSHA1AndRC2_128".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    63
        validTypes.add("PBEWithSHA1AndRC4_40".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    64
        validTypes.add("PBEWithSHA1AndRC4_128".toUpperCase());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // Proprietary algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        validTypes.add("PBEWithMD5AndTripleDES".toUpperCase());
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    67
        validTypes.add("PBEWithHmacSHA1AndAES_128".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    68
        validTypes.add("PBEWithHmacSHA224AndAES_128".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    69
        validTypes.add("PBEWithHmacSHA256AndAES_128".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    70
        validTypes.add("PBEWithHmacSHA384AndAES_128".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    71
        validTypes.add("PBEWithHmacSHA512AndAES_128".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    72
        validTypes.add("PBEWithHmacSHA1AndAES_256".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    73
        validTypes.add("PBEWithHmacSHA224AndAES_256".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    74
        validTypes.add("PBEWithHmacSHA256AndAES_256".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    75
        validTypes.add("PBEWithHmacSHA384AndAES_256".toUpperCase());
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    76
        validTypes.add("PBEWithHmacSHA512AndAES_256".toUpperCase());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final class PBEWithMD5AndDES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public PBEWithMD5AndDES()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            super("PBEWithMD5AndDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final class PBEWithSHA1AndDESede
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public PBEWithSHA1AndDESede()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            super("PBEWithSHA1AndDESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final class PBEWithSHA1AndRC2_40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public PBEWithSHA1AndRC2_40()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            super("PBEWithSHA1AndRC2_40");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   100
    public static final class PBEWithSHA1AndRC2_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   101
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   102
        public PBEWithSHA1AndRC2_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   103
            super("PBEWithSHA1AndRC2_128");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   104
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   105
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   106
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   107
    public static final class PBEWithSHA1AndRC4_40
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   108
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   109
        public PBEWithSHA1AndRC4_40()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   110
            super("PBEWithSHA1AndRC4_40");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   111
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   112
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   113
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   114
    public static final class PBEWithSHA1AndRC4_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   115
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   116
        public PBEWithSHA1AndRC4_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   117
            super("PBEWithSHA1AndRC4_128");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   118
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   119
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   120
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Private proprietary algorithm for supporting JCEKS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static final class PBEWithMD5AndTripleDES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        public PBEWithMD5AndTripleDES()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            super("PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   131
    public static final class PBEWithHmacSHA1AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   132
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   133
        public PBEWithHmacSHA1AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   134
            super("PBEWithHmacSHA1AndAES_128");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   135
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   136
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   137
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   138
    public static final class PBEWithHmacSHA224AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   139
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   140
        public PBEWithHmacSHA224AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   141
            super("PBEWithHmacSHA224AndAES_128");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   142
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   143
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   144
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   145
    public static final class PBEWithHmacSHA256AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   146
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   147
        public PBEWithHmacSHA256AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   148
            super("PBEWithHmacSHA256AndAES_128");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   149
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   150
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   151
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   152
    public static final class PBEWithHmacSHA384AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   153
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   154
        public PBEWithHmacSHA384AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   155
            super("PBEWithHmacSHA384AndAES_128");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   156
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   157
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   158
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   159
    public static final class PBEWithHmacSHA512AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   160
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   161
        public PBEWithHmacSHA512AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   162
            super("PBEWithHmacSHA512AndAES_128");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   163
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   164
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   165
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   166
    public static final class PBEWithHmacSHA1AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   167
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   168
        public PBEWithHmacSHA1AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   169
            super("PBEWithHmacSHA1AndAES_256");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   170
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   171
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   172
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   173
    public static final class PBEWithHmacSHA224AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   174
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   175
        public PBEWithHmacSHA224AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   176
            super("PBEWithHmacSHA224AndAES_256");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   177
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   178
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   179
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   180
    public static final class PBEWithHmacSHA256AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   181
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   182
        public PBEWithHmacSHA256AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   183
            super("PBEWithHmacSHA256AndAES_256");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   184
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   185
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   186
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   187
    public static final class PBEWithHmacSHA384AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   188
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   189
        public PBEWithHmacSHA384AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   190
            super("PBEWithHmacSHA384AndAES_256");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   191
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   192
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   193
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   194
    public static final class PBEWithHmacSHA512AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   195
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   196
        public PBEWithHmacSHA512AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   197
            super("PBEWithHmacSHA512AndAES_256");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   198
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   199
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Generates a <code>SecretKey</code> object from the provided key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * specification (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param keySpec the specification (key material) of the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    protected SecretKey engineGenerateSecret(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (!(keySpec instanceof PBEKeySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new InvalidKeySpecException("Invalid key spec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return new PBEKey((PBEKeySpec)keySpec, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns a specification (key material) of the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   237
    protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if ((key instanceof SecretKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            && (validTypes.contains(key.getAlgorithm().toUpperCase()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            && (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // Check if requested key spec is amongst the valid ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if ((keySpecCl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                && PBEKeySpec.class.isAssignableFrom(keySpecCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                byte[] passwdBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                char[] passwdChars = new char[passwdBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                for (int i=0; i<passwdChars.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                PBEKeySpec ret = new PBEKeySpec(passwdChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // password char[] was cloned in PBEKeySpec constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                // so we can zero it out here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                java.util.Arrays.fill(passwdChars, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                java.util.Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throw new InvalidKeySpecException("Invalid key spec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new InvalidKeySpecException("Invalid key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                              + "format/algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Translates a <code>SecretKey</code> object, whose provider may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * unknown or potentially untrusted, into a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>SecretKey</code> object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception InvalidKeyException if the given key cannot be processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    protected SecretKey engineTranslateKey(SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if ((key != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                (validTypes.contains(key.getAlgorithm().toUpperCase())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (key instanceof com.sun.crypto.provider.PBEKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    (key, PBEKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return engineGenerateSecret(pbeKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                throw new InvalidKeyException("Invalid key format/algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } catch (InvalidKeySpecException ikse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new InvalidKeyException("Cannot translate key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                          + ikse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}