1 /* GnuRSAPrivateKey.java -- GNU RSA private key. 2 Copyright (C) 2004 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 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) 9 any later version. 10 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. 15 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 19 02110-1301 USA. 20 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 24 combination. 25 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. */ 37 38 39 package gnu.java.security.provider; 40 41 import gnu.java.security.OID; 42 import gnu.java.security.der.DER; 43 import gnu.java.security.der.DERValue; 44 45 import java.math.BigInteger; 46 import java.security.interfaces.RSAPrivateCrtKey; 47 import java.security.spec.RSAPrivateCrtKeySpec; 48 import java.util.ArrayList; 49 50 class GnuRSAPrivateKey implements RSAPrivateCrtKey 51 { 52 53 // Fields. 54 // ------------------------------------------------------------------------- 55 56 private final RSAPrivateCrtKeySpec spec; 57 private byte[] encodedKey; 58 59 // Constructor. 60 // ------------------------------------------------------------------------- 61 GnuRSAPrivateKey(RSAPrivateCrtKeySpec spec)62 public GnuRSAPrivateKey(RSAPrivateCrtKeySpec spec) 63 { 64 this.spec = spec; 65 } 66 67 // Instance methods. 68 // ------------------------------------------------------------------------- 69 getModulus()70 public BigInteger getModulus() 71 { 72 return spec.getModulus(); 73 } 74 getPrivateExponent()75 public BigInteger getPrivateExponent() 76 { 77 return spec.getPrivateExponent(); 78 } 79 getCrtCoefficient()80 public BigInteger getCrtCoefficient() 81 { 82 return spec.getCrtCoefficient(); 83 } 84 getPrimeExponentP()85 public BigInteger getPrimeExponentP() 86 { 87 return spec.getPrimeExponentP(); 88 } 89 getPrimeExponentQ()90 public BigInteger getPrimeExponentQ() 91 { 92 return spec.getPrimeExponentQ(); 93 } 94 getPrimeP()95 public BigInteger getPrimeP() 96 { 97 return spec.getPrimeP(); 98 } 99 getPrimeQ()100 public BigInteger getPrimeQ() 101 { 102 return spec.getPrimeQ(); 103 } 104 getPublicExponent()105 public BigInteger getPublicExponent() 106 { 107 return spec.getPublicExponent(); 108 } 109 getAlgorithm()110 public String getAlgorithm() 111 { 112 return "RSA"; 113 } 114 getFormat()115 public String getFormat() 116 { 117 return "PKCS#8"; 118 } 119 120 /** 121 * The encoded form is: 122 * 123 * <pre> 124 * RSAPrivateKey ::= SEQUENCE { 125 * version Version, 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 } 134 * </pre> 135 * 136 * <p>Which is in turn encoded in a PrivateKeyInfo structure from PKCS#8. 137 */ getEncoded()138 public byte[] getEncoded() 139 { 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(); 163 } 164 } 165