aboutsummaryrefslogtreecommitdiff
path: root/utils/ipn_sqlclr/Taggant.cs
blob: c98925283dfe79daa03756eb1fff873d8c026773 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using Microsoft.SqlServer.Server;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System.Linq;

namespace ipn_sqlclr
{
	public class TaggantConfig : Dictionary<string, string>
	{
		public X509Certificate ClientCertificate { get; set; }
	}
	public partial class UserDefinedFunctions
	{
		public static X509Certificate LocateCertificate(string subjectName)
		{
			var certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
			certStore.Open(OpenFlags.ReadOnly);
			X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindBySubjectName, subjectName, true);
			certStore.Close();

			if (0 == certCollection.Count)
			{
				throw new ArgumentException(string.Format("No valid client certificate found at LocalMachine.My by SubjectName '{0}'", subjectName), "subjectName");
			}
			if (1 == certCollection.Count)
			{
				return certCollection[0];
			}
			throw new ArgumentException(string.Format("More than one client certificate found at LocalMachine.My by SubjectName '{0}'", subjectName), "subjectName");

		}
		public static TaggantConfig GetTaggantConfig(SqlInt32 taggantConfigId)
		{
			var config = new TaggantConfig();
			using (var conn = new SqlConnection("context connection=true"))
			{
				conn.Open();
				var readConfigCmd = conn.CreateCommand();
				readConfigCmd.Parameters.Add(new SqlParameter("@taggantConfigId", taggantConfigId.Value));
				readConfigCmd.CommandText =
					"SELECT Name, Value FROM dbo.TaggantConfig WHERE ID=@taggantConfigId";
				using (var reader = readConfigCmd.ExecuteReader())
				{
					while(reader.Read())
					{
						config[reader.GetString(0)] = reader[1] as string;
					}
				}
			}
			config.ClientCertificate = LocateCertificate(config["ClientCertificate"]);
			return config;
		}

		[SqlFunction]
		public static SqlString TaggantPrivateKeyGenerateNew()
		{
			var g = new RsaKeyPairGenerator();
			g.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
			var pair = g.GenerateKeyPair();

			using (var sw = new StringWriter())
			{
				new PemWriter(sw).WriteObject(pair);
				sw.Flush();
				return new SqlString(sw.ToString());
			}
		}

		[SqlProcedure]
		public static int TaggantCertRevoke(SqlInt32 taggantConfigId, SqlInt32 customerId)
		{
			var tc = GetTaggantConfig(taggantConfigId);
			var log = new List<LogItem>();
			try
			{
				string mail;
				using (var conn = new SqlConnection("context connection=true"))
				{
					conn.Open();
					var readCustomerCmd = conn.CreateCommand();
					readCustomerCmd.Parameters.Add(new SqlParameter("@CustomerID", customerId.Value));
					readCustomerCmd.CommandText =
						"SELECT EMail FROM dbo.Customer WHERE ID=@CustomerID";
					using (var reader = readCustomerCmd.ExecuteReader())
					{
						if (reader.Read())
						{
							mail = reader[0] as string;
						}
						else
						{
							throw new ArgumentException("Customer not found", "customerId");
						}
						if (string.IsNullOrWhiteSpace(mail))
							throw new InvalidOperationException("Customer EMail is not set");
					}
				}
				log.Add(new LogItem { MsgId = 1033, P = new[] { customerId.ToString(), tc["CertificateProfileOid"] } });

				TaggantWebService.CertRevoke(tc, mail, log);
				return 0;
			}
			catch (Exception ex)
			{
				log.Add(new LogItem { MsgId = 15, P = new[] { ex.Source, ex.Message, ex.StackTrace } });
				throw;
			}
			finally
			{
				FlushLog("TaggantCertRevoke", new SqlInt32(2), customerId, log);
			}
		}

