[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography.X509Certificates / X509CapiTest.cs
1 //
2 // X509CAPI.cs - NUnit Test Cases for X509 CAPI integration (IntPtr constructor)
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
10 //
11 #if !TARGET_JVM
12 using NUnit.Framework;
13
14 using System;
15 using System.Globalization;
16 using System.IO;
17 using System.Runtime.InteropServices;
18 using System.Security.Cryptography.X509Certificates;
19 using System.Text;
20 using System.Threading;
21
22 namespace MonoTests.System.Security.Cryptography.X509Certificates {
23
24         // This test is ONLY a simulation as we can't test CryptoAPI integration
25         // outside Windows. The simulation create a buffer similar to what CAPI
26         // would return as a valid IntPtr to a CERT_CONTEXT structure.
27
28         // There is a commented "main" at the end of the file that can be used
29         // to verify, under Windows, that the CERT_CONTEXT used for this test
30         // is similar to the one provided by CAPI.
31
32         [TestFixture]
33         public class X509CAPI {
34
35                 // copied from X509Certificate for test uses only
36                 [StructLayout (LayoutKind.Sequential)]
37                 internal struct CertificateContext {
38                         public UInt32 dwCertEncodingType;
39                         public IntPtr pbCertEncoded;
40                         public UInt32 cbCertEncoded;
41                         public IntPtr pCertInfo;
42                         public IntPtr hCertStore;
43                 }
44
45                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
46                 {
47                         Assert.AreEqual (array1, array2, msg);
48                 }
49
50                 public string ToString (byte[] data) 
51                 {
52                         StringBuilder sb = new StringBuilder ();
53                         for (int i = 0; i < data.Length; i++)
54                                 sb.Append (data[i].ToString ("X2"));
55                         return sb.ToString ();
56                 }
57
58                 private CultureInfo oldcult;
59
60                 [SetUp]
61                 public void SetUp () 
62                 {
63                         // the current culture determines the result of formatting
64                         oldcult = Thread.CurrentThread.CurrentCulture;
65                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("");
66                 }
67
68                 [TearDown]
69                 public void TearDown () 
70                 {
71                         Thread.CurrentThread.CurrentCulture = oldcult;
72                         File.Delete("temp.cer");
73                         File.Delete("temp.b64");
74                 }
75
76                 public IntPtr GetHandleEx (byte[] certificate) 
77                 {
78                         CertificateContext cc = new CertificateContext ();
79 //                      cc.dwCertEncodingType = 0x10001; // PKCS_7_ASN_ENCODING | X509_ASN_ENCODING
80 //                      cc.dwCertEncodingType = 0x10000; // PKCS_7_ASN_ENCODING 
81                         cc.dwCertEncodingType = 0x1; // X509_ASN_ENCODING
82                         cc.cbCertEncoded = (UInt32) certificate.Length;
83                         cc.pbCertEncoded = Marshal.AllocCoTaskMem (certificate.Length);
84                         cc.pCertInfo = (IntPtr) 0;
85                         cc.hCertStore = (IntPtr) 0;
86                         Marshal.Copy (certificate, 0, cc.pbCertEncoded, certificate.Length);
87
88                         int size = Marshal.SizeOf (cc.GetType ());
89                         IntPtr result = Marshal.AllocCoTaskMem (size);
90                         Marshal.StructureToPtr (cc, result, false);
91                         return result;
92                 }
93
94                 [DllImport(@"crypt32.dll")]
95                 internal static extern IntPtr CertCreateCertificateContext(int dwCertEncodingType, IntPtr pbCertEncoded, int cbCertEncoded);
96
97                 public IntPtr GetHandle (byte[] certificate) 
98                 {
99                         IntPtr data = Marshal.AllocHGlobal (certificate.Length);
100                         Marshal.Copy (certificate, 0, data, certificate.Length);
101                         IntPtr handle = CertCreateCertificateContext (0x10001, data, certificate.Length);
102                         Marshal.FreeHGlobal (data);
103                         return handle;
104                 }
105
106                 [Test]
107 #if NET_2_1
108                 [Ignore ("This constructor always throw a NotSupportedException under NET_2_1 and is useless without CryptoAPI (i.e. outside Windows)")]
109 #endif
110                 public void ConstructorIntPtr ()
111                 {
112                         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,
113                                 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,
114                                 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,
115                                 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,
116                                 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,
117                                 0xD7,0xBA,0x18,0x3A,0x48,0xA6,0xCB,0xC2,0xFD,0x20,0x57,0xBC,0x63,0x1C };
118
119                         // will only work on Windows (MS or Mono)
120                         // this isn't much of a problem because the structure CERT_CONTEXT (IntPtr)
121                         // is specific to CryptoAPI/Windows.
122                         IntPtr handle = IntPtr.Zero;
123
124                         if (Path.DirectorySeparatorChar == '\\') {
125                                 handle = GetHandle (cert);
126                         } else {
127                                 handle = GetHandleEx (cert);
128                         }
129
130                         X509Certificate x509 = new X509Certificate (handle);
131                         byte[] hash = { 0xD6,0x2F,0x48,0xD0,0x13,0xEE,0x7F,0xB5,0x8B,0x79,0x07,0x45,0x12,0x67,0x0D,0x9C,0x5B,0x3A,0x5D,0xA9 };
132                         Assert.AreEqual (hash, x509.GetCertHash (), "GetCertHash");
133                         Assert.AreEqual ("D62F48D013EE7FB58B79074512670D9C5B3A5DA9", x509.GetCertHashString (), "GetCertHashString");
134 #if NET_2_0
135                         DateTime from = DateTime.ParseExact (x509.GetEffectiveDateString (), "MM/dd/yyyy HH:mm:ss", null).ToUniversalTime ();
136                         Assert.AreEqual ("03/12/1996 18:38:47", from.ToString (), "GetEffectiveDateString");
137                         DateTime until = DateTime.ParseExact (x509.GetExpirationDateString (), "MM/dd/yyyy HH:mm:ss", null).ToUniversalTime ();
138                         Assert.AreEqual ("03/12/1997 18:38:46", until.ToString (), "GetExpirationDateString");
139 #else
140                         // fx 1.x has a bug where the returned dates were always in the Seattle time zone
141                         Assert.AreEqual ("03/12/1996 10:38:47", x509.GetEffectiveDateString (), "GetEffectiveDateString");
142                         Assert.AreEqual ("03/12/1997 10:38:46", x509.GetExpirationDateString (), "GetExpirationDateString");
143                         // which was making it easier to test the dates ;-)
144 #endif
145                         Assert.AreEqual ("X509", x509.GetFormat (), "GetFormat");
146                         Assert.AreEqual (-701544240, x509.GetHashCode (), "GetHashCode");
147                         Assert.AreEqual ("C=US, O=\"RSA Data Security, Inc.\", OU=Secure Server Certification Authority", x509.GetIssuerName (), "GetIssuerName");
148                         Assert.AreEqual ("1.2.840.113549.1.1.1", x509.GetKeyAlgorithm (), "GetKeyAlgorithm");
149                         byte[] keyparams = { 0x05,0x00 };
150                         Assert.AreEqual (keyparams, x509.GetKeyAlgorithmParameters (), "GetKeyAlgorithmParameters");
151                         Assert.AreEqual ("0500", x509.GetKeyAlgorithmParametersString (), "GetKeyAlgorithmParametersString");
152                         Assert.AreEqual ("C=US, S=California, O=CommerceNet, OU=Server Certification Authority", x509.GetName (), "GetName");
153                         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 };
154                         Assert.AreEqual (pubkey, x509.GetPublicKey (), "GetPublicKey");
155                         Assert.AreEqual ("305C02552D58E9BFF031CD7906505AD59E0E2CE6C2F7F9D2CE556485B1909A92B336C1BCEAC823B7AB3AA76463775F84228EE5B645DD46AE0ADD00C21FBAD9ADC07562F89582A280B18269FAE1AF7FBC7DE27C76D5BC2A80FB0203010001", x509.GetPublicKeyString (), "GetPublicKeyString");
156                         Assert.AreEqual (cert, x509.GetRawCertData (), "GetRawCertData");
157                         Assert.AreEqual (ToString (cert), x509.GetRawCertDataString (), "GetRawCertDataString");
158                         byte[] serial = { 0xE8,0x06,0x00,0x72,0x02 };
159                         Assert.AreEqual (serial, x509.GetSerialNumber (), "GetSerialNumber");
160 #if NET_2_0
161                         Assert.AreEqual ("02720006E8", x509.GetSerialNumberString (), "GetSerialNumberString");
162 #else
163                         Assert.AreEqual ("E806007202", x509.GetSerialNumberString (), "GetSerialNumberString");
164 #endif
165                 }
166
167                 [Test]
168 #if NET_2_0
169                 [ExpectedException (typeof (ArgumentException))]
170 #endif
171                 public void ConstructorIntPtrZero ()
172                 {
173                         X509Certificate spc = new X509Certificate (IntPtr.Zero);
174                         Assert.AreEqual (0, spc.GetHashCode (), "ConstructorIntPtrZero");
175                 }
176         }
177 }
178 #endif