Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Implement equivalent of certreq -sign for CSRs in native code #15

Open
jalliot opened this issue Feb 5, 2018 · 0 comments

Comments

@jalliot
Copy link

jalliot commented Feb 5, 2018

certreq -sign allows to sign CSRs with personal certificates, allowing to use the "This number of authorized signatures" feature from the "Issuance requirements" tab of certificate templates.
It is also used for enrolment agents to sign on behalf of someone else I believe (I never used that feature though).

Having this in native code would be really helpful, especially because currently there are some things that can only be done with this command.
SignedCms never worked for me but I managed to use the X509Enrollment COM classes to sign a PKCS#10, never a PKCS#7 or CMC directly.

Note that this should work with any kind of CSP/KSP. In my environment for instance I am using a signing certificate stored on a smart card and using a 3rd party KSP (which is why SignedCms did not work if I recall correctly...).

    $csr = Get-CertificateRequest -Path $Path
    if ($csr.RequestType -eq [System.Security.Cryptography.X509CertificateRequests.X509CertificateRequestType]::PKCS10) {
        $pkcs10 = New-Object -ComObject X509Enrollment.CX509CertificateRequestPkcs10
        $pkcs10.InitializeDecode([Convert]::ToBase64String($csr.RawData), 0x1)
        $pkcs7 = New-Object -ComObject X509enrollment.CX509CertificateRequestPkcs7
        $pkcs7.InitializeFromInnerRequest($pkcs10)
        $signer = New-Object -ComObject X509Enrollment.CSignerCertificate
        $signer.Initialize(0, 0, 0xC, $cert.Thumbprint)
        $pkcs7.SignerCertificate = $signer
        $pkcs7.Encode()
        $pkcs7.RawData(0x0) | Out-File -FilePath $outFile -Force
        [void] [Runtime.InteropServices.Marshal]::ReleaseComObject($pkcs7)
        [void] [Runtime.InteropServices.Marshal]::ReleaseComObject($pkcs10)
        [void] [Runtime.InteropServices.Marshal]::ReleaseComObject($signer)
    } else {
        certreq.exe -sign -cert $cert.Thumbprint $Path $outFile
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants