2 // CmsSignerTest.cs - NUnit tests for CmsSigner
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit
.Framework
;
35 using System
.Collections
;
36 using System
.Security
.Cryptography
;
37 using System
.Security
.Cryptography
.Pkcs
;
38 using System
.Security
.Cryptography
.X509Certificates
;
40 namespace MonoTests
.System
.Security
.Cryptography
.Pkcs
{
43 public class CmsSignerTest
: Assertion
{
45 static byte[] asnNull
= { 0x05, 0x00 }
;
46 static string sha1Oid
= "1.3.14.3.2.26";
47 static string sha1Name
= "sha1";
48 static string rsaOid
= "1.2.840.113549.1.1.1";
49 static string rsaName
= "RSA";
52 public void ConstructorEmpty ()
54 CmsSigner ps
= new CmsSigner ();
56 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
57 AssertNull ("Certificate", ps
.Certificate
);
58 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
59 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
60 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
61 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
62 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
66 public void ConstructorIssuerAndSerialNumber ()
68 CmsSigner ps
= new CmsSigner (SubjectIdentifierType
.IssuerAndSerialNumber
);
70 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
71 AssertNull ("Certificate", ps
.Certificate
);
72 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
73 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
74 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
75 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
76 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
80 public void ConstructorSubjectKeyIdentifier ()
82 CmsSigner ps
= new CmsSigner (SubjectIdentifierType
.SubjectKeyIdentifier
);
84 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
85 AssertNull ("Certificate", ps
.Certificate
);
86 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
87 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
88 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
89 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.SubjectKeyIdentifier
, ps
.SignerIdentifierType
);
90 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
94 public void ConstructorUnknown ()
96 CmsSigner ps
= new CmsSigner (SubjectIdentifierType
.Unknown
);
98 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
99 AssertNull ("Certificate", ps
.Certificate
);
100 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
101 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
102 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
103 // Unknown is converted to IssuerAndSerialNumber
104 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
105 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
108 // TODO: return valid x509 certifiate with private key
109 private X509CertificateEx
GetValidCertificateWithPrivateKey ()
111 X509CertificateEx x509
= new X509CertificateEx ();
116 public void ConstructorX509CertificateEx ()
118 X509CertificateEx x509
= GetValidCertificateWithPrivateKey ();
119 CmsSigner ps
= new CmsSigner (x509
);
120 // default properties
121 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
122 AssertNotNull ("Certificate", ps
.Certificate
);
123 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
124 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
125 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
126 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
127 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
131 public void ConstructorX509CertificateExEmpty ()
133 X509CertificateEx x509
= new X509CertificateEx (); // empty
134 CmsSigner ps
= new CmsSigner (x509
);
135 // default properties
136 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
137 AssertNotNull ("Certificate", ps
.Certificate
);
138 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
139 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
140 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
141 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
142 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
146 //BUG [ExpectedException (typeof (ArgumentNullException))]
147 public void ConstructorX509CertificateExNull ()
149 X509CertificateEx x509
= null;
150 CmsSigner ps
= new CmsSigner (x509
);
151 // default properties
152 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
153 AssertNull ("Certificate", ps
.Certificate
);
154 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
155 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
156 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
157 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
158 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
162 public void ConstructorIssuerAndSerialNumberX509CertificateEx ()
164 X509CertificateEx x509
= GetValidCertificateWithPrivateKey ();
165 CmsSigner ps
= new CmsSigner (SubjectIdentifierType
.IssuerAndSerialNumber
, x509
);
166 // default properties
167 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
168 AssertNotNull ("Certificate", ps
.Certificate
);
169 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
170 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
171 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
172 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
173 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
177 public void ConstructorSubjectKeyIdentifierX509CertificateEx ()
179 X509CertificateEx x509
= GetValidCertificateWithPrivateKey ();
180 CmsSigner ps
= new CmsSigner (SubjectIdentifierType
.SubjectKeyIdentifier
, x509
);
181 // default properties
182 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
183 AssertNotNull ("Certificate", ps
.Certificate
);
184 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
185 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
186 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
187 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.SubjectKeyIdentifier
, ps
.SignerIdentifierType
);
188 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
192 public void ConstructorUnknownX509CertificateEx ()
194 X509CertificateEx x509
= GetValidCertificateWithPrivateKey ();
195 CmsSigner ps
= new CmsSigner (SubjectIdentifierType
.Unknown
, x509
);
196 // default properties
197 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
198 AssertNotNull ("Certificate", ps
.Certificate
);
199 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
200 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
201 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
202 // Unknown is converted to IssuerAndSerialNumber
203 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
204 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
208 //BUG [ExpectedException (typeof (ArgumentNullException))]
209 public void ConstructorIssuerAndSerialNumberX509CertificateExNull ()
211 CmsSigner ps
= new CmsSigner (SubjectIdentifierType
.IssuerAndSerialNumber
, null);
212 // default properties
213 AssertEquals ("SignedAttributes", 0, ps
.SignedAttributes
.Count
);
214 AssertNull ("Certificate", ps
.Certificate
);
215 AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name
, ps
.DigestAlgorithm
.FriendlyName
);
216 AssertEquals ("DigestAlgorithm.Value", sha1Oid
, ps
.DigestAlgorithm
.Value
);
217 AssertEquals ("IncludeOption", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
218 AssertEquals ("SignerIdentifierType", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
219 AssertEquals ("UnsignedAttributes", 0, ps
.UnsignedAttributes
.Count
);
223 public void SignedAttributes ()
225 CmsSigner ps
= new CmsSigner ();
226 AssertEquals ("SignedAttributes=0", 0, ps
.SignedAttributes
.Count
);
227 ps
.SignedAttributes
.Add (new Pkcs9DocumentDescription ("mono"));
228 AssertEquals ("SignedAttributes=1", 1, ps
.SignedAttributes
.Count
);
232 public void Certificate ()
234 CmsSigner ps
= new CmsSigner ();
235 AssertNull ("Certificate=default(null)", ps
.Certificate
);
236 ps
.Certificate
= GetValidCertificateWithPrivateKey ();
237 AssertNotNull ("Certificate!=null", ps
.Certificate
);
238 ps
.Certificate
= null;
239 AssertNull ("Certificate=null", ps
.Certificate
);
243 public void Digest ()
245 CmsSigner ps
= new CmsSigner ();
246 ps
.DigestAlgorithm
= new Oid ("1.2.840.113549.2.5");
247 AssertEquals ("DigestAlgorithm.FriendlyName", "md5", ps
.DigestAlgorithm
.FriendlyName
);
248 AssertEquals ("DigestAlgorithm.Value", "1.2.840.113549.2.5", ps
.DigestAlgorithm
.Value
);
249 ps
.DigestAlgorithm
= null;
250 AssertNull ("DigestAlgorithm=null", ps
.DigestAlgorithm
);
254 public void IncludeOption ()
256 CmsSigner ps
= new CmsSigner ();
257 ps
.IncludeOption
= X509IncludeOption
.EndCertOnly
;
258 AssertEquals ("EndCertOnly", X509IncludeOption
.EndCertOnly
, ps
.IncludeOption
);
259 ps
.IncludeOption
= X509IncludeOption
.ExcludeRoot
;
260 AssertEquals ("ExcludeRoot", X509IncludeOption
.ExcludeRoot
, ps
.IncludeOption
);
261 ps
.IncludeOption
= X509IncludeOption
.None
;
262 AssertEquals ("None", X509IncludeOption
.None
, ps
.IncludeOption
);
263 ps
.IncludeOption
= X509IncludeOption
.WholeChain
;
264 AssertEquals ("WholeChain", X509IncludeOption
.WholeChain
, ps
.IncludeOption
);
268 public void SubjectIdentifierTypeProperty ()
270 CmsSigner ps
= new CmsSigner ();
271 ps
.SignerIdentifierType
= SubjectIdentifierType
.IssuerAndSerialNumber
;
272 AssertEquals ("IssuerAndSerialNumber", SubjectIdentifierType
.IssuerAndSerialNumber
, ps
.SignerIdentifierType
);
273 ps
.SignerIdentifierType
= SubjectIdentifierType
.SubjectKeyIdentifier
;
274 AssertEquals ("SubjectKeyIdentifier", SubjectIdentifierType
.SubjectKeyIdentifier
, ps
.SignerIdentifierType
);
278 [ExpectedException (typeof (ArgumentException
))]
279 public void SubjectIdentifierTypeUnknown ()
281 CmsSigner ps
= new CmsSigner ();
282 ps
.SignerIdentifierType
= SubjectIdentifierType
.Unknown
;
286 public void UnauthenticatedAttributes ()
288 CmsSigner ps
= new CmsSigner ();
289 AssertEquals ("UnsignedAttributes=0", 0, ps
.UnsignedAttributes
.Count
);
290 ps
.UnsignedAttributes
.Add (new Pkcs9DocumentDescription ("mono"));
291 AssertEquals ("UnsignedAttributes=1", 1, ps
.UnsignedAttributes
.Count
);