Showing the changes to java/pkg/sun/security/pkcs12/MacData.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,198 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.security.pkcs12; | ||
| 27 | |||
| 28 | import java.io.*; | ||
| 29 | import java.security.*; | ||
| 30 | |||
| 31 | import sun.security.util.DerInputStream; | ||
| 32 | import sun.security.util.DerOutputStream; | ||
| 33 | import sun.security.util.DerValue; | ||
| 34 | import sun.security.x509.AlgorithmId; | ||
| 35 | import sun.security.pkcs.ParsingException; | ||
| 36 | |||
| 37 | |||
| 38 | /** | ||
| 39 | * A MacData type, as defined in PKCS#12. | ||
| 40 | * | ||
| 41 | * @author Sharon Liu | ||
| 42 | */ | ||
| 43 | |||
| 44 | class MacData { | ||
| 45 | |||
| 46 | private String digestAlgorithmName; | ||
| 47 | private AlgorithmParameters digestAlgorithmParams; | ||
| 48 | private byte[] digest; | ||
| 49 | private byte[] macSalt; | ||
| 50 | private int iterations; | ||
| 51 | |||
| 52 | // the ASN.1 encoded contents of this class | ||
| 53 | private byte[] encoded = null; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Parses a PKCS#12 MAC data. | ||
| 57 | */ | ||
| 58 | MacData(DerInputStream derin) | ||
| 59 | throws IOException, ParsingException | ||
| 60 | { | ||
| 61 | DerValue[] macData = derin.getSequence(2); | ||
| 62 | |||
| 63 | // Parse the digest info | ||
| 64 | DerInputStream digestIn = new DerInputStream(macData[0].toByteArray()); | ||
| 65 | DerValue[] digestInfo = digestIn.getSequence(2); | ||
| 66 | |||
| 67 | // Parse the DigestAlgorithmIdentifier. | ||
| 68 | AlgorithmId digestAlgorithmId = AlgorithmId.parse(digestInfo[0]); | ||
| 69 | this.digestAlgorithmName = digestAlgorithmId.getName(); | ||
| 70 | this.digestAlgorithmParams = digestAlgorithmId.getParameters(); | ||
| 71 | // Get the digest. | ||
| 72 | this.digest = digestInfo[1].getOctetString(); | ||
| 73 | |||
| 74 | // Get the salt. | ||
| 75 | this.macSalt = macData[1].getOctetString(); | ||
| 76 | |||
| 77 | // Iterations is optional. The default value is 1. | ||
| 78 | if (macData.length > 2) { | ||
| 79 | this.iterations = macData[2].getInteger(); | ||
| 80 | } else { | ||
| 81 | this.iterations = 1; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | MacData(String algName, byte[] digest, byte[] salt, int iterations) | ||
| 86 | throws NoSuchAlgorithmException | ||
| 87 | { | ||
| 88 | if (algName == null) | ||
| 89 | throw new NullPointerException("the algName parameter " + | ||
| 90 | "must be non-null"); | ||
| 91 | |||
| 92 | AlgorithmId algid = AlgorithmId.get(algName); | ||
| 93 | this.digestAlgorithmName = algid.getName(); | ||
| 94 | this.digestAlgorithmParams = algid.getParameters(); | ||
| 95 | |||
| 96 | if (digest == null) { | ||
| 97 | throw new NullPointerException("the digest " + | ||
| 98 | "parameter must be non-null"); | ||
| 99 | } else if (digest.length == 0) { | ||
| 100 | throw new IllegalArgumentException("the digest " + | ||
| 101 | "parameter must not be empty"); | ||
| 102 | } else { | ||
| 103 | this.digest = digest.clone(); | ||
| 104 | } | ||
| 105 | |||
| 106 | this.macSalt = salt; | ||
| 107 | this.iterations = iterations; | ||
| 108 | |||
| 109 | // delay the generation of ASN.1 encoding until | ||
| 110 | // getEncoded() is called | ||
| 111 | this.encoded = null; | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | MacData(AlgorithmParameters algParams, byte[] digest, | ||
| 116 | byte[] salt, int iterations) throws NoSuchAlgorithmException | ||
| 117 | { | ||
| 118 | if (algParams == null) | ||
| 119 | throw new NullPointerException("the algParams parameter " + | ||
| 120 | "must be non-null"); | ||
| 121 | |||
| 122 | AlgorithmId algid = AlgorithmId.get(algParams); | ||
| 123 | this.digestAlgorithmName = algid.getName(); | ||
| 124 | this.digestAlgorithmParams = algid.getParameters(); | ||
| 125 | |||
| 126 | if (digest == null) { | ||
| 127 | throw new NullPointerException("the digest " + | ||
| 128 | "parameter must be non-null"); | ||
| 129 | } else if (digest.length == 0) { | ||
| 130 | throw new IllegalArgumentException("the digest " + | ||
| 131 | "parameter must not be empty"); | ||
| 132 | } else { | ||
| 133 | this.digest = digest.clone(); | ||
| 134 | } | ||
| 135 | |||
| 136 | this.macSalt = salt; | ||
| 137 | this.iterations = iterations; | ||
| 138 | |||
| 139 | // delay the generation of ASN.1 encoding until | ||
| 140 | // getEncoded() is called | ||
| 141 | this.encoded = null; | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | String getDigestAlgName() { | ||
| 146 | return digestAlgorithmName; | ||
| 147 | } | ||
| 148 | |||
| 149 | byte[] getSalt() { | ||
| 150 | return macSalt; | ||
| 151 | } | ||
| 152 | |||
| 153 | int getIterations() { | ||
| 154 | return iterations; | ||
| 155 | } | ||
| 156 | |||
| 157 | byte[] getDigest() { | ||
| 158 | return digest; | ||
| 159 | } | ||
| 160 | |||
| 161 | /** | ||
| 162 | * Returns the ASN.1 encoding of this object. | ||
| 163 | * @return the ASN.1 encoding. | ||
| 164 | * @exception IOException if error occurs when constructing its | ||
| 165 | * ASN.1 encoding. | ||
| 166 | */ | ||
| 167 | public byte[] getEncoded() throws NoSuchAlgorithmException, IOException | ||
| 168 | { | ||
| 169 | if (this.encoded != null) | ||
| 170 | return this.encoded.clone(); | ||
| 171 | |||
| 172 | DerOutputStream out = new DerOutputStream(); | ||
| 173 | DerOutputStream tmp = new DerOutputStream(); | ||
| 174 | |||
| 175 | DerOutputStream tmp2 = new DerOutputStream(); | ||
| 176 | // encode encryption algorithm | ||
| 177 | AlgorithmId algid = AlgorithmId.get(digestAlgorithmName); | ||
| 178 | algid.encode(tmp2); | ||
| 179 | |||
| 180 | // encode digest data | ||
| 181 | tmp2.putOctetString(digest); | ||
| 182 | |||
| 183 | tmp.write(DerValue.tag_Sequence, tmp2); | ||
| 184 | |||
| 185 | // encode salt | ||
| 186 | tmp.putOctetString(macSalt); | ||
| 187 | |||
| 188 | // encode iterations | ||
| 189 | tmp.putInteger(iterations); | ||
| 190 | |||
| 191 | // wrap everything into a SEQUENCE | ||
| 192 | out.write(DerValue.tag_Sequence, tmp); | ||
| 193 | this.encoded = out.toByteArray(); | ||
| 194 | |||
| 195 | return this.encoded.clone(); | ||
| 196 | } | ||
| 197 | |||
| 198 | } | ||