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