1 /*
2 * Copyright (c) 2003, 2013, 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
174 // see JCA doc
175 public BigInteger getCrtCoefficient() {
176 return coeff;
177 }
178
179 /**
180 * Parse the key. Called by PKCS8Key.
181 */
182 protected void parseKeyBits() throws InvalidKeyException {
183 try {
184 DerInputStream in = new DerInputStream(key);
185 DerValue derValue = in.getDerValue();
186 if (derValue.tag != DerValue.tag_Sequence) {
187 throw new IOException("Not a SEQUENCE");
188 }
189 DerInputStream data = derValue.data;
190 int version = data.getInteger();
191 if (version != 0) {
192 throw new IOException("Version must be 0");
193 }
194 n = getBigInteger(data);
195 e = getBigInteger(data);
196 d = getBigInteger(data);
197 p = getBigInteger(data);
198 q = getBigInteger(data);
199 pe = getBigInteger(data);
200 qe = getBigInteger(data);
201 coeff = getBigInteger(data);
202 if (derValue.data.available() != 0) {
203 throw new IOException("Extra data available");
204 }
205 } catch (IOException e) {
206 throw new InvalidKeyException("Invalid RSA private key", e);
207 }
208 }
209
210 /**
211 * Read a BigInteger from the DerInputStream.
212 */
213 static BigInteger getBigInteger(DerInputStream data) throws IOException {
214 BigInteger b = data.getBigInteger();
215
216 /*
217 * Some implementations do not correctly encode ASN.1 INTEGER values
218 * in 2's complement format, resulting in a negative integer when
219 * decoded. Correct the error by converting it to a positive integer.
220 *
221 * See CR 6255949
222 */
223 if (b.signum() < 0) {
224 b = new BigInteger(1, b.toByteArray());
225 }
226 return b;
227 }
228 }
|
1 /*
2 * Copyright (c) 2003, 2017, 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
174 // see JCA doc
175 public BigInteger getCrtCoefficient() {
176 return coeff;
177 }
178
179 /**
180 * Parse the key. Called by PKCS8Key.
181 */
182 protected void parseKeyBits() throws InvalidKeyException {
183 try {
184 DerInputStream in = new DerInputStream(key);
185 DerValue derValue = in.getDerValue();
186 if (derValue.tag != DerValue.tag_Sequence) {
187 throw new IOException("Not a SEQUENCE");
188 }
189 DerInputStream data = derValue.data;
190 int version = data.getInteger();
191 if (version != 0) {
192 throw new IOException("Version must be 0");
193 }
194
195 /*
196 * Some implementations do not correctly encode ASN.1 INTEGER values
197 * in 2's complement format, resulting in a negative integer when
198 * decoded. Correct the error by converting it to a positive integer.
199 *
200 * See CR 6255949
201 */
202 n = data.getPositiveBigInteger();
203 e = data.getPositiveBigInteger();
204 d = data.getPositiveBigInteger();
205 p = data.getPositiveBigInteger();
206 q = data.getPositiveBigInteger();
207 pe = data.getPositiveBigInteger();
208 qe = data.getPositiveBigInteger();
209 coeff = data.getPositiveBigInteger();
210 if (derValue.data.available() != 0) {
211 throw new IOException("Extra data available");
212 }
213 } catch (IOException e) {
214 throw new InvalidKeyException("Invalid RSA private key", e);
215 }
216 }
217 }
|