I try to generate a self signed code signing certificate and move it into the trusted store. Everything works great (with the help of https://stackoverflow.com/a/52535184/10819755) except that I need a code signing certificate instead of a "normal" certificate. Is there any way to change the way to generate the certificate or a way to convert the certificate into a code signing cert?
Thank you for helping.
Code:
string certlocation = Environment.ExpandEnvironmentVariables("%appdata%\\x\\");
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
var ecdsa = ECDsa.Create(); // generate asymmetric key pair
var req = new CertificateRequest("cn=" + certname, ecdsa, HashAlgorithmName.SHA256);
var cert = req.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(5));
// Create PFX (PKCS #12) with private key
File.WriteAllBytes(certlocation + certname + ".pfx", cert.Export(X509ContentType.Pfx));
// Create Base 64 encoded CER (public key only)
File.WriteAllText(certlocation + certname + ".cer",
"-----BEGIN CERTIFICATE-----\r\n"
+ Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks)
+ "\r\n-----END CERTIFICATE-----");
string cerFileName = certlocation + certname + ".pfx";
X509Certificate2 certificate = new X509Certificate2(cerFileName);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();