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.asn1.x509;
020
021 import java.math.BigInteger;
022 import java.util.Enumeration;
023
024 import org.apache.geronimo.util.asn1.ASN1Encodable;
025 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
026 import org.apache.geronimo.util.asn1.ASN1Sequence;
027 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
028 import org.apache.geronimo.util.asn1.DERInteger;
029 import org.apache.geronimo.util.asn1.DERObject;
030 import org.apache.geronimo.util.asn1.DERSequence;
031
032 public class RSAPublicKeyStructure
033 extends ASN1Encodable
034 {
035 private BigInteger modulus;
036 private BigInteger publicExponent;
037
038 public static RSAPublicKeyStructure getInstance(
039 ASN1TaggedObject obj,
040 boolean explicit)
041 {
042 return getInstance(ASN1Sequence.getInstance(obj, explicit));
043 }
044
045 public static RSAPublicKeyStructure getInstance(
046 Object obj)
047 {
048 if(obj == null || obj instanceof RSAPublicKeyStructure)
049 {
050 return (RSAPublicKeyStructure)obj;
051 }
052
053 if(obj instanceof ASN1Sequence)
054 {
055 return new RSAPublicKeyStructure((ASN1Sequence)obj);
056 }
057
058 throw new IllegalArgumentException("Invalid RSAPublicKeyStructure: " + obj.getClass().getName());
059 }
060
061 public RSAPublicKeyStructure(
062 BigInteger modulus,
063 BigInteger publicExponent)
064 {
065 this.modulus = modulus;
066 this.publicExponent = publicExponent;
067 }
068
069 public RSAPublicKeyStructure(
070 ASN1Sequence seq)
071 {
072 Enumeration e = seq.getObjects();
073
074 modulus = ((DERInteger)e.nextElement()).getPositiveValue();
075 publicExponent = ((DERInteger)e.nextElement()).getPositiveValue();
076 }
077
078 public BigInteger getModulus()
079 {
080 return modulus;
081 }
082
083 public BigInteger getPublicExponent()
084 {
085 return publicExponent;
086 }
087
088 /**
089 * This outputs the key in PKCS1v2 format.
090 * <pre>
091 * RSAPublicKey ::= SEQUENCE {
092 * modulus INTEGER, -- n
093 * publicExponent INTEGER, -- e
094 * }
095 * </pre>
096 * <p>
097 */
098 public DERObject toASN1Object()
099 {
100 ASN1EncodableVector v = new ASN1EncodableVector();
101
102 v.add(new DERInteger(getModulus()));
103 v.add(new DERInteger(getPublicExponent()));
104
105 return new DERSequence(v);
106 }
107 }