22#include <winpr/assert.h>
24#include <winpr/path.h>
25#include <winpr/file.h>
26#include <winpr/cmdline.h>
27#include <winpr/sysinfo.h>
28#include <winpr/crypto.h>
29#include <winpr/print.h>
32#include <openssl/crypto.h>
33#include <openssl/conf.h>
34#include <openssl/pem.h>
35#include <openssl/err.h>
36#include <openssl/rsa.h>
37#include <openssl/pkcs12.h>
38#include <openssl/x509v3.h>
39#include <openssl/bn.h>
42#include <winpr/tools/makecert.h>
48struct S_MAKECERT_CONTEXT
78static BOOL utils_set_umask(
void)
81 (void)umask(S_IRWXG | S_IRWXO);
86WINPR_ATTR_MALLOC(winpr_zfree, 1)
87static
char* makecert_read_str(BIO* bio,
size_t* pOffset)
92 char* x509_str =
nullptr;
94 while (offset >= length)
97 char* new_str =
nullptr;
98 size_t new_len = length + 2048ull;
100 if (new_len > INT_MAX)
106 new_str = (
char*)realloc(x509_str, new_len);
117#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
118 status = BIO_read_ex(bio, &x509_str[offset], length - offset, &readBytes);
120 status = BIO_read(bio, &x509_str[offset], length - offset);
137 x509_str[offset] =
'\0';
139 *pOffset = offset + 1;
148 if (!argv || (argc < 1))
151 printf(
"Usage: %s [options] [output file]\n", argv[0]);
157 if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
160 printf(
"%-20s", arg->Name);
161 printf(
"\t%s\n", arg->Text);
163 else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
164 (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
170 size_t length = strlen(arg->Name) + strlen(arg->Format) + 2;
171 str = malloc(length + 1);
176 (void)sprintf_s(str, length + 1,
"%s %s", arg->Name, arg->Format);
177 (void)printf(
"%-20s", str);
182 printf(
"%-20s", arg->Name);
185 printf(
"\t%s\n", arg->Text);
187 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
194static int x509_add_ext(X509* cert,
int nid,
char* value)
197 X509_EXTENSION* ext =
nullptr;
202 X509V3_set_ctx_nodb(&ctx) X509V3_set_ctx(&ctx, cert, cert,
nullptr,
nullptr, 0);
203 ext = X509V3_EXT_conf_nid(
nullptr, &ctx, nid, value);
208 X509_add_ext(cert, ext, -1);
209 X509_EXTENSION_free(ext);
215static char* x509_name_parse(
char* name,
char* txt,
size_t* length)
218 char* entry =
nullptr;
220 if (!name || !txt || !length)
223 p = strstr(name, txt);
228 entry = p + strlen(txt) + 1;
229 p = strchr(entry,
'=');
232 *length = strlen(entry);
234 *length = (size_t)(p - entry);
239WINPR_ATTR_MALLOC(free, 1)
240static
char* get_name(COMPUTER_NAME_FORMAT type)
244 if (GetComputerNameExA(type,
nullptr, &nSize))
247 if (GetLastError() != ERROR_MORE_DATA)
250 char* computerName = calloc(1, nSize);
255 if (!GetComputerNameExA(type, computerName, &nSize))
264WINPR_ATTR_MALLOC(free, 1)
265static
char* x509_get_default_name(
void)
267 char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
269 computerName = get_name(ComputerNamePhysicalNetBIOS);
274static int command_line_pre_filter(
void* pvctx,
int index,
int argc, LPSTR* argv)
276 MAKECERT_CONTEXT* context = pvctx;
277 if (!context || !argv || (index < 0) || (argc < 0))
280 if (index == (argc - 1))
282 if (argv[index][0] !=
'-')
284 context->output_file = _strdup(argv[index]);
286 if (!context->output_file)
297static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
304 if (!context || !argv || (argc < 0))
311 CommandLineClearArgumentsA(args);
312 flags = COMMAND_LINE_SEPARATOR_SPACE | COMMAND_LINE_SIGIL_DASH;
313 status = CommandLineParseArgumentsA(argc, argv, args, flags, context, command_line_pre_filter,
316 if (status & COMMAND_LINE_STATUS_PRINT_HELP)
318 makecert_print_command_line_help(args, argc, argv);
327 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
330 CommandLineSwitchStart(arg)
332 CommandLineSwitchCase(arg,
"silent")
334 context->silent = TRUE;
336 CommandLineSwitchCase(arg,
"live")
338 context->live = TRUE;
340 CommandLineSwitchCase(arg,
"format")
342 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
345 if (strcmp(arg->Value,
"crt") == 0)
347 context->crtFormat = TRUE;
348 context->pemFormat = FALSE;
349 context->pfxFormat = FALSE;
351 else if (strcmp(arg->Value,
"pem") == 0)
353 context->crtFormat = FALSE;
354 context->pemFormat = TRUE;
355 context->pfxFormat = FALSE;
357 else if (strcmp(arg->Value,
"pfx") == 0)
359 context->crtFormat = FALSE;
360 context->pemFormat = FALSE;
361 context->pfxFormat = TRUE;
366 CommandLineSwitchCase(arg,
"path")
368 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
371 context->output_path = _strdup(arg->Value);
373 if (!context->output_path)
376 CommandLineSwitchCase(arg,
"p")
378 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
381 context->password = _strdup(arg->Value);
383 if (!context->password)
386 CommandLineSwitchCase(arg,
"n")
388 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
391 context->common_name = _strdup(arg->Value);
393 if (!context->common_name)
396 CommandLineSwitchCase(arg,
"y")
400 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
403 val = strtol(arg->Value,
nullptr, 0);
405 if ((errno != 0) || (val < 0) || (val > INT32_MAX))
408 context->duration_years = (int)val;
410 CommandLineSwitchCase(arg,
"m")
414 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
417 val = strtol(arg->Value,
nullptr, 0);
419 if ((errno != 0) || (val < 0))
422 context->duration_months = (int)val;
424 CommandLineSwitchDefault(arg)
427 CommandLineSwitchEnd(arg)
428 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
433int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context,
const char* name)
438 free(context->output_file);
439 context->output_file =
nullptr;
442 context->output_file = _strdup(name);
444 if (!context->output_file)
450int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context,
const char* path)
456 char* fullpath =
nullptr;
460 char* x509_str =
nullptr;
465 if (!context->output_file)
467 context->output_file = _strdup(context->default_name);
469 if (!context->output_file)
476 size_t length = strlen(context->output_file);
477 char* filename = malloc(length + 8);
482 if (context->crtFormat)
484 else if (context->pemFormat)
486 else if (context->pfxFormat)
491 (void)sprintf_s(filename, length + 8,
"%s.%s", context->output_file, ext);
494 fullpath = GetCombinedPath(path, filename);
496 fullpath = _strdup(filename);
501 fp = winpr_fopen(fullpath,
"w+");
505 if (context->pfxFormat)
507 if (!context->password)
509 BYTE random[32] = WINPR_C_ARRAY_INIT;
510 if (winpr_RAND(random,
sizeof(random)) < 0)
513 context->password = winpr_BinToHexString(random,
sizeof(random), FALSE);
515 if (!context->password)
518 printf(
"Using random export password \"%s\"\n", context->password);
521#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
522 OpenSSL_add_all_algorithms();
523 OpenSSL_add_all_ciphers();
524 OpenSSL_add_all_digests();
526 if (OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
527 OPENSSL_INIT_LOAD_CONFIG,
531 context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
532 context->x509,
nullptr, 0, 0, 0, 0, 0);
534 if (!context->pkcs12)
537 bio = BIO_new(BIO_s_mem());
542 status = i2d_PKCS12_bio(bio, context->pkcs12);
547 x509_str = makecert_read_str(bio, &offset);
554 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
559 bio = BIO_new(BIO_s_mem());
564 if (!PEM_write_bio_X509(bio, context->x509))
567 x509_str = makecert_read_str(bio, &offset);
574 if (fwrite(x509_str, length, 1, fp) != 1)
582 if (context->pemFormat)
584 bio = BIO_new(BIO_s_mem());
589 status = PEM_write_bio_PrivateKey(bio, context->pkey,
nullptr,
nullptr, 0,
nullptr,
595 x509_str = makecert_read_str(bio, &offset);
601 if (fwrite(x509_str, length, 1, fp) != 1)
619 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
624int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context,
const char* path)
630 char* filename =
nullptr;
631 char* fullpath =
nullptr;
634 char* x509_str =
nullptr;
636 if (!context->crtFormat)
639 if (!context->output_file)
641 context->output_file = _strdup(context->default_name);
643 if (!context->output_file)
650 length = strlen(context->output_file);
651 filename = malloc(length + 8);
656 (void)sprintf_s(filename, length + 8,
"%s.key", context->output_file);
659 fullpath = GetCombinedPath(path, filename);
661 fullpath = _strdup(filename);
666 fp = winpr_fopen(fullpath,
"w+");
671 bio = BIO_new(BIO_s_mem());
676 if (!PEM_write_bio_PrivateKey(bio, context->pkey,
nullptr,
nullptr, 0,
nullptr,
nullptr))
679 x509_str = makecert_read_str(bio, &offset);
686 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
701 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
708static BOOL makecert_create_rsa(EVP_PKEY** ppkey,
size_t key_length)
714#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
716#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
717 rsa = RSA_generate_key(key_length, RSA_F4,
nullptr,
nullptr);
720 BIGNUM* bn = BN_secure_new();
733 BN_set_word(bn, RSA_F4);
734 const int res = RSA_generate_key_ex(rsa, key_length, bn,
nullptr);
742 if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
749 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(
nullptr,
"RSA",
nullptr);
753 if (EVP_PKEY_keygen_init(pctx) != 1)
757 WINPR_ASSERT(key_length <= UINT_MAX);
758 unsigned int keylen = (
unsigned int)key_length;
759 const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint(
"bits", &keylen),
760 OSSL_PARAM_construct_end() };
761 if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
765 if (EVP_PKEY_generate(pctx, ppkey) != 1)
770 EVP_PKEY_CTX_free(pctx);
776int makecert_context_process(MAKECERT_CONTEXT* context,
int argc,
char** argv)
781 {
"rdp", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
782 "Unsupported - Generate certificate with required options for RDP usage." },
783 {
"silent", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
784 "Silently generate certificate without verbose output." },
785 {
"live", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
786 "Generate certificate live in memory when used as a library." },
787 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<crt|pem|pfx>",
nullptr,
nullptr, -1,
nullptr,
788 "Specify certificate file format" },
789 {
"path", COMMAND_LINE_VALUE_REQUIRED,
"<path>",
nullptr,
nullptr, -1,
nullptr,
790 "Specify certificate file output path" },
791 {
"p", COMMAND_LINE_VALUE_REQUIRED,
"<password>",
nullptr,
nullptr, -1,
nullptr,
792 "Specify certificate export password" },
796 {
"n", COMMAND_LINE_VALUE_REQUIRED,
"<name>",
nullptr,
nullptr, -1,
nullptr,
797 "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
798 "The simplest method is to specify the name in double quotes, preceded by CN=; for "
800 "-n \"CN=myName\"." },
801 {
"pe", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
802 "Unsupported - Marks the generated private key as exportable. This allows the private "
804 "be included in the certificate." },
805 {
"sk", COMMAND_LINE_VALUE_REQUIRED,
"<keyname>",
nullptr,
nullptr, -1,
nullptr,
806 "Unsupported - Specifies the subject's key container location, which contains the "
809 "If a key container does not exist, it will be created." },
810 {
"sr", COMMAND_LINE_VALUE_REQUIRED,
"<location>",
nullptr,
nullptr, -1,
nullptr,
811 "Unsupported - Specifies the subject's certificate store location. location can be "
813 "currentuser (the default) or localmachine." },
814 {
"ss", COMMAND_LINE_VALUE_REQUIRED,
"<store>",
nullptr,
nullptr, -1,
nullptr,
815 "Unsupported - Specifies the subject's certificate store name that stores the output "
817 {
"#", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
818 "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
820 "by Makecert.exe." },
821 {
"$", COMMAND_LINE_VALUE_REQUIRED,
"<authority>",
nullptr,
nullptr, -1,
nullptr,
822 "Unsupported - Specifies the signing authority of the certificate, which must be set to "
824 "(for certificates used by commercial software publishers) or individual (for "
826 "used by individual software publishers)." },
830 {
"a", COMMAND_LINE_VALUE_REQUIRED,
"<algorithm>",
nullptr,
nullptr, -1,
nullptr,
831 "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
832 "sha384, or sha512." },
833 {
"b", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>",
nullptr,
nullptr, -1,
nullptr,
834 "Unsupported - Specifies the start of the validity period. Defaults to the current "
836 {
"crl", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
837 "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
838 {
"cy", COMMAND_LINE_VALUE_REQUIRED,
"<certType>",
nullptr,
nullptr, -1,
nullptr,
839 "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
840 "authority for certification authority." },
841 {
"e", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>",
nullptr,
nullptr, -1,
nullptr,
842 "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
844 {
"eku", COMMAND_LINE_VALUE_REQUIRED,
"<oid[,oid…]>",
nullptr,
nullptr, -1,
nullptr,
845 "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
846 "(OIDs) into the certificate." },
847 {
"h", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
848 "Unsupported - Specifies the maximum height of the tree below this certificate." },
849 {
"ic", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
850 "Unsupported - Specifies the issuer's certificate file." },
851 {
"ik", COMMAND_LINE_VALUE_REQUIRED,
"<keyName>",
nullptr,
nullptr, -1,
nullptr,
852 "Unsupported - Specifies the issuer's key container name." },
853 {
"iky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>",
nullptr,
nullptr, -1,
nullptr,
854 "Unsupported - Specifies the issuer's key type, which must be one of the following: "
855 "signature (which indicates that the key is used for a digital signature), "
856 "exchange (which indicates that the key is used for key encryption and key exchange), "
857 "or an integer that represents a provider type. "
858 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
859 {
"in", COMMAND_LINE_VALUE_REQUIRED,
"<name>",
nullptr,
nullptr, -1,
nullptr,
860 "Unsupported - Specifies the issuer's certificate common name." },
861 {
"ip", COMMAND_LINE_VALUE_REQUIRED,
"<provider>",
nullptr,
nullptr, -1,
nullptr,
862 "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
863 "CryptoAPI provider name, see the –sp option." },
864 {
"ir", COMMAND_LINE_VALUE_REQUIRED,
"<location>",
nullptr,
nullptr, -1,
nullptr,
865 "Unsupported - Specifies the location of the issuer's certificate store. location can be "
866 "either currentuser (the default) or localmachine." },
867 {
"is", COMMAND_LINE_VALUE_REQUIRED,
"<store>",
nullptr,
nullptr, -1,
nullptr,
868 "Unsupported - Specifies the issuer's certificate store name." },
869 {
"iv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>",
nullptr,
nullptr, -1,
nullptr,
870 "Unsupported - Specifies the issuer's .pvk private key file." },
871 {
"iy", COMMAND_LINE_VALUE_REQUIRED,
"<type>",
nullptr,
nullptr, -1,
nullptr,
872 "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
873 "CryptoAPI provider type, see the –sy option." },
874 {
"l", COMMAND_LINE_VALUE_REQUIRED,
"<link>",
nullptr,
nullptr, -1,
nullptr,
875 "Unsupported - Links to policy information (for example, to a URL)." },
876 {
"len", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
877 "Specifies the generated key length, in bits." },
878 {
"m", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
879 "Specifies the duration, in months, of the certificate validity period." },
880 {
"y", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
881 "Specifies the duration, in years, of the certificate validity period." },
882 {
"nscp", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
883 "Unsupported - Includes the Netscape client-authorization extension." },
884 {
"r", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
885 "Unsupported - Creates a self-signed certificate." },
886 {
"sc", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
887 "Unsupported - Specifies the subject's certificate file." },
888 {
"sky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>",
nullptr,
nullptr, -1,
nullptr,
889 "Unsupported - Specifies the subject's key type, which must be one of the following: "
890 "signature (which indicates that the key is used for a digital signature), "
891 "exchange (which indicates that the key is used for key encryption and key exchange), "
892 "or an integer that represents a provider type. "
893 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
894 {
"sp", COMMAND_LINE_VALUE_REQUIRED,
"<provider>",
nullptr,
nullptr, -1,
nullptr,
895 "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
897 "registry subkeys of "
898 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
901 "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
903 {
"sv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>",
nullptr,
nullptr, -1,
nullptr,
904 "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
907 {
"sy", COMMAND_LINE_VALUE_REQUIRED,
"<type>",
nullptr,
nullptr, -1,
nullptr,
908 "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
910 "registry subkeys of "
911 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
913 "–sy and –sp are present, "
914 "the name of the CryptoAPI provider must correspond to the Name value of the provider "
917 {
"tbs", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
918 "Unsupported - Specifies the certificate or CRL file to be signed." },
922 {
"?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP,
nullptr,
nullptr,
nullptr, -1,
923 "help",
"print help" },
924 {
"!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP,
nullptr,
nullptr,
nullptr, -1,
925 "help-ext",
"print extended help" },
926 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
930 X509_NAME* name =
nullptr;
931 const EVP_MD* md =
nullptr;
933 int ret = makecert_context_parse_arguments(context, args, argc, argv);
940 if (!context->default_name && !context->common_name)
942 context->default_name = x509_get_default_name();
944 if (!context->default_name)
949 context->default_name = _strdup(context->common_name);
951 if (!context->default_name)
955 if (!context->common_name)
957 context->common_name = _strdup(context->default_name);
959 if (!context->common_name)
964 context->pkey = EVP_PKEY_new();
970 context->x509 = X509_new();
975 size_t key_length = 2048;
976 arg = CommandLineFindArgumentA(args,
"len");
978 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
980 unsigned long val = strtoul(arg->Value,
nullptr, 0);
982 if ((errno != 0) || (val > INT_MAX))
987 if (!makecert_create_rsa(&context->pkey, key_length))
990 if (X509_set_version(context->x509, 2) != 1)
993 arg = CommandLineFindArgumentA(args,
"#");
995 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
997 serial = strtol(arg->Value,
nullptr, 0);
1003 serial = (long)GetTickCount64();
1005 if (ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial) != 1)
1009 ASN1_TIME* before =
nullptr;
1010 ASN1_TIME* after =
nullptr;
1011#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1012 before = X509_get_notBefore(context->x509);
1013 after = X509_get_notAfter(context->x509);
1015 before = X509_getm_notBefore(context->x509);
1016 after = X509_getm_notAfter(context->x509);
1018 X509_gmtime_adj(before, 0);
1020 long duration = context->duration_months * 31l + context->duration_years * 365l;
1021 duration *= 60l * 60l * 24l;
1022 X509_gmtime_adj(after, duration);
1024 if (X509_set_pubkey(context->x509, context->pkey) != 1)
1027 name = X509_get_subject_name(context->x509);
1028 arg = CommandLineFindArgumentA(args,
"n");
1030 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1033 char* entry = x509_name_parse(arg->Value,
"C", &length);
1037 if (length > INT32_MAX)
1039 if (X509_NAME_add_entry_by_txt(name,
"C", MBSTRING_UTF8, (
const unsigned char*)entry,
1040 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
1044 entry = x509_name_parse(arg->Value,
"ST", &length);
1048 if (length > INT32_MAX)
1050 if (X509_NAME_add_entry_by_txt(name,
"ST", MBSTRING_UTF8, (
const unsigned char*)entry,
1051 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
1055 entry = x509_name_parse(arg->Value,
"L", &length);
1059 if (length > INT32_MAX)
1061 if (X509_NAME_add_entry_by_txt(name,
"L", MBSTRING_UTF8, (
const unsigned char*)entry,
1062 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
1066 entry = x509_name_parse(arg->Value,
"O", &length);
1070 if (length > INT32_MAX)
1072 if (X509_NAME_add_entry_by_txt(name,
"O", MBSTRING_UTF8, (
const unsigned char*)entry,
1073 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
1077 entry = x509_name_parse(arg->Value,
"OU", &length);
1081 if (length > INT32_MAX)
1083 if (X509_NAME_add_entry_by_txt(name,
"OU", MBSTRING_UTF8, (
const unsigned char*)entry,
1084 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
1088 entry = context->common_name;
1089 length = strlen(entry);
1090 if (length > INT32_MAX)
1093 if (X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1094 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
1099 char* entry = context->common_name;
1100 const size_t length = strlen(entry);
1101 if (length > INT32_MAX)
1103 if (X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1104 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
1108 if (X509_set_issuer_name(context->x509, name) != 1)
1111 if (x509_add_ext(context->x509, NID_ext_key_usage,
"serverAuth") != 1)
1114 arg = CommandLineFindArgumentA(args,
"a");
1117 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1119 md = EVP_get_digestbyname(arg->Value);
1124 if (!X509_sign(context->x509, context->pkey, md))
1131 if (!context->silent)
1133 BIO* bio = BIO_new(BIO_s_mem());
1138 const int status = X509_print(bio, context->x509);
1146 char* x509_str = makecert_read_str(bio,
nullptr);
1153 printf(
"%s", x509_str);
1164 if (!winpr_PathFileExists(context->output_path))
1166 if (!winpr_PathMakePath(context->output_path,
nullptr))
1170 if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1173 if (context->crtFormat)
1175 if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1182 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
1187MAKECERT_CONTEXT* makecert_context_new(
void)
1189 if (!utils_set_umask())
1192 MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1,
sizeof(MAKECERT_CONTEXT));
1196 context->crtFormat = TRUE;
1197 context->duration_years = 1;
1203void makecert_context_free(MAKECERT_CONTEXT* context)
1207 winpr_zfree(context->password);
1208 free(context->default_name);
1209 free(context->common_name);
1210 free(context->output_file);
1211 free(context->output_path);
1213 X509_free(context->x509);
1214 EVP_PKEY_free(context->pkey);
1215#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1216 CRYPTO_cleanup_all_ex_data();