0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.pkcs12;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.security.*;
0N/A
0N/Aimport sun.security.util.DerInputStream;
0N/Aimport sun.security.util.DerOutputStream;
0N/Aimport sun.security.util.DerValue;
0N/Aimport sun.security.x509.AlgorithmId;
0N/Aimport sun.security.pkcs.ParsingException;
0N/A
0N/A
0N/A/**
0N/A * A MacData type, as defined in PKCS#12.
0N/A *
0N/A * @author Sharon Liu
0N/A */
0N/A
0N/Aclass MacData {
0N/A
0N/A private String digestAlgorithmName;
0N/A private AlgorithmParameters digestAlgorithmParams;
0N/A private byte[] digest;
0N/A private byte[] macSalt;
0N/A private int iterations;
0N/A
0N/A // the ASN.1 encoded contents of this class
0N/A private byte[] encoded = null;
0N/A
0N/A /**
0N/A * Parses a PKCS#12 MAC data.
0N/A */
0N/A MacData(DerInputStream derin)
0N/A throws IOException, ParsingException
0N/A {
0N/A DerValue[] macData = derin.getSequence(2);
0N/A
0N/A // Parse the digest info
0N/A DerInputStream digestIn = new DerInputStream(macData[0].toByteArray());
0N/A DerValue[] digestInfo = digestIn.getSequence(2);
0N/A
0N/A // Parse the DigestAlgorithmIdentifier.
0N/A AlgorithmId digestAlgorithmId = AlgorithmId.parse(digestInfo[0]);
0N/A this.digestAlgorithmName = digestAlgorithmId.getName();
0N/A this.digestAlgorithmParams = digestAlgorithmId.getParameters();
0N/A // Get the digest.
0N/A this.digest = digestInfo[1].getOctetString();
0N/A
0N/A // Get the salt.
0N/A this.macSalt = macData[1].getOctetString();
0N/A
0N/A // Iterations is optional. The default value is 1.
0N/A if (macData.length > 2) {
0N/A this.iterations = macData[2].getInteger();
0N/A } else {
0N/A this.iterations = 1;
0N/A }
0N/A }
0N/A
0N/A MacData(String algName, byte[] digest, byte[] salt, int iterations)
0N/A throws NoSuchAlgorithmException
0N/A {
0N/A if (algName == null)
0N/A throw new NullPointerException("the algName parameter " +
0N/A "must be non-null");
0N/A
0N/A AlgorithmId algid = AlgorithmId.get(algName);
0N/A this.digestAlgorithmName = algid.getName();
0N/A this.digestAlgorithmParams = algid.getParameters();
0N/A
0N/A if (digest == null) {
0N/A throw new NullPointerException("the digest " +
0N/A "parameter must be non-null");
0N/A } else if (digest.length == 0) {
0N/A throw new IllegalArgumentException("the digest " +
0N/A "parameter must not be empty");
0N/A } else {
0N/A this.digest = digest.clone();
0N/A }
0N/A
0N/A this.macSalt = salt;
0N/A this.iterations = iterations;
0N/A
0N/A // delay the generation of ASN.1 encoding until
0N/A // getEncoded() is called
0N/A this.encoded = null;
0N/A
0N/A }
0N/A
0N/A MacData(AlgorithmParameters algParams, byte[] digest,
0N/A byte[] salt, int iterations) throws NoSuchAlgorithmException
0N/A {
0N/A if (algParams == null)
0N/A throw new NullPointerException("the algParams parameter " +
0N/A "must be non-null");
0N/A
0N/A AlgorithmId algid = AlgorithmId.get(algParams);
0N/A this.digestAlgorithmName = algid.getName();
0N/A this.digestAlgorithmParams = algid.getParameters();
0N/A
0N/A if (digest == null) {
0N/A throw new NullPointerException("the digest " +
0N/A "parameter must be non-null");
0N/A } else if (digest.length == 0) {
0N/A throw new IllegalArgumentException("the digest " +
0N/A "parameter must not be empty");
0N/A } else {
0N/A this.digest = digest.clone();
0N/A }
0N/A
0N/A this.macSalt = salt;
0N/A this.iterations = iterations;
0N/A
0N/A // delay the generation of ASN.1 encoding until
0N/A // getEncoded() is called
0N/A this.encoded = null;
0N/A
0N/A }
0N/A
0N/A String getDigestAlgName() {
0N/A return digestAlgorithmName;
0N/A }
0N/A
0N/A byte[] getSalt() {
0N/A return macSalt;
0N/A }
0N/A
0N/A int getIterations() {
0N/A return iterations;
0N/A }
0N/A
0N/A byte[] getDigest() {
0N/A return digest;
0N/A }
0N/A
0N/A /**
0N/A * Returns the ASN.1 encoding of this object.
0N/A * @return the ASN.1 encoding.
0N/A * @exception IOException if error occurs when constructing its
0N/A * ASN.1 encoding.
0N/A */
0N/A public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
0N/A {
0N/A if (this.encoded != null)
0N/A return this.encoded.clone();
0N/A
0N/A DerOutputStream out = new DerOutputStream();
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A
0N/A DerOutputStream tmp2 = new DerOutputStream();
0N/A // encode encryption algorithm
0N/A AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
0N/A algid.encode(tmp2);
0N/A
0N/A // encode digest data
0N/A tmp2.putOctetString(digest);
0N/A
0N/A tmp.write(DerValue.tag_Sequence, tmp2);
0N/A
0N/A // encode salt
0N/A tmp.putOctetString(macSalt);
0N/A
0N/A // encode iterations
0N/A tmp.putInteger(iterations);
0N/A
0N/A // wrap everything into a SEQUENCE
0N/A out.write(DerValue.tag_Sequence, tmp);
0N/A this.encoded = out.toByteArray();
0N/A
0N/A return this.encoded.clone();
0N/A }
0N/A
0N/A}