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.pkcs;
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.DERInteger;
027    import org.apache.geronimo.util.asn1.DERObject;
028    import org.apache.geronimo.util.asn1.DERSequence;
029    
030    public class DHParameter
031        extends ASN1Encodable
032    {
033        DERInteger      p, g, l;
034    
035        public DHParameter(
036            BigInteger  p,
037            BigInteger  g,
038            int         l)
039        {
040            this.p = new DERInteger(p);
041            this.g = new DERInteger(g);
042    
043            if (l != 0)
044            {
045                this.l = new DERInteger(l);
046            }
047            else
048            {
049                this.l = null;
050            }
051        }
052    
053        public DHParameter(
054            ASN1Sequence  seq)
055        {
056            Enumeration     e = seq.getObjects();
057    
058            p = (DERInteger)e.nextElement();
059            g = (DERInteger)e.nextElement();
060    
061            if (e.hasMoreElements())
062            {
063                l = (DERInteger)e.nextElement();
064            }
065            else
066            {
067                l = null;
068            }
069        }
070    
071        public BigInteger getP()
072        {
073            return p.getPositiveValue();
074        }
075    
076        public BigInteger getG()
077        {
078            return g.getPositiveValue();
079        }
080    
081        public BigInteger getL()
082        {
083            if (l == null)
084            {
085                return null;
086            }
087    
088            return l.getPositiveValue();
089        }
090    
091        public DERObject toASN1Object()
092        {
093            ASN1EncodableVector  v = new ASN1EncodableVector();
094    
095            v.add(p);
096            v.add(g);
097    
098            if (this.getL() != null)
099            {
100                v.add(l);
101            }
102    
103            return new DERSequence(v);
104        }
105    }