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
|
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.X509;
namespace Org.BouncyCastle.Cmp
{
public sealed class ProtectedPkiMessageBuilder
{
private readonly PkiHeaderBuilder m_hdrBuilder;
private PkiBody body;
private readonly List<InfoTypeAndValue> generalInfos = new List<InfoTypeAndValue>();
private readonly 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)
{
m_hdrBuilder = new PkiHeaderBuilder(pvno, sender, recipient);
}
public ProtectedPkiMessageBuilder SetTransactionId(byte[] tid)
{
m_hdrBuilder.SetTransactionID(tid);
return this;
}
public ProtectedPkiMessageBuilder SetFreeText(PkiFreeText freeText)
{
m_hdrBuilder.SetFreeText(freeText);
return this;
}
public ProtectedPkiMessageBuilder AddGeneralInfo(InfoTypeAndValue genInfo)
{
generalInfos.Add(genInfo);
return this;
}
public ProtectedPkiMessageBuilder SetMessageTime(DateTime time)
{
m_hdrBuilder.SetMessageTime(new Asn1GeneralizedTime(time));
return this;
}
public ProtectedPkiMessageBuilder SetMessageTime(Asn1GeneralizedTime generalizedTime)
{
m_hdrBuilder.SetMessageTime(generalizedTime);
return this;
}
public ProtectedPkiMessageBuilder SetRecipKID(byte[] id)
{
m_hdrBuilder.SetRecipKID(id);
return this;
}
public ProtectedPkiMessageBuilder SetRecipNonce(byte[] nonce)
{
m_hdrBuilder.SetRecipNonce(nonce);
return this;
}
public ProtectedPkiMessageBuilder SetSenderKID(byte[] id)
{
m_hdrBuilder.SetSenderKID(id);
return this;
}
public ProtectedPkiMessageBuilder SetSenderNonce(byte[] nonce)
{
m_hdrBuilder.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");
if (!(signatureFactory.AlgorithmDetails is AlgorithmIdentifier algorithmDetails))
throw new ArgumentException("AlgorithmDetails is not AlgorithmIdentifier");
FinalizeHeader(algorithmDetails);
PkiHeader header = m_hdrBuilder.Build();
DerBitString protection = X509Utilities.GenerateSignature(signatureFactory, new DerSequence(header, body));
return FinalizeMessage(header, protection);
}
public ProtectedPkiMessage Build(IMacFactory macFactory)
{
if (null == body)
throw new InvalidOperationException("body must be set before building");
if (!(macFactory.AlgorithmDetails is AlgorithmIdentifier algorithmDetails))
throw new ArgumentException("AlgorithmDetails is not AlgorithmIdentifier");
FinalizeHeader(algorithmDetails);
PkiHeader header = m_hdrBuilder.Build();
DerBitString protection = X509Utilities.GenerateMac(macFactory, new DerSequence(header, body));
return FinalizeMessage(header, protection);
}
private void FinalizeHeader(AlgorithmIdentifier algorithmIdentifier)
{
m_hdrBuilder.SetProtectionAlg(algorithmIdentifier);
if (generalInfos.Count > 0)
{
m_hdrBuilder.SetGeneralInfo(generalInfos.ToArray());
}
}
private ProtectedPkiMessage FinalizeMessage(PkiHeader header, DerBitString protection)
{
if (extraCerts.Count < 1)
return new ProtectedPkiMessage(new PkiMessage(header, body, protection));
CmpCertificate[] cmpCertificates = new CmpCertificate[extraCerts.Count];
for (int i = 0; i < cmpCertificates.Length; i++)
{
cmpCertificates[i] = new CmpCertificate(extraCerts[i].CertificateStructure);
}
return new ProtectedPkiMessage(new PkiMessage(header, body, protection, cmpCertificates));
}
}
}
|