- /* We are now totally happy, lets make and sign the certificate */
- if (verbose)
- BIO_printf(bio_err,
- "Everything appears to be ok, creating and signing the certificate\n");
-
- if ((ret = X509_new()) == NULL)
- goto end;
-
-#ifdef X509_V3
- /* Make it an X509 v3 certificate. */
- if (!X509_set_version(ret, 2))
- goto end;
-#endif
-
- if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL)
- goto end;
- if (selfsign) {
- if (!X509_set_issuer_name(ret, subject))
- goto end;
- } else {
- if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
- goto end;
- }
-
- if (!set_cert_times(ret, startdate, enddate, days))
- goto end;
-
- if (enddate != NULL) {
- int tdays;
- ASN1_TIME_diff(&tdays, NULL, NULL, X509_get0_notAfter(ret));
- days = tdays;
- }
-
- if (!X509_set_subject_name(ret, subject))
- goto end;
-
- pktmp = X509_REQ_get0_pubkey(req);
- i = X509_set_pubkey(ret, pktmp);
- if (!i)
- goto end;
-
- /* Lets add the extensions, if there are any */
- if (ext_sect) {
- X509V3_CTX ctx;
-
- /* Initialize the context structure */
- if (selfsign)
- X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
- else
- X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
-
- if (extconf != NULL) {
- if (verbose)
- BIO_printf(bio_err, "Extra configuration file found\n");
-
- /* Use the extconf configuration db LHASH */
- X509V3_set_nconf(&ctx, extconf);
-
- /* Test the structure (needed?) */
- /* X509V3_set_ctx_test(&ctx); */
-
- /* Adds exts contained in the configuration file */
- if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {
- BIO_printf(bio_err,
- "ERROR: adding extensions in section %s\n",
- ext_sect);
- ERR_print_errors(bio_err);
- goto end;
- }
- if (verbose)
- BIO_printf(bio_err,
- "Successfully added extensions from file.\n");
- } else if (ext_sect) {
- /* We found extensions to be set from config file */
- X509V3_set_nconf(&ctx, lconf);
-
- if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
- BIO_printf(bio_err,
- "ERROR: adding extensions in section %s\n",
- ext_sect);
- ERR_print_errors(bio_err);
- goto end;
- }
-
- if (verbose)
- BIO_printf(bio_err,
- "Successfully added extensions from config\n");
- }
- }
-
- /* Copy extensions from request (if any) */
-
- if (!copy_extensions(ret, req, ext_copy)) {
- BIO_printf(bio_err, "ERROR: adding extensions from request\n");
- ERR_print_errors(bio_err);
- goto end;
- }
-
- {
- const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(ret);
-
- if (exts != NULL && sk_X509_EXTENSION_num(exts) > 0)
- /* Make it an X509 v3 certificate. */
- if (!X509_set_version(ret, 2))
- goto end;
- }
-
- /* Set the right value for the noemailDN option */
- if (email_dn == 0) {
- if (!X509_set_subject_name(ret, dn_subject))
- goto end;
- }
-