Path: blob/master/src/java.base/share/classes/sun/security/pkcs12/MacData.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.pkcs12;2627import java.io.*;28import java.security.*;2930import sun.security.util.DerInputStream;31import sun.security.util.DerOutputStream;32import sun.security.util.DerValue;33import sun.security.x509.AlgorithmId;34import sun.security.pkcs.ParsingException;353637/**38* A MacData type, as defined in PKCS#12.39*40* @author Sharon Liu41*/4243class MacData {4445private String digestAlgorithmName;46private AlgorithmParameters digestAlgorithmParams;47private byte[] digest;48private byte[] macSalt;49private int iterations;5051// the ASN.1 encoded contents of this class52private byte[] encoded = null;5354/**55* Parses a PKCS#12 MAC data.56*/57MacData(DerInputStream derin)58throws IOException, ParsingException59{60DerValue[] macData = derin.getSequence(2);61if (macData.length < 2 || macData.length > 3) {62throw new ParsingException("Invalid length for MacData");63}6465// Parse the digest info66DerInputStream digestIn = new DerInputStream(macData[0].toByteArray());67DerValue[] digestInfo = digestIn.getSequence(2);68if (digestInfo.length != 2) {69throw new ParsingException("Invalid length for DigestInfo");70}7172// Parse the DigestAlgorithmIdentifier.73AlgorithmId digestAlgorithmId = AlgorithmId.parse(digestInfo[0]);74this.digestAlgorithmName = digestAlgorithmId.getName();75this.digestAlgorithmParams = digestAlgorithmId.getParameters();76// Get the digest.77this.digest = digestInfo[1].getOctetString();7879// Get the salt.80this.macSalt = macData[1].getOctetString();8182// Iterations is optional. The default value is 1.83if (macData.length > 2) {84this.iterations = macData[2].getInteger();85} else {86this.iterations = 1;87}88}8990MacData(String algName, byte[] digest, byte[] salt, int iterations)91throws NoSuchAlgorithmException92{93if (algName == null)94throw new NullPointerException("the algName parameter " +95"must be non-null");9697AlgorithmId algid = AlgorithmId.get(algName);98this.digestAlgorithmName = algid.getName();99this.digestAlgorithmParams = algid.getParameters();100101if (digest == null) {102throw new NullPointerException("the digest " +103"parameter must be non-null");104} else if (digest.length == 0) {105throw new IllegalArgumentException("the digest " +106"parameter must not be empty");107} else {108this.digest = digest.clone();109}110111this.macSalt = salt;112this.iterations = iterations;113114// delay the generation of ASN.1 encoding until115// getEncoded() is called116this.encoded = null;117118}119120MacData(AlgorithmParameters algParams, byte[] digest,121byte[] salt, int iterations) throws NoSuchAlgorithmException122{123if (algParams == null)124throw new NullPointerException("the algParams parameter " +125"must be non-null");126127AlgorithmId algid = AlgorithmId.get(algParams);128this.digestAlgorithmName = algid.getName();129this.digestAlgorithmParams = algid.getParameters();130131if (digest == null) {132throw new NullPointerException("the digest " +133"parameter must be non-null");134} else if (digest.length == 0) {135throw new IllegalArgumentException("the digest " +136"parameter must not be empty");137} else {138this.digest = digest.clone();139}140141this.macSalt = salt;142this.iterations = iterations;143144// delay the generation of ASN.1 encoding until145// getEncoded() is called146this.encoded = null;147148}149150String getDigestAlgName() {151return digestAlgorithmName;152}153154byte[] getSalt() {155return macSalt;156}157158int getIterations() {159return iterations;160}161162byte[] getDigest() {163return digest;164}165166/**167* Returns the ASN.1 encoding of this object.168* @return the ASN.1 encoding.169* @exception IOException if error occurs when constructing its170* ASN.1 encoding.171*/172public byte[] getEncoded() throws NoSuchAlgorithmException, IOException173{174if (this.encoded != null)175return this.encoded.clone();176177DerOutputStream out = new DerOutputStream();178DerOutputStream tmp = new DerOutputStream();179180DerOutputStream tmp2 = new DerOutputStream();181// encode encryption algorithm182AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);183algid.encode(tmp2);184185// encode digest data186tmp2.putOctetString(digest);187188tmp.write(DerValue.tag_Sequence, tmp2);189190// encode salt191tmp.putOctetString(macSalt);192193// encode iterations194tmp.putInteger(iterations);195196// wrap everything into a SEQUENCE197out.write(DerValue.tag_Sequence, tmp);198this.encoded = out.toByteArray();199200return this.encoded.clone();201}202203}204205206