src/java.base/share/classes/com/sun/crypto/provider/PBEKeyFactory.java
author coffeys
Fri, 03 Aug 2018 14:14:59 +0100
changeset 51293 53c3b460503c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208583: Better management of internal KeyStore buffers Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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;
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    35
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class implements a key factory for PBE keys according to PKCS#5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * meaning that the password must consist of printable ASCII characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * (values 32 to 126 decimal inclusive) and only the low order 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of each password character are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
abstract class PBEKeyFactory extends SecretKeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static HashSet<String> validTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
3353
ddbd63234844 6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents: 2
diff changeset
    52
     * Simple constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private PBEKeyFactory(String keytype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        type = keytype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static {
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    59
        validTypes = new HashSet<>(17);
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    60
        validTypes.add("PBEWithMD5AndDES".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    61
        validTypes.add("PBEWithSHA1AndDESede".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    62
        validTypes.add("PBEWithSHA1AndRC2_40".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    63
        validTypes.add("PBEWithSHA1AndRC2_128".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    64
        validTypes.add("PBEWithSHA1AndRC4_40".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    65
        validTypes.add("PBEWithSHA1AndRC4_128".toUpperCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Proprietary algorithm.
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    67
        validTypes.add("PBEWithMD5AndTripleDES".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    68
        validTypes.add("PBEWithHmacSHA1AndAES_128".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    69
        validTypes.add("PBEWithHmacSHA224AndAES_128".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    70
        validTypes.add("PBEWithHmacSHA256AndAES_128".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    71
        validTypes.add("PBEWithHmacSHA384AndAES_128".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    72
        validTypes.add("PBEWithHmacSHA512AndAES_128".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    73
        validTypes.add("PBEWithHmacSHA1AndAES_256".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    74
        validTypes.add("PBEWithHmacSHA224AndAES_256".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    75
        validTypes.add("PBEWithHmacSHA256AndAES_256".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    76
        validTypes.add("PBEWithHmacSHA384AndAES_256".toUpperCase(Locale.ENGLISH));
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
    77
        validTypes.add("PBEWithHmacSHA512AndAES_256".toUpperCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static final class PBEWithMD5AndDES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public PBEWithMD5AndDES()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            super("PBEWithMD5AndDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static final class PBEWithSHA1AndDESede
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public PBEWithSHA1AndDESede()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            super("PBEWithSHA1AndDESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final class PBEWithSHA1AndRC2_40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public PBEWithSHA1AndRC2_40()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            super("PBEWithSHA1AndRC2_40");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   101
    public static final class PBEWithSHA1AndRC2_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   102
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   103
        public PBEWithSHA1AndRC2_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   104
            super("PBEWithSHA1AndRC2_128");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   108
    public static final class PBEWithSHA1AndRC4_40
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   109
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   110
        public PBEWithSHA1AndRC4_40()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   111
            super("PBEWithSHA1AndRC4_40");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   115
    public static final class PBEWithSHA1AndRC4_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   116
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   117
        public PBEWithSHA1AndRC4_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   118
            super("PBEWithSHA1AndRC4_128");
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
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   121
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Private proprietary algorithm for supporting JCEKS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static final class PBEWithMD5AndTripleDES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public PBEWithMD5AndTripleDES()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            super("PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   132
    public static final class PBEWithHmacSHA1AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   133
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   134
        public PBEWithHmacSHA1AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   135
            super("PBEWithHmacSHA1AndAES_128");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   139
    public static final class PBEWithHmacSHA224AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   140
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   141
        public PBEWithHmacSHA224AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   142
            super("PBEWithHmacSHA224AndAES_128");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   146
    public static final class PBEWithHmacSHA256AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   147
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   148
        public PBEWithHmacSHA256AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   149
            super("PBEWithHmacSHA256AndAES_128");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   153
    public static final class PBEWithHmacSHA384AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   154
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   155
        public PBEWithHmacSHA384AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   156
            super("PBEWithHmacSHA384AndAES_128");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   160
    public static final class PBEWithHmacSHA512AndAES_128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   161
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   162
        public PBEWithHmacSHA512AndAES_128()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   163
            super("PBEWithHmacSHA512AndAES_128");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   167
    public static final class PBEWithHmacSHA1AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   168
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   169
        public PBEWithHmacSHA1AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   170
            super("PBEWithHmacSHA1AndAES_256");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   174
    public static final class PBEWithHmacSHA224AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   175
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   176
        public PBEWithHmacSHA224AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   177
            super("PBEWithHmacSHA224AndAES_256");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   181
    public static final class PBEWithHmacSHA256AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   182
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   183
        public PBEWithHmacSHA256AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   184
            super("PBEWithHmacSHA256AndAES_256");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   188
    public static final class PBEWithHmacSHA384AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   189
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   190
        public PBEWithHmacSHA384AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   191
            super("PBEWithHmacSHA384AndAES_256");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   195
    public static final class PBEWithHmacSHA512AndAES_256
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   196
            extends PBEKeyFactory {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   197
        public PBEWithHmacSHA512AndAES_256()  {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   198
            super("PBEWithHmacSHA512AndAES_256");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   199
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
   200
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Generates a <code>SecretKey</code> object from the provided key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * specification (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param keySpec the specification (key material) of the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    protected SecretKey engineGenerateSecret(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throws InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (!(keySpec instanceof PBEKeySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new InvalidKeySpecException("Invalid key spec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 47216
diff changeset
   219
        return new PBEKey((PBEKeySpec)keySpec, type, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Returns a specification (key material) of the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   238
    protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if ((key instanceof SecretKey)
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
   241
            && (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH)))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            && (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // Check if requested key spec is amongst the valid ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if ((keySpecCl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                && PBEKeySpec.class.isAssignableFrom(keySpecCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                byte[] passwdBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                char[] passwdChars = new char[passwdBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                for (int i=0; i<passwdChars.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                PBEKeySpec ret = new PBEKeySpec(passwdChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                // password char[] was cloned in PBEKeySpec constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                // so we can zero it out here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                java.util.Arrays.fill(passwdChars, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                java.util.Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                throw new InvalidKeySpecException("Invalid key spec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new InvalidKeySpecException("Invalid key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                              + "format/algorithm");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Translates a <code>SecretKey</code> object, whose provider may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * unknown or potentially untrusted, into a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>SecretKey</code> object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception InvalidKeyException if the given key cannot be processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    protected SecretKey engineTranslateKey(SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if ((key != null) &&
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14405
diff changeset
   283
                (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (key instanceof com.sun.crypto.provider.PBEKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    (key, PBEKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return engineGenerateSecret(pbeKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                throw new InvalidKeyException("Invalid key format/algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } catch (InvalidKeySpecException ikse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new InvalidKeyException("Cannot translate key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                          + ikse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}