jdk/src/share/classes/com/sun/crypto/provider/PBEParameters.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 14405 e7fff80005c1
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2011, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AlgorithmParametersSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.spec.PBEParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class implements the parameter set used with password-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * encryption, which is defined in PKCS#5 as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * PBEParameter ::=  SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     salt   OCTET STRING SIZE(8),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *     iterationCount   INTEGER }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public final class PBEParameters extends AlgorithmParametersSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // the salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private byte[] salt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // the iteration count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int iCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected void engineInit(AlgorithmParameterSpec paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        throws InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
       if (!(paramSpec instanceof PBEParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
           throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
               ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
       }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    67
       this.salt = ((PBEParameterSpec)paramSpec).getSalt().clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
       this.iCount = ((PBEParameterSpec)paramSpec).getIterationCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected void engineInit(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            DerValue val = new DerValue(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw new IOException("PBE parameter parsing error: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                      + "not a sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            val.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            this.salt = val.data.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            this.iCount = val.data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    ("PBE parameter parsing error: extra data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IOException("iteration count too big");
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
    protected void engineInit(byte[] encoded, String decodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        engineInit(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   100
    protected <T extends AlgorithmParameterSpec>
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   101
            T engineGetParameterSpec(Class<T> paramSpec)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        throws InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (PBEParameterSpec.class.isAssignableFrom(paramSpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   105
            return paramSpec.cast(new PBEParameterSpec(this.salt, this.iCount));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected byte[] engineGetEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        bytes.putOctetString(this.salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        bytes.putInteger(this.iCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        out.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected byte[] engineGetEncoded(String encodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return engineGetEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns a formatted string describing the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected String engineToString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String LINE_SEP = System.getProperty("line.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String saltString = LINE_SEP + "    salt:" + LINE_SEP + "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        HexDumpEncoder encoder = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        saltString += encoder.encodeBuffer(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        saltString += "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return saltString + LINE_SEP + "    iterationCount:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            + LINE_SEP + Debug.toHexString(BigInteger.valueOf(iCount))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            + LINE_SEP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}