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
|
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Cms
{
public class SignerInfoGenerator
{
internal X509Certificate certificate;
internal ISignatureFactory contentSigner;
internal SignerIdentifier sigId;
internal CmsAttributeTableGenerator signedGen;
internal CmsAttributeTableGenerator unsignedGen;
private bool isDirectSignature;
internal SignerInfoGenerator(SignerIdentifier sigId, ISignatureFactory signerFactory)
: this(sigId, signerFactory, false)
{
}
internal SignerInfoGenerator(SignerIdentifier sigId, ISignatureFactory signerFactory, bool isDirectSignature)
{
this.sigId = sigId;
this.contentSigner = signerFactory;
this.isDirectSignature = isDirectSignature;
if (this.isDirectSignature)
{
this.signedGen = null;
this.unsignedGen = null;
}
else
{
this.signedGen = new DefaultSignedAttributeTableGenerator();
this.unsignedGen = null;
}
}
internal SignerInfoGenerator(SignerIdentifier sigId, ISignatureFactory contentSigner,
CmsAttributeTableGenerator signedGen, CmsAttributeTableGenerator unsignedGen)
{
this.sigId = sigId;
this.contentSigner = contentSigner;
this.signedGen = signedGen;
this.unsignedGen = unsignedGen;
this.isDirectSignature = false;
}
internal void SetAssociatedCertificate(X509Certificate certificate)
{
this.certificate = certificate;
}
public SignerInfoGeneratorBuilder NewBuilder()
{
SignerInfoGeneratorBuilder builder = new SignerInfoGeneratorBuilder();
builder.WithSignedAttributeGenerator(signedGen);
builder.WithUnsignedAttributeGenerator(unsignedGen);
builder.SetDirectSignature(isDirectSignature);
return builder;
}
}
public class SignerInfoGeneratorBuilder
{
private bool directSignature;
private CmsAttributeTableGenerator signedGen;
private CmsAttributeTableGenerator unsignedGen;
public SignerInfoGeneratorBuilder()
{
}
/**
* If the passed in flag is true, the signer signature will be based on the data, not
* a collection of signed attributes, and no signed attributes will be included.
*
* @return the builder object
*/
public SignerInfoGeneratorBuilder SetDirectSignature(bool hasNoSignedAttributes)
{
this.directSignature = hasNoSignedAttributes;
return this;
}
/**
* Provide a custom signed attribute generator.
*
* @param signedGen a generator of signed attributes.
* @return the builder object
*/
public SignerInfoGeneratorBuilder WithSignedAttributeGenerator(CmsAttributeTableGenerator signedGen)
{
this.signedGen = signedGen;
return this;
}
/**
* Provide a generator of unsigned attributes.
*
* @param unsignedGen a generator for signed attributes.
* @return the builder object
*/
public SignerInfoGeneratorBuilder WithUnsignedAttributeGenerator(CmsAttributeTableGenerator unsignedGen)
{
this.unsignedGen = unsignedGen;
return this;
}
/**
* Build a generator with the passed in X.509 certificate issuer and serial number as the signerIdentifier.
*
* @param contentSigner operator for generating the final signature in the SignerInfo with.
* @param certificate X.509 certificate related to the contentSigner.
* @return a SignerInfoGenerator
* @throws OperatorCreationException if the generator cannot be built.
*/
public SignerInfoGenerator Build(ISignatureFactory contentSigner, X509Certificate certificate)
{
SignerIdentifier sigId = new SignerIdentifier(new IssuerAndSerialNumber(certificate.CertificateStructure));
SignerInfoGenerator sigInfoGen = CreateGenerator(contentSigner, sigId);
sigInfoGen.SetAssociatedCertificate(certificate);
return sigInfoGen;
}
/**
* Build a generator with the passed in subjectKeyIdentifier as the signerIdentifier. If used you should
* try to follow the calculation described in RFC 5280 section 4.2.1.2.
*
* @param signerFactory operator factory for generating the final signature in the SignerInfo with.
* @param subjectKeyIdentifier key identifier to identify the public key for verifying the signature.
* @return a SignerInfoGenerator
*/
public SignerInfoGenerator Build(ISignatureFactory signerFactory, byte[] subjectKeyIdentifier)
{
SignerIdentifier sigId = new SignerIdentifier(new DerOctetString(subjectKeyIdentifier));
return CreateGenerator(signerFactory, sigId);
}
private SignerInfoGenerator CreateGenerator(ISignatureFactory contentSigner, SignerIdentifier sigId)
{
if (directSignature)
{
return new SignerInfoGenerator(sigId, contentSigner, true);
}
if (signedGen != null || unsignedGen != null)
{
if (signedGen == null)
{
signedGen = new DefaultSignedAttributeTableGenerator();
}
return new SignerInfoGenerator(sigId, contentSigner, signedGen, unsignedGen);
}
return new SignerInfoGenerator(sigId, contentSigner);
}
}
}
|