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 DSAParameter
033        extends ASN1Encodable
034    {
035        DERInteger      p, q, g;
036    
037        public static DSAParameter getInstance(
038            ASN1TaggedObject obj,
039            boolean          explicit)
040        {
041            return getInstance(ASN1Sequence.getInstance(obj, explicit));
042        }
043    
044        public static DSAParameter getInstance(
045            Object obj)
046        {
047            if(obj == null || obj instanceof DSAParameter)
048            {
049                return (DSAParameter)obj;
050            }
051    
052            if(obj instanceof ASN1Sequence)
053            {
054                return new DSAParameter((ASN1Sequence)obj);
055            }
056    
057            throw new IllegalArgumentException("Invalid DSAParameter: " + obj.getClass().getName());
058        }
059    
060        public DSAParameter(
061            BigInteger  p,
062            BigInteger  q,
063            BigInteger  g)
064        {
065            this.p = new DERInteger(p);
066            this.q = new DERInteger(q);
067            this.g = new DERInteger(g);
068        }
069    
070        public DSAParameter(
071            ASN1Sequence  seq)
072        {
073            Enumeration     e = seq.getObjects();
074    
075            p = (DERInteger)e.nextElement();
076            q = (DERInteger)e.nextElement();
077            g = (DERInteger)e.nextElement();
078        }
079    
080        public BigInteger getP()
081        {
082            return p.getPositiveValue();
083        }
084    
085        public BigInteger getQ()
086        {
087            return q.getPositiveValue();
088        }
089    
090        public BigInteger getG()
091        {
092            return g.getPositiveValue();
093        }
094    
095        public DERObject toASN1Object()
096        {
097            ASN1EncodableVector  v = new ASN1EncodableVector();
098    
099            v.add(p);
100            v.add(q);
101            v.add(g);
102    
103            return new DERSequence(v);
104        }
105    }