001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.geronimo.util.jce.provider;
020
021 import java.io.ByteArrayOutputStream;
022 import java.io.IOException;
023 import java.math.BigInteger;
024 import java.security.interfaces.RSAPublicKey;
025 import java.security.spec.RSAPublicKeySpec;
026
027 import org.apache.geronimo.util.asn1.ASN1Sequence;
028 import org.apache.geronimo.util.asn1.DERNull;
029 import org.apache.geronimo.util.asn1.DEROutputStream;
030 import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
031 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
032 import org.apache.geronimo.util.asn1.x509.RSAPublicKeyStructure;
033 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
034 import org.apache.geronimo.util.crypto.params.RSAKeyParameters;
035
036 public class JCERSAPublicKey
037 implements RSAPublicKey
038 {
039 private BigInteger modulus;
040 private BigInteger publicExponent;
041
042 JCERSAPublicKey(
043 RSAKeyParameters key)
044 {
045 this.modulus = key.getModulus();
046 this.publicExponent = key.getExponent();
047 }
048
049 JCERSAPublicKey(
050 RSAPublicKeySpec spec)
051 {
052 this.modulus = spec.getModulus();
053 this.publicExponent = spec.getPublicExponent();
054 }
055
056 JCERSAPublicKey(
057 RSAPublicKey key)
058 {
059 this.modulus = key.getModulus();
060 this.publicExponent = key.getPublicExponent();
061 }
062
063 JCERSAPublicKey(
064 SubjectPublicKeyInfo info)
065 {
066 try
067 {
068 RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.getPublicKey());
069
070 this.modulus = pubKey.getModulus();
071 this.publicExponent = pubKey.getPublicExponent();
072 }
073 catch (IOException e)
074 {
075 throw new IllegalArgumentException("invalid info structure in RSA public key");
076 }
077 }
078
079 /**
080 * return the modulus.
081 *
082 * @return the modulus.
083 */
084 public BigInteger getModulus()
085 {
086 return modulus;
087 }
088
089 /**
090 * return the public exponent.
091 *
092 * @return the public exponent.
093 */
094 public BigInteger getPublicExponent()
095 {
096 return publicExponent;
097 }
098
099 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 }