		[SqlProcedure]
		public static int TaggantCertEnsure(SqlInt32 taggantConfigId, SqlInt32 customerId)
		{
			var tc = GetTaggantConfig(taggantConfigId);
			var log = new List<LogItem>();
			var id = "ipn" + customerId.Value;
			string taggantCert = null;
			try
			{
				string mail;
				string privateKey;
				using (var conn = new SqlConnection("context connection=true"))
				{
					conn.Open();
					var readCustomerCmd = conn.CreateCommand();
					readCustomerCmd.Parameters.Add(new SqlParameter("@CustomerID", customerId.Value));
					readCustomerCmd.CommandText =
						"SELECT EMail, PrivateKeyCert, TaggantCert FROM dbo.Customer WHERE ID=@CustomerID";
					using (var reader = readCustomerCmd.ExecuteReader())
					{
						if (reader.Read())
						{
							mail = reader[0] as string;
							privateKey = reader[1] as string;
							taggantCert = reader[2] as string;
						}
						else
						{
							throw new ArgumentException("Customer not found", "customerId");
						}
						if (string.IsNullOrWhiteSpace(mail))
							throw new InvalidOperationException("Customer EMail is not set");
						if (string.IsNullOrWhiteSpace(privateKey))
							throw new InvalidOperationException("Customer PrivateKeyCert is not set");
						if (!string.IsNullOrWhiteSpace(taggantCert))
							return 0; // ensured
					}
				}
				log.Add(new LogItem { MsgId = 14, P = new[] { customerId.ToString(), mail, tc["CertificateProfileOid"] } });

				taggantCert = TaggantWebService.CertRequestNew(tc, id, mail, privateKey, log);
				using (var conn = new SqlConnection("context connection=true"))
				{
					conn.Open();
					var writeCustomerCmd = conn.CreateCommand();
					writeCustomerCmd.Parameters.Add(new SqlParameter("@CustomerID", customerId.Value));
					writeCustomerCmd.Parameters.Add(new SqlParameter("@TaggantCert", taggantCert));
					writeCustomerCmd.CommandText =
						"UPDATE dbo.Customer SET TaggantCert=@TaggantCert WHERE ID=@CustomerID";
					writeCustomerCmd.ExecuteNonQuery();
				}
				return 1; // created new
			}
			catch (Exception ex)
			{
				log.Add(new LogItem { MsgId = 15, P = new[] { ex.Source, ex.Message, ex.StackTrace, taggantCert } });
				throw;
			}
			finally
			{
				FlushLog("TaggantCertEnsure", new SqlInt32(2), customerId, log);
			}
		}

		private static void FlushLog(string src, SqlInt32 refKindId, SqlInt32 refId, IEnumerable<LogItem> log)
		{
			using (var conn = new SqlConnection("context connection=true"))
			{
				conn.Open();
				foreach (var li in log)
				{
					var insLogCmd = conn.CreateCommand();
					insLogCmd.Parameters.Add(new SqlParameter("@RefKindID", refKindId));
					insLogCmd.Parameters.Add(new SqlParameter("@RefID", refId));
					insLogCmd.Parameters.Add(new SqlParameter("@MsgID", li.MsgId));
					int i;
					for (i = 0; i < 2; i++)
					{
						insLogCmd.Parameters.Add(
							new SqlParameter(string.Format("@xml{0}", i), SqlDbType.Xml)
							{
								Value = (li.Xml == null || li.Xml[i] == null)
									? DBNull.Value
									: (object)new SqlXml(new XmlTextReader(li.Xml[i].InnerXml, XmlNodeType.Document, null))
							});
					}
					insLogCmd.Parameters.Add(new SqlParameter("@P0", src));
					i = 1;
					foreach (var p in li.P)
					{
						insLogCmd.Parameters.Add(new SqlParameter(string.Format("@P{0}", i++), p));
					}
					for (; i <= 8; i++)
					{
						insLogCmd.Parameters.Add(new SqlParameter(string.Format("@P{0}", i), DBNull.Value));
					}

					insLogCmd.CommandText =
						"INSERT dbo.Log(RefID, RefKindID, MsgID, xml, xml2, P0, P1, P2, P3, P4, P5, P6, P7, P8)" +
						" VALUES(@RefID, @RefKindID, @MsgID, @xml0, @xml1, @P0, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8)";

					insLogCmd.ExecuteNonQuery();
				}
			}
		}

		[SqlProcedure]
		public static SqlInt32 TaggantGetPolicies(SqlInt32 taggantConfigId)
		{
			var tc = GetTaggantConfig(taggantConfigId);
			var log = new List<LogItem>();
			try
			{
				var meta = new[]
				{
					new SqlMetaData("defaultName", SqlDbType.NVarChar, -1),
					new SqlMetaData("groupId", SqlDbType.Int),
					new SqlMetaData("oIdReferenceId", SqlDbType.Int),
					new SqlMetaData("certificateProfileId", SqlDbType.NVarChar, -1)
				};
				SqlDataRecord[] records = TaggantWebService.GetPolicies(tc, log).Select(x =>
				{
					var r = new SqlDataRecord(meta);
					r.SetSqlString(0, x.defaultName);
					r.SetSqlInt32(1, (int)x.group);
					r.SetSqlInt32(2, x.oIDReferenceID);
					r.SetSqlString(3, x.value);
					return r;
				}).ToArray();

				if (SqlContext.Pipe != null)
				{
					SqlContext.Pipe.SendResultsStart(new SqlDataRecord(meta));
					foreach (var r in records)
						SqlContext.Pipe.SendResultsRow(r);
					SqlContext.Pipe.SendResultsEnd();
				}
				return records.Length;
			}
			catch (Exception ex)
			{
				log.Add(new LogItem { MsgId = 15, P = new[] { ex.Source, ex.Message, ex.StackTrace } });
				throw;
			}
			finally
			{
				FlushLog("TaggantGetPolicies", new SqlInt32(), new SqlInt32(), log);
			}
		}
	}
}