New test.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography.X509Certificates / X509CertificateCas.cs
1 //
2 // X509CertificateCas.cs - CAS unit tests for 
3 //      System.Security.Cryptography.X509Certificates.X509CertificateCas
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 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 using NUnit.Framework;
31
32 using System;
33 using System.Globalization;
34 using System.IO;
35 using System.Runtime.InteropServices;
36 using System.Security;
37 using System.Security.Cryptography.X509Certificates;
38 using System.Security.Permissions;
39 using System.Threading;
40
41 namespace MonoCasTests.System.Security.Cryptography.X509Certificates {
42
43         [TestFixture]
44         [Category ("CAS")]
45         public class X509CertificateCas {
46
47                 private static readonly byte[] cert = { 0x30,0x82,0x01,0xFF,0x30,0x82,0x01,0x6C,0x02,0x05,0x02,0x72,0x00,0x06,0xE8,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x02,0x05,0x00,0x30,0x5F,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x31,0x20,0x30,0x1E,0x06,0x03,0x55,0x04,0x0A,0x13,0x17,0x52,0x53,0x41,0x20,0x44,0x61,0x74,0x61,0x20,0x53,0x65,0x63,0x75,0x72,0x69,0x74,0x79,0x2C,0x20,0x49,0x6E,0x63,0x2E,0x31,0x2E,0x30,0x2C,0x06,0x03,0x55,0x04,0x0B,0x13,0x25,0x53,0x65,0x63,0x75,0x72,0x65,0x20,0x53,0x65,0x72,0x76,
48                         0x65,0x72,0x20,0x43,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x41,0x75,0x74,0x68,0x6F,0x72,0x69,0x74,0x79,0x30,0x1E,0x17,0x0D,0x39,0x36,0x30,0x33,0x31,0x32,0x31,0x38,0x33,0x38,0x34,0x37,0x5A,0x17,0x0D,0x39,0x37,0x30,0x33,0x31,0x32,0x31,0x38,0x33,0x38,0x34,0x36,0x5A,0x30,0x61,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x08,0x13,0x0A,0x43,0x61,0x6C,0x69,0x66,0x6F,0x72,0x6E,0x69,0x61,0x31,0x14,0x30,0x12,0x06,0x03,
49                         0x55,0x04,0x0A,0x13,0x0B,0x43,0x6F,0x6D,0x6D,0x65,0x72,0x63,0x65,0x4E,0x65,0x74,0x31,0x27,0x30,0x25,0x06,0x03,0x55,0x04,0x0B,0x13,0x1E,0x53,0x65,0x72,0x76,0x65,0x72,0x20,0x43,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x41,0x75,0x74,0x68,0x6F,0x72,0x69,0x74,0x79,0x30,0x70,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x01,0x05,0x00,0x03,0x5F,0x00,0x30,0x5C,0x02,0x55,0x2D,0x58,0xE9,0xBF,0xF0,0x31,0xCD,0x79,0x06,0x50,0x5A,0xD5,0x9E,0x0E,0x2C,0xE6,0xC2,0xF7,0xF9,
50                         0xD2,0xCE,0x55,0x64,0x85,0xB1,0x90,0x9A,0x92,0xB3,0x36,0xC1,0xBC,0xEA,0xC8,0x23,0xB7,0xAB,0x3A,0xA7,0x64,0x63,0x77,0x5F,0x84,0x22,0x8E,0xE5,0xB6,0x45,0xDD,0x46,0xAE,0x0A,0xDD,0x00,0xC2,0x1F,0xBA,0xD9,0xAD,0xC0,0x75,0x62,0xF8,0x95,0x82,0xA2,0x80,0xB1,0x82,0x69,0xFA,0xE1,0xAF,0x7F,0xBC,0x7D,0xE2,0x7C,0x76,0xD5,0xBC,0x2A,0x80,0xFB,0x02,0x03,0x01,0x00,0x01,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x02,0x05,0x00,0x03,0x7E,0x00,0x54,0x20,0x67,0x12,0xBB,0x66,0x14,0xC3,0x26,0x6B,0x7F,
51                         0xDA,0x4A,0x25,0x4D,0x8B,0xE0,0xFD,0x1E,0x53,0x6D,0xAC,0xA2,0xD0,0x89,0xB8,0x2E,0x90,0xA0,0x27,0x43,0xA4,0xEE,0x4A,0x26,0x86,0x40,0xFF,0xB8,0x72,0x8D,0x1E,0xE7,0xB7,0x77,0xDC,0x7D,0xD8,0x3F,0x3A,0x6E,0x55,0x10,0xA6,0x1D,0xB5,0x58,0xF2,0xF9,0x0F,0x2E,0xB4,0x10,0x55,0x48,0xDC,0x13,0x5F,0x0D,0x08,0x26,0x88,0xC9,0xAF,0x66,0xF2,0x2C,0x9C,0x6F,0x3D,0xC3,0x2B,0x69,0x28,0x89,0x40,0x6F,0x8F,0x35,0x3B,0x9E,0xF6,0x8E,0xF1,0x11,0x17,0xFB,0x0C,0x98,0x95,0xA1,0xC2,0xBA,0x89,0x48,0xEB,0xB4,0x06,0x6A,0x22,0x54,
52                         0xD7,0xBA,0x18,0x3A,0x48,0xA6,0xCB,0xC2,0xFD,0x20,0x57,0xBC,0x63,0x1C };
53
54                 private CultureInfo oldcult;
55                 private string temppath;
56                 private string certfile;
57                 private string signedfile;
58
59                 private void WriteFile (string filename, byte[] data)
60                 {
61                         using (FileStream fs = File.OpenWrite (filename)) {
62                                 fs.Write (data, 0, data.Length);
63                                 fs.Close ();
64                         }
65                 }
66
67                 [TestFixtureSetUp]
68                 public void FixtureSetUp ()
69                 {
70                         // the current culture determines the result of formatting
71                         oldcult = Thread.CurrentThread.CurrentCulture;
72                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("");
73
74                         temppath = Path.GetTempPath ();
75                         certfile = Path.Combine (temppath, "temp.cer");
76                         signedfile = Path.Combine (temppath, "temp.exe");
77
78                         WriteFile (certfile, cert);
79                         WriteFile (signedfile, MonoTests.System.Security.Cryptography.X509Certificates.SoftwarePublisherCertificateTest.smallspcexe);
80                 }
81
82                 [SetUp]
83                 public void SetUp ()
84                 {
85                         if (!SecurityManager.SecurityEnabled)
86                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
87                 }
88
89                 [TearDown]
90                 public void TearDown () 
91                 {
92                         Thread.CurrentThread.CurrentCulture = oldcult;
93                 }
94
95                 [TestFixtureTearDown]
96                 public void FixtureTearDown ()
97                 {
98                         Thread.CurrentThread.CurrentCulture = oldcult;
99
100                         if (File.Exists (certfile))
101                                 File.Delete (certfile);
102                         if (File.Exists (signedfile))
103                                 File.Delete (signedfile);
104                 }
105
106                 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
107
108                 [Test]
109                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
110                 public void PartialTrust_DenyUnrestricted ()
111                 {
112                         X509Certificate x509 = new X509Certificate (cert);
113                         X509Certificate clone = new X509Certificate (x509);
114
115                         Assert.IsTrue (x509.Equals (clone), "Equals 1");
116                         Assert.IsTrue (clone.Equals (x509), "Equals 2");
117
118                         byte[] hash = { 0xD6,0x2F,0x48,0xD0,0x13,0xEE,0x7F,0xB5,0x8B,0x79,0x07,0x45,0x12,0x67,0x0D,0x9C,0x5B,0x3A,0x5D,0xA9 };
119                         Assert.AreEqual (hash, x509.GetCertHash (), "GetCertHash");
120                         Assert.AreEqual ("D62F48D013EE7FB58B79074512670D9C5B3A5DA9", x509.GetCertHashString (), "GetCertHashString");
121                         Assert.AreEqual ("03/12/1996 10:38:47", x509.GetEffectiveDateString (), "GetEffectiveDateString");
122                         Assert.AreEqual ("03/12/1997 10:38:46", x509.GetExpirationDateString (), "GetExpirationDateString");
123                         Assert.AreEqual ("X509", x509.GetFormat (), "GetFormat");
124                         Assert.AreEqual (-701544240, x509.GetHashCode (), "GetHashCode");
125                         Assert.AreEqual ("C=US, O=\"RSA Data Security, Inc.\", OU=Secure Server Certification Authority", x509.GetIssuerName (), "GetIssuerName");
126                         Assert.AreEqual ("1.2.840.113549.1.1.1", x509.GetKeyAlgorithm (), "GetKeyAlgorithm");
127                         byte[] keyparams = { 0x05,0x00 };
128                         Assert.AreEqual (keyparams, x509.GetKeyAlgorithmParameters (), "GetKeyAlgorithmParameters");
129                         Assert.AreEqual ("0500", x509.GetKeyAlgorithmParametersString (), "GetKeyAlgorithmParametersString");
130                         Assert.AreEqual ("C=US, S=California, O=CommerceNet, OU=Server Certification Authority", x509.GetName (), "GetName");
131                         byte[] pubkey = { 0x30,0x5C,0x02,0x55,0x2D,0x58,0xE9,0xBF,0xF0,0x31,0xCD,0x79,0x06,0x50,0x5A,0xD5,0x9E,0x0E,0x2C,0xE6,0xC2,0xF7,0xF9,0xD2,0xCE,0x55,0x64,0x85,0xB1,0x90,0x9A,0x92,0xB3,0x36,0xC1,0xBC,0xEA,0xC8,0x23,0xB7,0xAB,0x3A,0xA7,0x64,0x63,0x77,0x5F,0x84,0x22,0x8E,0xE5,0xB6,0x45,0xDD,0x46,0xAE,0x0A,0xDD,0x00,0xC2,0x1F,0xBA,0xD9,0xAD,0xC0,0x75,0x62,0xF8,0x95,0x82,0xA2,0x80,0xB1,0x82,0x69,0xFA,0xE1,0xAF,0x7F,0xBC,0x7D,0xE2,0x7C,0x76,0xD5,0xBC,0x2A,0x80,0xFB,0x02,0x03,0x01,0x00,0x01 };
132                         Assert.AreEqual (pubkey, x509.GetPublicKey (), "GetPublicKey");
133                         Assert.AreEqual ("305C02552D58E9BFF031CD7906505AD59E0E2CE6C2F7F9D2CE556485B1909A92B336C1BCEAC823B7AB3AA76463775F84228EE5B645DD46AE0ADD00C21FBAD9ADC07562F89582A280B18269FAE1AF7FBC7DE27C76D5BC2A80FB0203010001", x509.GetPublicKeyString (), "GetPublicKeyString");
134                         Assert.AreEqual (cert, x509.GetRawCertData (), "GetRawCertData");
135                         Assert.IsNotNull (x509.GetRawCertDataString (), "GetRawCertDataString");
136                         byte[] serial = { 0xE8,0x06,0x00,0x72,0x02 };
137                         Assert.AreEqual (serial, x509.GetSerialNumber (), "GetSerialNumber");
138                         Assert.AreEqual ("E806007202", x509.GetSerialNumberString (), "GetSerialNumberString");
139                         Assert.IsNotNull (x509.ToString (true), "ToString");
140                 }       
141
142                 [Test]
143                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
144                 [ExpectedException (typeof (SecurityException))]
145                 public void PartialTrust_DenyUnrestricted_CreateFromCertFile ()
146                 {
147                         X509Certificate disk = X509Certificate.CreateFromCertFile (certfile);
148                 }
149
150                 [Test]
151                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
152                 [ExpectedException (typeof (SecurityException))]
153                 public void PartialTrust_DenyUnrestricted_CreateFromSignedFile ()
154                 {
155                         X509Certificate spc = X509Certificate.CreateFromSignedFile (signedfile);
156                 }
157
158                 [Test]
159                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
160                 public void PartialTrust_PermitOnlyFileIOPermission ()
161                 {
162                         X509Certificate x509 = new X509Certificate (cert);
163                         X509Certificate disk = X509Certificate.CreateFromCertFile (certfile);
164                         Assert.IsTrue (disk.Equals (x509), "Equals 1");
165                         Assert.IsTrue (x509.Equals (disk), "Equals 2");
166
167                         try {
168                                 X509Certificate spc = X509Certificate.CreateFromSignedFile (signedfile);
169                                 Assert.IsFalse (spc.Equals (x509), "!Equals 1");
170                                 Assert.IsFalse (x509.Equals (spc), "!Equals 2");
171                         }
172                         catch (COMException) {
173                                 // most likely the root certificate isn't available
174                                 // anyway this indicates that the security check is ok
175                         }
176                 }
177
178                 // test demands by denying the required permission
179
180                 [Test]
181                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
182                 [ExpectedException (typeof (SecurityException))]
183                 public void ConstructorIntPtr_DenyUnmanagedCode ()
184                 {
185                         // will fail before calling the constructor
186                         X509Certificate x509 = new X509Certificate (IntPtr.Zero);
187                 }
188
189                 [Test]
190                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
191                 public void ConstructorIntPtr_PermitOnlyUnmanagedCode ()
192                 {
193                         // will fail _when_ calling the constructor
194                         X509Certificate x509 = new X509Certificate (IntPtr.Zero);
195                         Assert.AreEqual (0, x509.GetHashCode (), "ConstructorIntPtrZero");
196                 }
197         }
198 }