001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.jce.provider;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.math.BigInteger;
021: import java.security.interfaces.RSAPublicKey;
022: import java.security.spec.RSAPublicKeySpec;
023:
024: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
025: import org.apache.geronimo.crypto.asn1.DERNull;
026: import org.apache.geronimo.crypto.asn1.DEROutputStream;
027: import org.apache.geronimo.crypto.asn1.pkcs.PKCSObjectIdentifiers;
028: import org.apache.geronimo.crypto.asn1.x509.AlgorithmIdentifier;
029: import org.apache.geronimo.crypto.asn1.x509.RSAPublicKeyStructure;
030: import org.apache.geronimo.crypto.asn1.x509.SubjectPublicKeyInfo;
031: import org.apache.geronimo.crypto.crypto.params.RSAKeyParameters;
032:
033: public class JCERSAPublicKey implements RSAPublicKey {
034: private BigInteger modulus;
035: private BigInteger publicExponent;
036:
037: JCERSAPublicKey(RSAKeyParameters key) {
038: this .modulus = key.getModulus();
039: this .publicExponent = key.getExponent();
040: }
041:
042: JCERSAPublicKey(RSAPublicKeySpec spec) {
043: this .modulus = spec.getModulus();
044: this .publicExponent = spec.getPublicExponent();
045: }
046:
047: JCERSAPublicKey(RSAPublicKey key) {
048: this .modulus = key.getModulus();
049: this .publicExponent = key.getPublicExponent();
050: }
051:
052: JCERSAPublicKey(SubjectPublicKeyInfo info) {
053: try {
054: RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure(
055: (ASN1Sequence) info.getPublicKey());
056:
057: this .modulus = pubKey.getModulus();
058: this .publicExponent = pubKey.getPublicExponent();
059: } catch (IOException e) {
060: throw new IllegalArgumentException(
061: "invalid info structure in RSA public key", e);
062: }
063: }
064:
065: /**
066: * return the modulus.
067: *
068: * @return the modulus.
069: */
070: public BigInteger getModulus() {
071: return modulus;
072: }
073:
074: /**
075: * return the public exponent.
076: *
077: * @return the public exponent.
078: */
079: public BigInteger getPublicExponent() {
080: return publicExponent;
081: }
082:
083: public String getAlgorithm() {
084: return "RSA";
085: }
086:
087: public String getFormat() {
088: return "X.509";
089: }
090:
091: public byte[] getEncoded() {
092: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
093: DEROutputStream dOut = new DEROutputStream(bOut);
094: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
095: new AlgorithmIdentifier(
096: PKCSObjectIdentifiers.rsaEncryption,
097: new DERNull()), new RSAPublicKeyStructure(
098: getModulus(), getPublicExponent())
099: .getDERObject());
100:
101: try {
102: dOut.writeObject(info);
103: dOut.close();
104: } catch (IOException e) {
105: throw new RuntimeException("Error encoding RSA public key",
106: e);
107: }
108:
109: return bOut.toByteArray();
110:
111: }
112:
113: public boolean equals(Object o) {
114: if (!(o instanceof RSAPublicKey)) {
115: return false;
116: }
117:
118: if (o == this ) {
119: return true;
120: }
121:
122: RSAPublicKey key = (RSAPublicKey) o;
123:
124: return getModulus().equals(key.getModulus())
125: && getPublicExponent().equals(key.getPublicExponent());
126: }
127:
128: public String toString() {
129: StringBuffer buf = new StringBuffer();
130: String nl = System.getProperty("line.separator");
131:
132: buf.append("RSA Public Key" + nl);
133: buf.append(" modulus: "
134: + this .getModulus().toString(16) + nl);
135: buf.append(" public exponent: "
136: + this .getPublicExponent().toString(16) + nl);
137:
138: return buf.toString();
139: }
140: }
|