Use New-SelfSignedCertificate to create a Trusted Root certificate

Gordon Saxby
Gordon Saxby used Ask the Experts™
on
I am trying to use New-SelfSignedCertificate to create a Trusted Root certificate and then to create some Personal certificates to use for local development testing (IIS).

I followed the instructions on this site http://blog.davidchristiansen.com/2016/09/howto-create-self-signed-certificates-with-powershell/

I used this to create the root certificate:
New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname "XXX Local Certificate Authority"

Open in new window


When I look at the certificate, the intended purposes are:

  • Proves your identity to a remote computer
  • Ensures the identity of a remote computer
  • All issuance policies

Rather than the expected:

  • All issuance policies
  • All application policies

I created a client certificate with:
New-SelfSignedCertificate -Subject *.testing.local  -DnsName *.testing.local, testing.local -CertStoreLocation Cert:\LocalMachine\My  -Signer $rootcert

Open in new window


Which seems to work, but the certificate says "This certificate is not valid for the selected purpose"

The certificates do kind of work in IIS, but I have Kaspersky and it keeps interrupting, saying certificate not valid (or something).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:
You're going to need to do a bit more work to generate a CA certificate. That is, a certificate which is suitable for signing other certificates.

I suggest you start with this for the root certificate:
New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname "XXX Local Certificate Authority" -KeyusageProperty Sign

Open in new window

It might not be enough, but I can't test it, requires either Windows 10 or Server 2016 and I have neither here.
Gordon SaxbySenior Web Developer

Author

Commented:
Unfortunately, that didn't help (adding -KeyusageProperty Sign). The root cert appears to be exactly the same as before. I then created a client certificate and the problem still exists.

Selecting the root cert in the "Certification path" tab shows this in the status panel - "This certificate does not appear to be valid for the selected purpose." The Certificate Information panel (on general tab) says "This certificate is not valid for the selected purpose".
PowerShell Developer
Top Expert 2010
Commented:
You need to set valid key usages  and extended key usages to create a CA certificate. Look at the others in the store, they will all have a wide array of usages attached to them. It's going to take a bit of playing.
New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname "XXX Local Certificate Authority" -KeyusageProperty All -KeyUsage CertSign, CRLSign, DigitalSiganture

Open in new window

I think you won't need then enhanced key usages, but it needs to get to a state where Windows considers it a suitable CA certificate. The CA certificate must also be in the personal store for your next command to work (where this is used as a signing certificate). That is, you'll need it in trusted root on clients leveraging the key chain, and personal on the "CA" itself.
Gordon SaxbySenior Web Developer

Author

Commented:
OK - I think that has sorted it (after I corrected a slight typo in your command - DigitalSignature rather than DigitalSiganture)

I'll try it for a bit longer before I close the question.
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Amusingly, I inherited the typo from MS:

https://technet.microsoft.com/en-us/itpro/powershell/windows/pkiclient/new-selfsignedcertificate

Help section for the KeyUsage parameter.

Ah well, you fixed it, hopefully that's sufficient now :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial