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
|
using System;
using System.Collections.Generic;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Cmp
{
public class ProtectedPkiMessageBuilder
{
private PkiHeaderBuilder hdrBuilBuilder;
private PkiBody body;
private List<InfoTypeAndValue> generalInfos = new List<InfoTypeAndValue>();
private List<X509Certificate> extraCerts = new List<X509Certificate>();
public ProtectedPkiMessageBuilder(GeneralName sender, GeneralName recipient)
: this(PkiHeader.CMP_2000, sender, recipient)
{
}
public ProtectedPkiMessageBuilder(int pvno, GeneralName sender, GeneralName recipient)
{
hdrBuilBuilder = new PkiHeaderBuilder(pvno, sender, recipient);
}
public ProtectedPkiMessageBuilder SetTransactionId(byte[] tid)
{
hdrBuilBuilder.SetTransactionID(tid);
return this;
}
public ProtectedPkiMessageBuilder SetFreeText(PkiFreeText freeText)
{
hdrBuilBuilder.SetFreeText(freeText);
return this;
}
public ProtectedPkiMessageBuilder AddGeneralInfo(InfoTypeAndValue genInfo)
{
generalInfos.Add(genInfo);
return this;
}
public ProtectedPkiMessageBuilder SetMessageTime(DerGeneralizedTime generalizedTime)
{
hdrBuilBuilder.SetMessageTime(generalizedTime);
return this;
}
public ProtectedPkiMessageBuilder SetRecipKID(byte[] id)
{
hdrBuilBuilder.SetRecipKID(id);
return this;
}
public ProtectedPkiMessageBuilder SetRecipNonce(byte[] nonce)
{
hdrBuilBuilder.SetRecipNonce(nonce);
return this;
}
public ProtectedPkiMessageBuilder SetSenderKID(byte[] id)
{
hdrBuilBuilder.SetSenderKID(id);
return this;
}
public ProtectedPkiMessageBuilder SetSenderNonce(byte[] nonce)
{
hdrBuilBuilder.SetSenderNonce(nonce);
return this;
}
public ProtectedPkiMessageBuilder SetBody(PkiBody body)
{
this.body = body;
return this;
}
public ProtectedPkiMessageBuilder AddCmpCertificate(X509Certificate certificate)
{
extraCerts.Add(certificate);
return this;
}
public ProtectedPkiMessage Build(ISignatureFactory signatureFactory)
{
if (null == body)
throw new InvalidOperationException("body must be set before building");
IStreamCalculator calculator = signatureFactory.CreateCalculator();
if (!(signatureFactory.AlgorithmDetails is AlgorithmIdentifier))
{
throw new ArgumentException("AlgorithmDetails is not AlgorithmIdentifier");
}
FinalizeHeader((AlgorithmIdentifier)signatureFactory.AlgorithmDetails);
PkiHeader header = hdrBuilBuilder.Build();
DerBitString protection = new DerBitString(CalculateSignature(calculator, header, body));
return FinalizeMessage(header, protection);
}
public ProtectedPkiMessage Build(IMacFactory factory)
{
if (null == body)
throw new InvalidOperationException("body must be set before building");
IStreamCalculator calculator = factory.CreateCalculator();
FinalizeHeader((AlgorithmIdentifier)factory.AlgorithmDetails);
PkiHeader header = hdrBuilBuilder.Build();
DerBitString protection = new DerBitString(CalculateSignature(calculator, header, body));
return FinalizeMessage(header, protection);
}
private void FinalizeHeader(AlgorithmIdentifier algorithmIdentifier)
{
hdrBuilBuilder.SetProtectionAlg(algorithmIdentifier);
if (generalInfos.Count > 0)
{
hdrBuilBuilder.SetGeneralInfo(generalInfos.ToArray());
}
}
private ProtectedPkiMessage FinalizeMessage(PkiHeader header, DerBitString protection)
{
if (extraCerts.Count > 0)
{
CmpCertificate[] cmpCertificates = new CmpCertificate[extraCerts.Count];
for (int i = 0; i < cmpCertificates.Length; i++)
{
byte[] cert = extraCerts[i].GetEncoded();
cmpCertificates[i] = CmpCertificate.GetInstance(Asn1Object.FromByteArray(cert));
}
return new ProtectedPkiMessage(new PkiMessage(header, body, protection, cmpCertificates));
}
return new ProtectedPkiMessage(new PkiMessage(header, body, protection));
}
private byte[] CalculateSignature(IStreamCalculator signer, PkiHeader header, PkiBody body)
{
Asn1EncodableVector avec = new Asn1EncodableVector(2);
avec.Add(header);
avec.Add(body);
new DerSequence(avec).EncodeTo(signer.Stream);
object result = signer.GetResult();
if (result is DefaultSignatureResult sigResult)
{
return sigResult.Collect();
}
else if (result is IBlockResult blockResult)
{
return blockResult.Collect();
}
else if (result is byte[] bytesResult)
{
return bytesResult;
}
throw new InvalidOperationException("result is not byte[] or DefaultSignatureResult");
}
}
}
|