1 package org.bouncycastle.asn1.crmf;
2 
3 import org.bouncycastle.asn1.ASN1EncodableVector;
4 import org.bouncycastle.asn1.ASN1Object;
5 import org.bouncycastle.asn1.ASN1OctetString;
6 import org.bouncycastle.asn1.ASN1Primitive;
7 import org.bouncycastle.asn1.ASN1Sequence;
8 import org.bouncycastle.asn1.DEROctetString;
9 import org.bouncycastle.asn1.DERSequence;
10 import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;
11 import org.bouncycastle.util.Arrays;
12 
13 /**
14  * From RFC 2875 for Diffie-Hellman POP.
15  * <pre>
16  *     DhSigStatic ::= SEQUENCE {
17  *         IssuerAndSerial IssuerAndSerialNumber OPTIONAL,
18  *         hashValue       MessageDigest
19  *     }
20  * </pre>
21  */
22 public class DhSigStatic
23     extends ASN1Object
24 {
25     private final IssuerAndSerialNumber issuerAndSerial;
26     private final ASN1OctetString hashValue;
27 
DhSigStatic(byte[] hashValue)28     public DhSigStatic(byte[] hashValue)
29     {
30         this(null, hashValue);
31     }
32 
DhSigStatic(IssuerAndSerialNumber issuerAndSerial, byte[] hashValue)33     public DhSigStatic(IssuerAndSerialNumber issuerAndSerial, byte[] hashValue)
34     {
35         this.issuerAndSerial = issuerAndSerial;
36         this.hashValue = new DEROctetString(Arrays.clone(hashValue));
37     }
38 
getInstance(Object o)39     public static DhSigStatic getInstance(Object o)
40     {
41         if (o instanceof DhSigStatic)
42         {
43             return (DhSigStatic)o;
44         }
45         else if (o != null)
46         {
47             return new DhSigStatic(ASN1Sequence.getInstance(o));
48         }
49 
50         return null;
51     }
52 
DhSigStatic(ASN1Sequence seq)53     private DhSigStatic(ASN1Sequence seq)
54     {
55         if (seq.size() == 1)
56         {
57             issuerAndSerial = null;
58             hashValue = ASN1OctetString.getInstance(seq.getObjectAt(0));
59         }
60         else if (seq.size() == 2)
61         {
62             issuerAndSerial = IssuerAndSerialNumber.getInstance(seq.getObjectAt(0));
63             hashValue = ASN1OctetString.getInstance(seq.getObjectAt(1));
64         }
65         else
66         {
67             throw new IllegalArgumentException("sequence wrong length for DhSigStatic");
68         }
69     }
70 
getIssuerAndSerial()71     public IssuerAndSerialNumber getIssuerAndSerial()
72     {
73         return issuerAndSerial;
74     }
75 
getHashValue()76     public byte[] getHashValue()
77     {
78         return Arrays.clone(hashValue.getOctets());
79     }
80 
toASN1Primitive()81     public ASN1Primitive toASN1Primitive()
82     {
83         ASN1EncodableVector v = new ASN1EncodableVector(2);
84 
85         if (issuerAndSerial != null)
86         {
87             v.add(issuerAndSerial);
88         }
89 
90         v.add(hashValue);
91 
92         return new DERSequence(v);
93     }
94 }
95