View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.jce.provider;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.math.BigInteger;
24  import java.security.interfaces.RSAPublicKey;
25  import java.security.spec.RSAPublicKeySpec;
26  
27  import org.apache.geronimo.util.asn1.ASN1Sequence;
28  import org.apache.geronimo.util.asn1.DERNull;
29  import org.apache.geronimo.util.asn1.DEROutputStream;
30  import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
31  import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
32  import org.apache.geronimo.util.asn1.x509.RSAPublicKeyStructure;
33  import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
34  import org.apache.geronimo.util.crypto.params.RSAKeyParameters;
35  
36  public class JCERSAPublicKey
37      implements RSAPublicKey
38  {
39      private BigInteger modulus;
40      private BigInteger publicExponent;
41  
42      JCERSAPublicKey(
43          RSAKeyParameters key)
44      {
45          this.modulus = key.getModulus();
46          this.publicExponent = key.getExponent();
47      }
48  
49      JCERSAPublicKey(
50          RSAPublicKeySpec spec)
51      {
52          this.modulus = spec.getModulus();
53          this.publicExponent = spec.getPublicExponent();
54      }
55  
56      JCERSAPublicKey(
57          RSAPublicKey key)
58      {
59          this.modulus = key.getModulus();
60          this.publicExponent = key.getPublicExponent();
61      }
62  
63      JCERSAPublicKey(
64          SubjectPublicKeyInfo    info)
65      {
66          try
67          {
68              RSAPublicKeyStructure   pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.getPublicKey());
69  
70              this.modulus = pubKey.getModulus();
71              this.publicExponent = pubKey.getPublicExponent();
72          }
73          catch (IOException e)
74          {
75              throw new IllegalArgumentException("invalid info structure in RSA public key");
76          }
77      }
78  
79      /**
80       * return the modulus.
81       *
82       * @return the modulus.
83       */
84      public BigInteger getModulus()
85      {
86          return modulus;
87      }
88  
89      /**
90       * return the public exponent.
91       *
92       * @return the public exponent.
93       */
94      public BigInteger getPublicExponent()
95      {
96          return publicExponent;
97      }
98  
99      public String getAlgorithm()
100     {
101         return "RSA";
102     }
103 
104     public String getFormat()
105     {
106         return "X.509";
107     }
108 
109     public byte[] getEncoded()
110     {
111         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
112         DEROutputStream         dOut = new DEROutputStream(bOut);
113         SubjectPublicKeyInfo    info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPublicKeyStructure(getModulus(), getPublicExponent()).getDERObject());
114 
115         try
116         {
117             dOut.writeObject(info);
118             dOut.close();
119         }
120         catch (IOException e)
121         {
122             throw new RuntimeException("Error encoding RSA public key");
123         }
124 
125         return bOut.toByteArray();
126 
127     }
128 
129     public boolean equals(Object o)
130     {
131         if ( !(o instanceof RSAPublicKey) )
132         {
133             return false;
134         }
135 
136         if ( o == this )
137         {
138             return true;
139         }
140 
141         RSAPublicKey key = (RSAPublicKey)o;
142 
143         return getModulus().equals(key.getModulus())
144             && getPublicExponent().equals(key.getPublicExponent());
145     }
146 
147     public String toString()
148     {
149         StringBuffer    buf = new StringBuffer();
150         String          nl = System.getProperty("line.separator");
151 
152         buf.append("RSA Public Key" + nl);
153         buf.append("            modulus: " + this.getModulus().toString(16) + nl);
154         buf.append("    public exponent: " + this.getPublicExponent().toString(16) + nl);
155 
156         return buf.toString();
157     }
158 }