01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.crypto.asn1.x509;
17:
18: import java.math.BigInteger;
19: import java.util.Enumeration;
20:
21: import org.apache.geronimo.crypto.asn1.ASN1Encodable;
22: import org.apache.geronimo.crypto.asn1.ASN1EncodableVector;
23: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
24: import org.apache.geronimo.crypto.asn1.ASN1TaggedObject;
25: import org.apache.geronimo.crypto.asn1.DERInteger;
26: import org.apache.geronimo.crypto.asn1.DERObject;
27: import org.apache.geronimo.crypto.asn1.DERSequence;
28:
29: public class RSAPublicKeyStructure extends ASN1Encodable {
30: private BigInteger modulus;
31: private BigInteger publicExponent;
32:
33: public static RSAPublicKeyStructure getInstance(
34: ASN1TaggedObject obj, boolean explicit) {
35: return getInstance(ASN1Sequence.getInstance(obj, explicit));
36: }
37:
38: public static RSAPublicKeyStructure getInstance(Object obj) {
39: if (obj == null || obj instanceof RSAPublicKeyStructure) {
40: return (RSAPublicKeyStructure) obj;
41: }
42:
43: if (obj instanceof ASN1Sequence) {
44: return new RSAPublicKeyStructure((ASN1Sequence) obj);
45: }
46:
47: throw new IllegalArgumentException(
48: "Invalid RSAPublicKeyStructure: "
49: + obj.getClass().getName());
50: }
51:
52: public RSAPublicKeyStructure(BigInteger modulus,
53: BigInteger publicExponent) {
54: this .modulus = modulus;
55: this .publicExponent = publicExponent;
56: }
57:
58: public RSAPublicKeyStructure(ASN1Sequence seq) {
59: Enumeration e = seq.getObjects();
60:
61: modulus = ((DERInteger) e.nextElement()).getPositiveValue();
62: publicExponent = ((DERInteger) e.nextElement())
63: .getPositiveValue();
64: }
65:
66: public BigInteger getModulus() {
67: return modulus;
68: }
69:
70: public BigInteger getPublicExponent() {
71: return publicExponent;
72: }
73:
74: /**
75: * This outputs the key in PKCS1v2 format.
76: * <pre>
77: * RSAPublicKey ::= SEQUENCE {
78: * modulus INTEGER, -- n
79: * publicExponent INTEGER, -- e
80: * }
81: * </pre>
82: * <p>
83: */
84: public DERObject toASN1Object() {
85: ASN1EncodableVector v = new ASN1EncodableVector();
86:
87: v.add(new DERInteger(getModulus()));
88: v.add(new DERInteger(getPublicExponent()));
89:
90: return new DERSequence(v);
91: }
92: }
|