1 /* GnuRSAPrivateKey.java -- GNU RSA private key.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.java
.security
.provider
;
41 import gnu
.java
.security
.OID
;
42 import gnu
.java
.security
.der
.DER
;
43 import gnu
.java
.security
.der
.DERValue
;
45 import java
.math
.BigInteger
;
46 import java
.security
.interfaces
.RSAPrivateCrtKey
;
47 import java
.security
.spec
.RSAPrivateCrtKeySpec
;
48 import java
.util
.ArrayList
;
50 class GnuRSAPrivateKey
implements RSAPrivateCrtKey
54 // -------------------------------------------------------------------------
56 private final RSAPrivateCrtKeySpec spec
;
57 private byte[] encodedKey
;
60 // -------------------------------------------------------------------------
62 public GnuRSAPrivateKey(RSAPrivateCrtKeySpec spec
)
68 // -------------------------------------------------------------------------
70 public BigInteger
getModulus()
72 return spec
.getModulus();
75 public BigInteger
getPrivateExponent()
77 return spec
.getPrivateExponent();
80 public BigInteger
getCrtCoefficient()
82 return spec
.getCrtCoefficient();
85 public BigInteger
getPrimeExponentP()
87 return spec
.getPrimeExponentP();
90 public BigInteger
getPrimeExponentQ()
92 return spec
.getPrimeExponentQ();
95 public BigInteger
getPrimeP()
97 return spec
.getPrimeP();
100 public BigInteger
getPrimeQ()
102 return spec
.getPrimeQ();
105 public BigInteger
getPublicExponent()
107 return spec
.getPublicExponent();
110 public String
getAlgorithm()
115 public String
getFormat()
121 * The encoded form is:
124 * RSAPrivateKey ::= SEQUENCE {
126 * modulus INTEGER, -- n
127 * publicExponent INTEGER, -- e
128 * privateExponent INTEGER, -- d
129 * prime1 INTEGER, -- p
130 * prime2 INTEGER, -- q
131 * exponent1 INTEGER, -- d mod (p-1)
132 * exponent2 INTEGER, -- d mod (q-1)
133 * coefficient INTEGER -- (inverse of q) mod p }
136 * <p>Which is in turn encoded in a PrivateKeyInfo structure from PKCS#8.
138 public byte[] getEncoded()
140 if (encodedKey
!= null)
141 return (byte[]) encodedKey
.clone();
142 ArrayList key
= new ArrayList(9);
143 key
.add(new DERValue(DER
.INTEGER
, BigInteger
.ZERO
));
144 key
.add(new DERValue(DER
.INTEGER
, getModulus()));
145 key
.add(new DERValue(DER
.INTEGER
, getPublicExponent()));
146 key
.add(new DERValue(DER
.INTEGER
, getPrivateExponent()));
147 key
.add(new DERValue(DER
.INTEGER
, getPrimeP()));
148 key
.add(new DERValue(DER
.INTEGER
, getPrimeQ()));
149 key
.add(new DERValue(DER
.INTEGER
, getPrimeExponentP()));
150 key
.add(new DERValue(DER
.INTEGER
, getPrimeExponentQ()));
151 key
.add(new DERValue(DER
.INTEGER
, getCrtCoefficient()));
152 DERValue pk
= new DERValue(DER
.SEQUENCE
|DER
.CONSTRUCTED
, key
);
153 ArrayList pki
= new ArrayList(3);
154 pki
.add(new DERValue(DER
.INTEGER
, BigInteger
.ZERO
));
155 ArrayList alg
= new ArrayList(2);
156 alg
.add(new DERValue(DER
.OBJECT_IDENTIFIER
,
157 new OID("1.2.840.113549.1.1.1")));
158 alg
.add(new DERValue(DER
.NULL
, null));
159 pki
.add(new DERValue(DER
.CONSTRUCTED
|DER
.SEQUENCE
, alg
));
160 pki
.add(new DERValue(DER
.OCTET_STRING
, pk
.getEncoded()));
161 encodedKey
= new DERValue(DER
.SEQUENCE
|DER
.CONSTRUCTED
, pki
).getEncoded();
162 return (byte[]) encodedKey
.clone();