Skip to content

Commit

Permalink
Make sure the serial number field is always initialized
Browse files Browse the repository at this point in the history
fixes #9947
  • Loading branch information
gunnarbeutner committed Aug 18, 2015
1 parent 0cbcb75 commit 0f39d66
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/base/tlsutility.cpp
Expand Up @@ -379,9 +379,9 @@ boost::shared_ptr<X509> CreateCert(EVP_PKEY *pubkey, X509_NAME *subject, X509_NA
X509_set_subject_name(cert, subject);
X509_set_issuer_name(cert, issuer);

if (!serialfile.IsEmpty()) {
int serial = 0;
int serial = 1;

if (!serialfile.IsEmpty()) {
std::ifstream ifp;
ifp.open(serialfile.CStr());
ifp >> std::hex >> serial;
Expand All @@ -397,22 +397,28 @@ boost::shared_ptr<X509> CreateCert(EVP_PKEY *pubkey, X509_NAME *subject, X509_NA

if (ofp.fail())
BOOST_THROW_EXCEPTION(std::runtime_error("Could not update serial file."));

ASN1_INTEGER_set(X509_get_serialNumber(cert), serial);
}

if (ca) {
X509_EXTENSION *ext;
X509V3_CTX ctx;
X509V3_set_ctx_nodb(&ctx);
X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
ext = X509V3_EXT_conf_nid(NULL, &ctx, NID_basic_constraints, const_cast<char *>("critical,CA:TRUE"));
ASN1_INTEGER_set(X509_get_serialNumber(cert), serial);

if (ext)
X509_add_ext(cert, ext, -1);
X509_EXTENSION *ext;
X509V3_CTX ctx;
X509V3_set_ctx_nodb(&ctx);
X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);

X509_EXTENSION_free(ext);
}
const char *attr;

if (ca)
attr = "critical,CA:TRUE";
else
attr = "critical,CA:FALSE";

ext = X509V3_EXT_conf_nid(NULL, &ctx, NID_basic_constraints, const_cast<char *>(attr));

if (ext)
X509_add_ext(cert, ext, -1);

X509_EXTENSION_free(ext);

X509_sign(cert, cakey, EVP_sha256());

Expand Down

0 comments on commit 0f39d66

Please sign in to comment.