001package org.apache.commons.ssl.org.bouncycastle.asn1.pkcs; 002 003import java.math.BigInteger; 004import java.util.Enumeration; 005 006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector; 007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer; 008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object; 009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive; 010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence; 011import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject; 012import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence; 013 014public class RSAPublicKey 015 extends ASN1Object 016{ 017 private BigInteger modulus; 018 private BigInteger publicExponent; 019 020 public static RSAPublicKey getInstance( 021 ASN1TaggedObject obj, 022 boolean explicit) 023 { 024 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 025 } 026 027 public static RSAPublicKey getInstance( 028 Object obj) 029 { 030 if (obj instanceof RSAPublicKey) 031 { 032 return (RSAPublicKey)obj; 033 } 034 035 if (obj != null) 036 { 037 return new RSAPublicKey(ASN1Sequence.getInstance(obj)); 038 } 039 040 return null; 041 } 042 043 public RSAPublicKey( 044 BigInteger modulus, 045 BigInteger publicExponent) 046 { 047 this.modulus = modulus; 048 this.publicExponent = publicExponent; 049 } 050 051 private RSAPublicKey( 052 ASN1Sequence seq) 053 { 054 if (seq.size() != 2) 055 { 056 throw new IllegalArgumentException("Bad sequence size: " 057 + seq.size()); 058 } 059 060 Enumeration e = seq.getObjects(); 061 062 modulus = ASN1Integer.getInstance(e.nextElement()).getPositiveValue(); 063 publicExponent = ASN1Integer.getInstance(e.nextElement()).getPositiveValue(); 064 } 065 066 public BigInteger getModulus() 067 { 068 return modulus; 069 } 070 071 public BigInteger getPublicExponent() 072 { 073 return publicExponent; 074 } 075 076 /** 077 * This outputs the key in PKCS1v2 format. 078 * <pre> 079 * RSAPublicKey ::= SEQUENCE { 080 * modulus INTEGER, -- n 081 * publicExponent INTEGER, -- e 082 * } 083 * </pre> 084 * <p> 085 */ 086 public ASN1Primitive toASN1Primitive() 087 { 088 ASN1EncodableVector v = new ASN1EncodableVector(); 089 090 v.add(new ASN1Integer(getModulus())); 091 v.add(new ASN1Integer(getPublicExponent())); 092 093 return new DERSequence(v); 094 } 095}