Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/pkcs12/MacData.java
41159 views
1
/*
2
* Copyright (c) 1999, 2020, 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
if (macData.length < 2 || macData.length > 3) {
63
throw new ParsingException("Invalid length for MacData");
64
}
65
66
// Parse the digest info
67
DerInputStream digestIn = new DerInputStream(macData[0].toByteArray());
68
DerValue[] digestInfo = digestIn.getSequence(2);
69
if (digestInfo.length != 2) {
70
throw new ParsingException("Invalid length for DigestInfo");
71
}
72
73
// Parse the DigestAlgorithmIdentifier.
74
AlgorithmId digestAlgorithmId = AlgorithmId.parse(digestInfo[0]);
75
this.digestAlgorithmName = digestAlgorithmId.getName();
76
this.digestAlgorithmParams = digestAlgorithmId.getParameters();
77
// Get the digest.
78
this.digest = digestInfo[1].getOctetString();
79
80
// Get the salt.
81
this.macSalt = macData[1].getOctetString();
82
83
// Iterations is optional. The default value is 1.
84
if (macData.length > 2) {
85
this.iterations = macData[2].getInteger();
86
} else {
87
this.iterations = 1;
88
}
89
}
90
91
MacData(String algName, byte[] digest, byte[] salt, int iterations)
92
throws NoSuchAlgorithmException
93
{
94
if (algName == null)
95
throw new NullPointerException("the algName parameter " +
96
"must be non-null");
97
98
AlgorithmId algid = AlgorithmId.get(algName);
99
this.digestAlgorithmName = algid.getName();
100
this.digestAlgorithmParams = algid.getParameters();
101
102
if (digest == null) {
103
throw new NullPointerException("the digest " +
104
"parameter must be non-null");
105
} else if (digest.length == 0) {
106
throw new IllegalArgumentException("the digest " +
107
"parameter must not be empty");
108
} else {
109
this.digest = digest.clone();
110
}
111
112
this.macSalt = salt;
113
this.iterations = iterations;
114
115
// delay the generation of ASN.1 encoding until
116
// getEncoded() is called
117
this.encoded = null;
118
119
}
120
121
MacData(AlgorithmParameters algParams, byte[] digest,
122
byte[] salt, int iterations) throws NoSuchAlgorithmException
123
{
124
if (algParams == null)
125
throw new NullPointerException("the algParams parameter " +
126
"must be non-null");
127
128
AlgorithmId algid = AlgorithmId.get(algParams);
129
this.digestAlgorithmName = algid.getName();
130
this.digestAlgorithmParams = algid.getParameters();
131
132
if (digest == null) {
133
throw new NullPointerException("the digest " +
134
"parameter must be non-null");
135
} else if (digest.length == 0) {
136
throw new IllegalArgumentException("the digest " +
137
"parameter must not be empty");
138
} else {
139
this.digest = digest.clone();
140
}
141
142
this.macSalt = salt;
143
this.iterations = iterations;
144
145
// delay the generation of ASN.1 encoding until
146
// getEncoded() is called
147
this.encoded = null;
148
149
}
150
151
String getDigestAlgName() {
152
return digestAlgorithmName;
153
}
154
155
byte[] getSalt() {
156
return macSalt;
157
}
158
159
int getIterations() {
160
return iterations;
161
}
162
163
byte[] getDigest() {
164
return digest;
165
}
166
167
/**
168
* Returns the ASN.1 encoding of this object.
169
* @return the ASN.1 encoding.
170
* @exception IOException if error occurs when constructing its
171
* ASN.1 encoding.
172
*/
173
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
174
{
175
if (this.encoded != null)
176
return this.encoded.clone();
177
178
DerOutputStream out = new DerOutputStream();
179
DerOutputStream tmp = new DerOutputStream();
180
181
DerOutputStream tmp2 = new DerOutputStream();
182
// encode encryption algorithm
183
AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
184
algid.encode(tmp2);
185
186
// encode digest data
187
tmp2.putOctetString(digest);
188
189
tmp.write(DerValue.tag_Sequence, tmp2);
190
191
// encode salt
192
tmp.putOctetString(macSalt);
193
194
// encode iterations
195
tmp.putInteger(iterations);
196
197
// wrap everything into a SEQUENCE
198
out.write(DerValue.tag_Sequence, tmp);
199
this.encoded = out.toByteArray();
200
201
return this.encoded.clone();
202
}
203
204
}
205
206