PKIHeaderBuilder.cs
6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using Org.BouncyCastle.Asn1.X509;
namespace Org.BouncyCastle.Asn1.Cmp
{
public class PkiHeaderBuilder
{
private DerInteger pvno;
private GeneralName sender;
private GeneralName recipient;
private DerGeneralizedTime messageTime;
private AlgorithmIdentifier protectionAlg;
private Asn1OctetString senderKID; // KeyIdentifier
private Asn1OctetString recipKID; // KeyIdentifier
private Asn1OctetString transactionID;
private Asn1OctetString senderNonce;
private Asn1OctetString recipNonce;
private PkiFreeText freeText;
private Asn1Sequence generalInfo;
public PkiHeaderBuilder(
int pvno,
GeneralName sender,
GeneralName recipient)
: this(new DerInteger(pvno), sender, recipient)
{
}
private PkiHeaderBuilder(
DerInteger pvno,
GeneralName sender,
GeneralName recipient)
{
this.pvno = pvno;
this.sender = sender;
this.recipient = recipient;
}
public virtual PkiHeaderBuilder SetMessageTime(DerGeneralizedTime time)
{
messageTime = time;
return this;
}
public virtual PkiHeaderBuilder SetProtectionAlg(AlgorithmIdentifier aid)
{
protectionAlg = aid;
return this;
}
public virtual PkiHeaderBuilder SetSenderKID(byte[] kid)
{
return SetSenderKID(kid == null ? null : new DerOctetString(kid));
}
public virtual PkiHeaderBuilder SetSenderKID(Asn1OctetString kid)
{
senderKID = kid;
return this;
}
public virtual PkiHeaderBuilder SetRecipKID(byte[] kid)
{
return SetRecipKID(kid == null ? null : new DerOctetString(kid));
}
public virtual PkiHeaderBuilder SetRecipKID(DerOctetString kid)
{
recipKID = kid;
return this;
}
public virtual PkiHeaderBuilder SetTransactionID(byte[] tid)
{
return SetTransactionID(tid == null ? null : new DerOctetString(tid));
}
public virtual PkiHeaderBuilder SetTransactionID(Asn1OctetString tid)
{
transactionID = tid;
return this;
}
public virtual PkiHeaderBuilder SetSenderNonce(byte[] nonce)
{
return SetSenderNonce(nonce == null ? null : new DerOctetString(nonce));
}
public virtual PkiHeaderBuilder SetSenderNonce(Asn1OctetString nonce)
{
senderNonce = nonce;
return this;
}
public virtual PkiHeaderBuilder SetRecipNonce(byte[] nonce)
{
return SetRecipNonce(nonce == null ? null : new DerOctetString(nonce));
}
public virtual PkiHeaderBuilder SetRecipNonce(Asn1OctetString nonce)
{
recipNonce = nonce;
return this;
}
public virtual PkiHeaderBuilder SetFreeText(PkiFreeText text)
{
freeText = text;
return this;
}
public virtual PkiHeaderBuilder SetGeneralInfo(InfoTypeAndValue genInfo)
{
return SetGeneralInfo(MakeGeneralInfoSeq(genInfo));
}
public virtual PkiHeaderBuilder SetGeneralInfo(InfoTypeAndValue[] genInfos)
{
return SetGeneralInfo(MakeGeneralInfoSeq(genInfos));
}
public virtual PkiHeaderBuilder SetGeneralInfo(Asn1Sequence seqOfInfoTypeAndValue)
{
generalInfo = seqOfInfoTypeAndValue;
return this;
}
private static Asn1Sequence MakeGeneralInfoSeq(
InfoTypeAndValue generalInfo)
{
return new DerSequence(generalInfo);
}
private static Asn1Sequence MakeGeneralInfoSeq(
InfoTypeAndValue[] generalInfos)
{
Asn1Sequence genInfoSeq = null;
if (generalInfos != null)
{
Asn1EncodableVector v = new Asn1EncodableVector();
for (int i = 0; i < generalInfos.Length; ++i)
{
v.Add(generalInfos[i]);
}
genInfoSeq = new DerSequence(v);
}
return genInfoSeq;
}
/**
* <pre>
* PKIHeader ::= SEQUENCE {
* pvno INTEGER { cmp1999(1), cmp2000(2) },
* sender GeneralName,
* -- identifies the sender
* recipient GeneralName,
* -- identifies the intended recipient
* messageTime [0] GeneralizedTime OPTIONAL,
* -- time of production of this message (used when sender
* -- believes that the transport will be "suitable"; i.e.,
* -- that the time will still be meaningful upon receipt)
* protectionAlg [1] AlgorithmIdentifier OPTIONAL,
* -- algorithm used for calculation of protection bits
* senderKID [2] KeyIdentifier OPTIONAL,
* recipKID [3] KeyIdentifier OPTIONAL,
* -- to identify specific keys used for protection
* transactionID [4] OCTET STRING OPTIONAL,
* -- identifies the transaction; i.e., this will be the same in
* -- corresponding request, response, certConf, and PKIConf
* -- messages
* senderNonce [5] OCTET STRING OPTIONAL,
* recipNonce [6] OCTET STRING OPTIONAL,
* -- nonces used to provide replay protection, senderNonce
* -- is inserted by the creator of this message; recipNonce
* -- is a nonce previously inserted in a related message by
* -- the intended recipient of this message
* freeText [7] PKIFreeText OPTIONAL,
* -- this may be used to indicate context-specific instructions
* -- (this field is intended for human consumption)
* generalInfo [8] SEQUENCE SIZE (1..MAX) OF
* InfoTypeAndValue OPTIONAL
* -- this may be used to convey context-specific information
* -- (this field not primarily intended for human consumption)
* }
* </pre>
* @return a basic ASN.1 object representation.
*/
public virtual PkiHeader Build()
{
Asn1EncodableVector v = new Asn1EncodableVector(pvno, sender, recipient);
AddOptional(v, 0, messageTime);
AddOptional(v, 1, protectionAlg);
AddOptional(v, 2, senderKID);
AddOptional(v, 3, recipKID);
AddOptional(v, 4, transactionID);
AddOptional(v, 5, senderNonce);
AddOptional(v, 6, recipNonce);
AddOptional(v, 7, freeText);
AddOptional(v, 8, generalInfo);
messageTime = null;
protectionAlg = null;
senderKID = null;
recipKID = null;
transactionID = null;
senderNonce = null;
recipNonce = null;
freeText = null;
generalInfo = null;
return PkiHeader.GetInstance(new DerSequence(v));
}
private void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
{
if (obj != null)
{
v.Add(new DerTaggedObject(true, tagNo, obj));
}
}
}
}