* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Pkcs / CmsSignerTest.cs
1 //
2 // CmsSignerTest.cs - NUnit tests for CmsSigner
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 #if NET_2_0
31
32 using NUnit.Framework;
33
34 using System;
35 using System.Collections;
36 using System.Security.Cryptography;
37 using System.Security.Cryptography.Pkcs;
38 using System.Security.Cryptography.X509Certificates;
39
40 namespace MonoTests.System.Security.Cryptography.Pkcs {
41
42         [TestFixture]
43         public class CmsSignerTest : Assertion {
44
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";
50
51                 [Test]
52                 public void ConstructorEmpty () 
53                 {
54                         CmsSigner ps = new CmsSigner ();
55                         // default properties
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);
63                 }
64
65                 [Test]
66                 public void ConstructorIssuerAndSerialNumber () 
67                 {
68                         CmsSigner ps = new CmsSigner (SubjectIdentifierType.IssuerAndSerialNumber);
69                         // default properties
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);
77                 }
78
79                 [Test]
80                 public void ConstructorSubjectKeyIdentifier () 
81                 {
82                         CmsSigner ps = new CmsSigner (SubjectIdentifierType.SubjectKeyIdentifier);
83                         // default properties
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);
91                 }
92
93                 [Test]
94                 public void ConstructorUnknown ()
95                 {
96                         CmsSigner ps = new CmsSigner (SubjectIdentifierType.Unknown);
97                         // default properties
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);
106                 }
107
108                 // TODO: return valid x509 certifiate with private key
109                 private X509CertificateEx GetValidCertificateWithPrivateKey () 
110                 {
111                         X509CertificateEx x509 = new X509CertificateEx ();
112                         return x509;
113                 }
114
115                 [Test]
116                 public void ConstructorX509CertificateEx () 
117                 {
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);
128                 }
129
130                 [Test]
131                 public void ConstructorX509CertificateExEmpty () 
132                 {
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);
143                 }
144
145                 [Test]
146                 //BUG [ExpectedException (typeof (ArgumentNullException))]
147                 public void ConstructorX509CertificateExNull () 
148                 {
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);
159                 }
160
161                 [Test]
162                 public void ConstructorIssuerAndSerialNumberX509CertificateEx () 
163                 {
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);
174                 }
175
176                 [Test]
177                 public void ConstructorSubjectKeyIdentifierX509CertificateEx () 
178                 {
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);
189                 }
190
191                 [Test]
192                 public void ConstructorUnknownX509CertificateEx () 
193                 {
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);
205                 }
206
207                 [Test]
208                 //BUG [ExpectedException (typeof (ArgumentNullException))]
209                 public void ConstructorIssuerAndSerialNumberX509CertificateExNull () 
210                 {
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);
220                 }
221
222                 [Test]
223                 public void SignedAttributes ()
224                 {
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);
229                 }
230
231                 [Test]
232                 public void Certificate () 
233                 {
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);
240                 }
241
242                 [Test]
243                 public void Digest () 
244                 {
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);
251                 }
252
253                 [Test]
254                 public void IncludeOption () 
255                 {
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);
265                 }
266
267                 [Test]
268                 public void SubjectIdentifierTypeProperty () 
269                 {
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);
275                 }
276
277                 [Test]
278                 [ExpectedException (typeof (ArgumentException))]
279                 public void SubjectIdentifierTypeUnknown () 
280                 {
281                         CmsSigner ps = new CmsSigner ();
282                         ps.SignerIdentifierType = SubjectIdentifierType.Unknown;
283                 }
284
285                 [Test]
286                 public void UnauthenticatedAttributes () 
287                 {
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);
292                 }
293         }
294 }
295
296 #endif