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.asn1.x509;
20  
21  import java.math.BigInteger;
22  import java.util.Enumeration;
23  
24  import org.apache.geronimo.util.asn1.ASN1Encodable;
25  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
26  import org.apache.geronimo.util.asn1.ASN1Sequence;
27  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
28  import org.apache.geronimo.util.asn1.DERInteger;
29  import org.apache.geronimo.util.asn1.DERObject;
30  import org.apache.geronimo.util.asn1.DERSequence;
31  
32  public class RSAPublicKeyStructure
33      extends ASN1Encodable
34  {
35      private BigInteger  modulus;
36      private BigInteger  publicExponent;
37  
38      public static RSAPublicKeyStructure getInstance(
39          ASN1TaggedObject obj,
40          boolean          explicit)
41      {
42          return getInstance(ASN1Sequence.getInstance(obj, explicit));
43      }
44  
45      public static RSAPublicKeyStructure getInstance(
46          Object obj)
47      {
48          if(obj == null || obj instanceof RSAPublicKeyStructure)
49          {
50              return (RSAPublicKeyStructure)obj;
51          }
52  
53          if(obj instanceof ASN1Sequence)
54          {
55              return new RSAPublicKeyStructure((ASN1Sequence)obj);
56          }
57  
58          throw new IllegalArgumentException("Invalid RSAPublicKeyStructure: " + obj.getClass().getName());
59      }
60  
61      public RSAPublicKeyStructure(
62          BigInteger  modulus,
63          BigInteger  publicExponent)
64      {
65          this.modulus = modulus;
66          this.publicExponent = publicExponent;
67      }
68  
69      public RSAPublicKeyStructure(
70          ASN1Sequence  seq)
71      {
72          Enumeration e = seq.getObjects();
73  
74          modulus = ((DERInteger)e.nextElement()).getPositiveValue();
75          publicExponent = ((DERInteger)e.nextElement()).getPositiveValue();
76      }
77  
78      public BigInteger getModulus()
79      {
80          return modulus;
81      }
82  
83      public BigInteger getPublicExponent()
84      {
85          return publicExponent;
86      }
87  
88      /**
89       * This outputs the key in PKCS1v2 format.
90       * <pre>
91       *      RSAPublicKey ::= SEQUENCE {
92       *                          modulus INTEGER, -- n
93       *                          publicExponent INTEGER, -- e
94       *                      }
95       * </pre>
96       * <p>
97       */
98      public DERObject toASN1Object()
99      {
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 }