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