BL
Size: a a a
ML
BL
ML
DK
BL
BL
AM
BL
BL
AM
AK
AM
BL
BL
AK
AM
BL
AM
/** Creates a DER-enc certificate and corresponding RSA key-pair
*/
bool OpenSSL::createSelfSignedCertificateAndRSAKeyPair(std::string &newCert, std::string &privateKey, std::string &publicKey)
{
X509* x509 = NULL;
X509_NAME* name = NULL;
EVP_PKEY* rsaPrivKey = NULL;
EVP_PKEY* rsaPubKey = NULL;
if (!OpenSSL::generateRsaKeyPair(2048, publicKey, privateKey)) goto EXIT;
rsaPubKey = RsaPublicKey(publicKey);
rsaPrivKey = RsaPrivateKey(privateKey);
x509 = X509_new();
X509_gmtime_adj(X509_get_notBefore(x509), 0);
X509_gmtime_adj(X509_get_notAfter(x509), (long)60 * 60 * 24 * VALIDITY_LIMIT_ONE_WEEK);
X509_set_pubkey(x509, rsaPubKey);
/* set the name of the issuer to the name of the subject. */
name = X509_get_subject_name(x509);
if (1 != X509_NAME_add_entry_by_txt(name,"O", MBSTRING_ASC, (const uint8_t*)”
*elided*”, -1, -1, 0)){
goto EXIT;
}
X509_set_issuer_name(x509, name);
/* finally sign the certificate with the key. */
X509_sign(x509, rsaPrivKey, EVP_sha256());
if (!certificate(x509, newCert)) goto EXIT;
EXIT:
if (x509) X509_free(x509);
if (rsaPubKey) EVP_PKEY_free(rsaPubKey);
if (rsaPrivKey) EVP_PKEY_free(rsaPrivKey);
return newCert.length() > 0 ? true : false;
}