Creates a self-signed X.509 certificate.
Public Declare Function X509_MakeCertSelf Lib "diCrPKI.dll"
(ByVal strNewCertFile As String, ByVal strPriKeyFile As String,
ByVal nCertNum As Long, ByVal nYearsValid As Long,
ByVal strDistName As String, ByVal strExtensions As String,
ByVal KeyUsageFlags As Long,
ByVal strPassword As String, ByVal nOptions As Long) As Long
nRet = X509_MakeCertSelf(strNewCertFile, strPriKeyFile,
nCertNum, nYearsValid,
strDistName, strExtensions, KeyUsageFlags, strPassword, nOptions)
long __stdcall X509_MakeCertSelf(const char *szNewCertFile, const char *szPriKeyFile, long nCertNum, long nYearsValid, const char *szDistName, const char *szExtensions, long nKeyUsageFlags, const char *szPassword, long nOptions);
serialNumber
is set in szExtensions.""
or NULL
to ignore.
PKI_X509_KEYUSAGE_DIGITALSIGNATURE | to set digitalSignature |
PKI_X509_KEYUSAGE_NONREPUDIATION | to set nonRepudiation |
PKI_X509_KEYUSAGE_KEYENCIPHERMENT | to set keyEncipherment |
PKI_X509_KEYUSAGE_DATAENCIPHERMENT | to set dataEncipherment |
PKI_X509_KEYUSAGE_KEYAGREEMENT | to set keyAgreement |
PKI_X509_KEYUSAGE_KEYCERTSIGN | to set keyCertSign |
PKI_X509_KEYUSAGE_CRLSIGN | to set cRLSign |
PKI_X509_KEYUSAGE_ENCIPHERONLY | to set encipherOnly |
PKI_X509_KEYUSAGE_DECIPHERONLY | to set decipherOnly |
""
if key not encrypted [New in v12.0].sha1WithRSAEncryption
(default - CAUTION)sha224WithRSAEncryption
sha256WithRSAEncryption
[minimum recommended]sha384WithRSAEncryption
sha512WithRSAEncryption
md5WithRSAEncryption
[legacy, not recommended]md2WithRSAEncryption
[legacy, definitely not recommended]RSA-PSS-SHA1
RSA-PSS-SHA224
RSA-PSS-SHA256
RSA-PSS-SHA384
RSA-PSS-SHA512
ecdsaWithSHA1
ecdsaWithSHA224
ecdsaWithSHA256
ecdsaWithSHA384
ecdsaWithSHA512
Ed25519
[New in v20.0]Ed448
[New in v22.0]basicConstraints
subject type to be a CA (default = End Entity)basicConstraints
extension (default = include)UTF8String
(default = PrintableString)If successful, the return value is zero; otherwise it returns a nonzero error code.
static int dipki::X509::MakeCertSelf (const std::string &newCertFile, const std::string &priKeyFile, const std::string &password, int certNum, int yearsValid, const std::string distName, const std::string extns="", KeyUsageOptions keyUsageOptions=KeyUsageOptions::NoKeyUsageOption, SigAlg sigAlg=SigAlg::Default, CertOptions opts=CertOptions::Default_CertOpt)
static X509.make_cert_self(newcertfile, prikeyfile, password, certnum, yearsvalid, distname, extns="", keyusage=0, sigalg=0, opts=0)
See the remarks for X509_MakeCert()
.
A self-signed certificate has the same Issuer and Subject distinguished name.
Add the PKI_X509_UTF8
flag to encode the distinguished names in UTF-8.
The BasicConstraints
subject type will always be a CA for a version 3 self-signed certificate, unless
explicitly excluded with the PKI_X509_NO_BASIC
flag.
Dim nRet As Long Dim nKeyUsage As Long nKeyUsage = PKI_X509_KEYUSAGE_DIGITALSIGNATURE + _ PKI_X509_KEYUSAGE_KEYCERTSIGN + PKI_X509_KEYUSAGE_CRLSIGN nRet = X509_MakeCertSelf("myca.cer", "myca.p8e", 99, 10, _ "CN=My CA;O=Test Org;OU=Certificate Services", _ "", nKeyUsage, "password", 0) If nRet <> 0 Then Debug.Print nRet & " " & pkiGetLastError() Else Debug.Print "Success" End If
The above example will create a new self-signed X.509 certificate with filename myca.cer
.
The serial number will be 99.
It will be valid from today for 10 years.
The issuer's encrypted private key is in the file myca.p8e
and has
the password "password". The new certificate will be signed using the private key
using the default sha1WithRSAEncryption
algorithm.
The second example below shows how to specify a distinguished name using UTF-8-encoded CJK characters.
The PKI_X509_UTF8
flag must be used in this case.
Dim nRet As Long Dim nKeyUsage As Long Dim strDN As String ' Specify DN using chinese characters in UTF-8 ' CN=da wei (U+5927, U+536B) ' C=zhong guo (U+4E2D, U+56FD) strDN = "CN=#xE5A4A7E58DAB;C=#xe4b8ade59bbd" nKeyUsage = PKI_X509_KEYUSAGE_DIGITALSIGNATURE + PKI_X509_KEYUSAGE_KEYCERTSIGN + PKI_X509_KEYUSAGE_CRLSIGN nRet = X509_MakeCertSelf("myca-chinadavid.cer", "myca.p8e", _ &H888, 4, strDN, "", nKeyUsage, "password", PKI_X509_UTF8) If nRet <> 0 Then Debug.Print nRet & " " & pkiGetLastError() Else Debug.Print "Success" End If