1

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();
MAFRIESE
  • 11
  • 4
  • You want to get the certificate from the store : https://stackoverflow.com/questions/6304773/how-to-get-x509certificate-from-certificate-store-and-generate-xml-signature-dat – jdweng Dec 21 '18 at 10:45
  • @jdweng that's not the problem. My problem is that I generate the wrong type of certificate. I need a codesigning certificate instead of a "normal" certificate – MAFRIESE Dec 21 '18 at 12:00
  • Learn what extensions are required by a code signing certificate, https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.certificaterequest.certificateextensions?view=netframework-4.7.2#System_Security_Cryptography_X509Certificates_CertificateRequest_CertificateExtensions and then you go. – Lex Li Dec 21 '18 at 15:54
  • I have the same problem. Do you had found a solution? – amirfg Nov 11 '19 at 13:44

0 Answers0