-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateSignedCert.cs
193 lines (160 loc) · 6.51 KB
/
GenerateSignedCert.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Cryptool
{
public partial class GenSignedCert : Form
{
public GenSignedCert()
{
InitializeComponent();
}
private void GenSignedCert_Load(object sender, EventArgs e)
{
}
//Store CA Certificate to system
private void simpleButton2_Click(object sender, EventArgs e)
{
string[] certDetails = new string[5];
GenCASignedCert newCertGen = new GenCASignedCert();
string pfxPass = pfxPassword.Text;
certDetails = newCertGen.saveCAtoStore(StoreSelected.SelectedText, Location.Text, pfxPass);
dispThump.Text = certDetails[1];
displayDate.Text = certDetails[2];
disLocation.Text = certDetails[3];
DispDNName.Text = certDetails[0];
caThumbprint.Text = certDetails[1];
caStore.Text = StoreSelected.SelectedText;
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
//Load Certificate in pfx format
private void simpleButton1_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Browse Root CA in Pfx";
openFileDialog1.DefaultExt = "pfx";
openFileDialog1.Filter = "pfx files(*.pfx) | *.pfx";
openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Location.Text = openFileDialog1.FileName;
}
}
}
private void Location_EditValueChanged(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void labelControl9_Click(object sender, EventArgs e)
{
}
// Download a signed certificate
private void saveCert_Click(object sender, EventArgs e)
{
// Initialise byte array for Generated certificate
byte[] _certData;
// variable Store user preference
string _path;
// Save all the selected key usages as string in list
List<string> keyUsages = new List<string>();
//Validation for Algrithm seclection
if (hashAlgorithm.SelectedIndex == -1)//here should skip to else - but doesn't
{
MessageBox.Show("Select Algorithm for Hash your CA Certificate", "Error");
}
//Valiadation for Key Size
if (rsaKeySize.SelectedIndex == -1)//here should skip to else - but doesn't
{
MessageBox.Show("Select Key Size for you CA Certificate", "Error");
}
//
if (string.IsNullOrEmpty(passwordPfx.Text))//here should skip to else - but doesn't
{
MessageBox.Show("Enter a password", "Error");
}
if (string.IsNullOrEmpty(dnName.Text))
{
MessageBox.Show("Enter a DN Name", "Error");
}
else
{
try
{
SaveFileDialog saveFile = new SaveFileDialog();
if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// collect details from text box
string path = saveFile.FileName;
string _dnName = dnName.Text;
string _algorithm = "SHA256";
_algorithm = hashAlgorithm.Text;
keyUsages.Add("KeyCertSign");
string _password = passwordPfx.Text;
// default Certificate sign choice
if (isCRLSigner.Checked)
{
keyUsages.Add("crlsign");
}
if (isCertSigner.Checked)
{
keyUsages.Add("certSigner");
}
if (serverClientAuth.Checked)
{
keyUsages.Add("serverClientAuth");
}
if (isDataEncipher.Checked)
{
keyUsages.Add("DataEncipher");
}
// convert key size to integer
int _keySize = Int32.Parse(rsaKeySize.Text);
// path constrain
int _pathConstrain = 0;
// validity
int _noOfYears = (int)Decimal.Round(noOfYears.Value, 0);
// Create instance
GenCASignedCert newCertifcatereq = new GenCASignedCert();
// call method
//(int keySize, string subjectDN, string hashAlg, List<string> keyUsages, int pathLenght, int numberofYears, bool isCA)
X509Certificate2 certificate = newCertifcatereq.CreateSignedCertificate(_keySize, _dnName, _algorithm, keyUsages, _noOfYears, caThumbprint.Text, serialNumber.Text,caStore.Text);
// Export certificate as pfx and save to user defined location
_certData = certificate.Export(X509ContentType.Pfx, _password);
_path = path + ".pfx";
File.WriteAllBytes(_path, _certData);
}
else
{
MessageBox.Show("select Proper path");
}
}
catch (InvalidCastException erorr)
{
if (erorr.Source != null)
Console.WriteLine("IOException source: {0}", erorr.Source);
throw;
}
}
}
private void simpleButton1_Click_1(object sender, EventArgs e)
{
}
private void label8_Click(object sender, EventArgs e)
{
}
}
}