-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfSignedCert.cs
137 lines (117 loc) · 4.81 KB
/
SelfSignedCert.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
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 GenerateSignedCert : DevExpress.XtraEditors.XtraForm
{
public GenerateSignedCert()
{
InitializeComponent();
}
private void saveAsPfx_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 (algorithm.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");
}
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 = dn.Text;
string _algorithm = "SHA256";
_algorithm = algorithm.Text;
keyUsages.Add("KeyCertSign");
string _password = passwordPfx.Text;
// default Certificate sign choice
if (crlSigner.Checked)
{
keyUsages.Add("crlsign");
}
if (certSigner.Checked)
{
keyUsages.Add("certSigner");
}
if (keyEncipher.Checked)
{
keyUsages.Add("keyEncipher");
}
if (serverClientAuth.Checked)
{
keyUsages.Add("serverClientAuth");
}
if (DataEncipher.Checked)
{
keyUsages.Add("DataEncipher");
}
// convert key size to integer
int _keySize = Int32.Parse(rsaKeySize.Text);
// path constrain
int _pathConstrain = (int)Decimal.Round(pathConstrain.Value, 0);
// validity
int _noOfYears = (int)Decimal.Round(validity.Value, 0);
// Create instance
GenerateSelfSigned newCertifcatereq = new GenerateSelfSigned();
// call method
//(int keySize, string subjectDN, string hashAlg, List<string> keyUsages, int pathLenght, int numberofYears, bool isCA)
X509Certificate2 certificate = newCertifcatereq.CreateCertificate(2048, _dnName, _algorithm, keyUsages, _pathConstrain, _noOfYears, true);
// 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 fluentDesignFormContainer1_Click(object sender, EventArgs e)
{
}
private void passwordPfx_EditValueChanged(object sender, EventArgs e)
{
}
private void labelControl7_Click(object sender, EventArgs e)
{
}
}
}