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 const char* x509_name_parse(
const char* name,
const char* txt,
size_t* length)
217 if (!name || !txt || !length)
220 const char* entry =
nullptr;
223 (void)winpr_asprintf(&fmt, &fmtlen,
"%s=", txt);
227 const char* p = strstr(name, fmt);
233 const char* sep1 = strchr(entry,
';');
234 const char* sep2 = strchr(entry,
',');
235 const char* sep3 = strchr(entry,
' ');
236 if (!sep1 && !sep2 && !sep3)
237 *length = strlen(entry);
239 *length = WINPR_ASSERTING_INT_CAST(
size_t, sep1 - entry);
241 *length = WINPR_ASSERTING_INT_CAST(
size_t, sep2 - entry);
243 *length = WINPR_ASSERTING_INT_CAST(
size_t, sep3 - entry);
250WINPR_ATTR_MALLOC(free, 1)
251static
char* get_name(COMPUTER_NAME_FORMAT type)
255 if (GetComputerNameExA(type,
nullptr, &nSize))
258 if (GetLastError() != ERROR_MORE_DATA)
261 char* computerName = calloc(1, nSize);
266 if (!GetComputerNameExA(type, computerName, &nSize))
275WINPR_ATTR_MALLOC(free, 1)
276static
char* x509_get_default_name(
void)
278 char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
280 computerName = get_name(ComputerNamePhysicalNetBIOS);
285static int command_line_pre_filter(
void* pvctx,
int index,
int argc, LPSTR* argv)
287 MAKECERT_CONTEXT* context = pvctx;
288 if (!context || !argv || (index < 0) || (argc < 0))
291 if (index == (argc - 1))
293 if (argv[index][0] !=
'-')
295 context->output_file = _strdup(argv[index]);
297 if (!context->output_file)
308static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
315 if (!context || !argv || (argc < 0))
322 CommandLineClearArgumentsA(args);
323 flags = COMMAND_LINE_SEPARATOR_SPACE | COMMAND_LINE_SIGIL_DASH;
324 status = CommandLineParseArgumentsA(argc, argv, args, flags, context, command_line_pre_filter,
327 if (status & COMMAND_LINE_STATUS_PRINT_HELP)
329 makecert_print_command_line_help(args, argc, argv);
338 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
341 CommandLineSwitchStart(arg)
343 CommandLineSwitchCase(arg,
"silent")
345 context->silent = TRUE;
347 CommandLineSwitchCase(arg,
"live")
349 context->live = TRUE;
351 CommandLineSwitchCase(arg,
"format")
353 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
356 if (strcmp(arg->Value,
"crt") == 0)
358 context->crtFormat = TRUE;
359 context->pemFormat = FALSE;
360 context->pfxFormat = FALSE;
362 else if (strcmp(arg->Value,
"pem") == 0)
364 context->crtFormat = FALSE;
365 context->pemFormat = TRUE;
366 context->pfxFormat = FALSE;
368 else if (strcmp(arg->Value,
"pfx") == 0)
370 context->crtFormat = FALSE;
371 context->pemFormat = FALSE;
372 context->pfxFormat = TRUE;
377 CommandLineSwitchCase(arg,
"path")
379 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
382 context->output_path = _strdup(arg->Value);
384 if (!context->output_path)
387 CommandLineSwitchCase(arg,
"p")
389 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
392 context->password = _strdup(arg->Value);
394 if (!context->password)
397 CommandLineSwitchCase(arg,
"n")
399 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
402 context->common_name = _strdup(arg->Value);
404 if (!context->common_name)
407 CommandLineSwitchCase(arg,
"y")
411 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
414 val = strtol(arg->Value,
nullptr, 0);
416 if ((errno != 0) || (val < 0) || (val > INT32_MAX))
419 context->duration_years = (int)val;
421 CommandLineSwitchCase(arg,
"m")
425 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
428 val = strtol(arg->Value,
nullptr, 0);
430 if ((errno != 0) || (val < 0))
433 context->duration_months = (int)val;
435 CommandLineSwitchDefault(arg)
438 CommandLineSwitchEnd(arg)
439 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
444int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context,
const char* name)
449 free(context->output_file);
450 context->output_file =
nullptr;
453 context->output_file = _strdup(name);
455 if (!context->output_file)
461int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context,
const char* path)
467 char* fullpath =
nullptr;
471 char* x509_str =
nullptr;
476 if (!context->output_file)
478 context->output_file = _strdup(context->default_name);
480 if (!context->output_file)
487 size_t length = strlen(context->output_file);
488 char* filename = malloc(length + 8);
493 if (context->crtFormat)
495 else if (context->pemFormat)
497 else if (context->pfxFormat)
502 (void)sprintf_s(filename, length + 8,
"%s.%s", context->output_file, ext);
505 fullpath = GetCombinedPath(path, filename);
507 fullpath = _strdup(filename);
512 fp = winpr_fopen(fullpath,
"w+");
516 if (context->pfxFormat)
518 if (!context->password)
520 BYTE random[32] = WINPR_C_ARRAY_INIT;
521 if (winpr_RAND(random,
sizeof(random)) < 0)
524 context->password = winpr_BinToHexString(random,
sizeof(random), FALSE);
526 if (!context->password)
529 printf(
"Using random export password \"%s\"\n", context->password);
532#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
533 OpenSSL_add_all_algorithms();
534 OpenSSL_add_all_ciphers();
535 OpenSSL_add_all_digests();
537 if (OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
538 OPENSSL_INIT_LOAD_CONFIG,
542 context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
543 context->x509,
nullptr, 0, 0, 0, 0, 0);
545 if (!context->pkcs12)
548 bio = BIO_new(BIO_s_mem());
553 status = i2d_PKCS12_bio(bio, context->pkcs12);
558 x509_str = makecert_read_str(bio, &offset);
565 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
570 bio = BIO_new(BIO_s_mem());
575 if (!PEM_write_bio_X509(bio, context->x509))
578 x509_str = makecert_read_str(bio, &offset);
585 if (fwrite(x509_str, length, 1, fp) != 1)
593 if (context->pemFormat)
595 bio = BIO_new(BIO_s_mem());
600 status = PEM_write_bio_PrivateKey(bio, context->pkey,
nullptr,
nullptr, 0,
nullptr,
606 x509_str = makecert_read_str(bio, &offset);
612 if (fwrite(x509_str, length, 1, fp) != 1)
630 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
635int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context,
const char* path)
641 char* filename =
nullptr;
642 char* fullpath =
nullptr;
645 char* x509_str =
nullptr;
647 if (!context->crtFormat)
650 if (!context->output_file)
652 context->output_file = _strdup(context->default_name);
654 if (!context->output_file)
661 length = strlen(context->output_file);
662 filename = malloc(length + 8);
667 (void)sprintf_s(filename, length + 8,
"%s.key", context->output_file);
670 fullpath = GetCombinedPath(path, filename);
672 fullpath = _strdup(filename);
677 fp = winpr_fopen(fullpath,
"w+");
682 bio = BIO_new(BIO_s_mem());
687 if (!PEM_write_bio_PrivateKey(bio, context->pkey,
nullptr,
nullptr, 0,
nullptr,
nullptr))
690 x509_str = makecert_read_str(bio, &offset);
697 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
712 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
719static BOOL makecert_create_rsa(EVP_PKEY** ppkey,
size_t key_length)
725#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
727#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
728 rsa = RSA_generate_key(key_length, RSA_F4,
nullptr,
nullptr);
731 BIGNUM* bn = BN_secure_new();
744 BN_set_word(bn, RSA_F4);
745 const int res = RSA_generate_key_ex(rsa, key_length, bn,
nullptr);
753 if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
760 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(
nullptr,
"RSA",
nullptr);
764 if (EVP_PKEY_keygen_init(pctx) != 1)
768 WINPR_ASSERT(key_length <= UINT_MAX);
769 unsigned int keylen = (
unsigned int)key_length;
770 const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint(
"bits", &keylen),
771 OSSL_PARAM_construct_end() };
772 if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
776 if (EVP_PKEY_generate(pctx, ppkey) != 1)
781 EVP_PKEY_CTX_free(pctx);
787static int add_entry(X509_NAME* name,
const char* value,
const char* txt)
794 const char* entry = x509_name_parse(value, txt, &length);
797 if (length > INT32_MAX)
800 X509_NAME_add_entry_by_txt(name, txt, MBSTRING_UTF8, (
const unsigned char*)entry,
801 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0);
809int makecert_context_process(MAKECERT_CONTEXT* context,
int argc,
char** argv)
814 {
"rdp", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
815 "Unsupported - Generate certificate with required options for RDP usage." },
816 {
"silent", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
817 "Silently generate certificate without verbose output." },
818 {
"live", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
819 "Generate certificate live in memory when used as a library." },
820 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<crt|pem|pfx>",
nullptr,
nullptr, -1,
nullptr,
821 "Specify certificate file format" },
822 {
"path", COMMAND_LINE_VALUE_REQUIRED,
"<path>",
nullptr,
nullptr, -1,
nullptr,
823 "Specify certificate file output path" },
824 {
"p", COMMAND_LINE_VALUE_REQUIRED,
"<password>",
nullptr,
nullptr, -1,
nullptr,
825 "Specify certificate export password" },
829 {
"n", COMMAND_LINE_VALUE_REQUIRED,
"<name>",
nullptr,
nullptr, -1,
nullptr,
830 "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
831 "The simplest method is to specify the name in double quotes, preceded by CN=; for "
833 "-n \"CN=myName\"." },
834 {
"pe", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
835 "Unsupported - Marks the generated private key as exportable. This allows the private "
837 "be included in the certificate." },
838 {
"sk", COMMAND_LINE_VALUE_REQUIRED,
"<keyname>",
nullptr,
nullptr, -1,
nullptr,
839 "Unsupported - Specifies the subject's key container location, which contains the "
842 "If a key container does not exist, it will be created." },
843 {
"sr", COMMAND_LINE_VALUE_REQUIRED,
"<location>",
nullptr,
nullptr, -1,
nullptr,
844 "Unsupported - Specifies the subject's certificate store location. location can be "
846 "currentuser (the default) or localmachine." },
847 {
"ss", COMMAND_LINE_VALUE_REQUIRED,
"<store>",
nullptr,
nullptr, -1,
nullptr,
848 "Unsupported - Specifies the subject's certificate store name that stores the output "
850 {
"#", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
851 "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
853 "by Makecert.exe." },
854 {
"$", COMMAND_LINE_VALUE_REQUIRED,
"<authority>",
nullptr,
nullptr, -1,
nullptr,
855 "Unsupported - Specifies the signing authority of the certificate, which must be set to "
857 "(for certificates used by commercial software publishers) or individual (for "
859 "used by individual software publishers)." },
863 {
"a", COMMAND_LINE_VALUE_REQUIRED,
"<algorithm>",
nullptr,
nullptr, -1,
nullptr,
864 "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
865 "sha384, or sha512." },
866 {
"b", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>",
nullptr,
nullptr, -1,
nullptr,
867 "Unsupported - Specifies the start of the validity period. Defaults to the current "
869 {
"crl", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
870 "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
871 {
"cy", COMMAND_LINE_VALUE_REQUIRED,
"<certType>",
nullptr,
nullptr, -1,
nullptr,
872 "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
873 "authority for certification authority." },
874 {
"e", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>",
nullptr,
nullptr, -1,
nullptr,
875 "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
877 {
"eku", COMMAND_LINE_VALUE_REQUIRED,
"<oid[,oid…]>",
nullptr,
nullptr, -1,
nullptr,
878 "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
879 "(OIDs) into the certificate." },
880 {
"h", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
881 "Unsupported - Specifies the maximum height of the tree below this certificate." },
882 {
"ic", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
883 "Unsupported - Specifies the issuer's certificate file." },
884 {
"ik", COMMAND_LINE_VALUE_REQUIRED,
"<keyName>",
nullptr,
nullptr, -1,
nullptr,
885 "Unsupported - Specifies the issuer's key container name." },
886 {
"iky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>",
nullptr,
nullptr, -1,
nullptr,
887 "Unsupported - Specifies the issuer's key type, which must be one of the following: "
888 "signature (which indicates that the key is used for a digital signature), "
889 "exchange (which indicates that the key is used for key encryption and key exchange), "
890 "or an integer that represents a provider type. "
891 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
892 {
"in", COMMAND_LINE_VALUE_REQUIRED,
"<name>",
nullptr,
nullptr, -1,
nullptr,
893 "Unsupported - Specifies the issuer's certificate common name." },
894 {
"ip", COMMAND_LINE_VALUE_REQUIRED,
"<provider>",
nullptr,
nullptr, -1,
nullptr,
895 "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
896 "CryptoAPI provider name, see the –sp option." },
897 {
"ir", COMMAND_LINE_VALUE_REQUIRED,
"<location>",
nullptr,
nullptr, -1,
nullptr,
898 "Unsupported - Specifies the location of the issuer's certificate store. location can be "
899 "either currentuser (the default) or localmachine." },
900 {
"is", COMMAND_LINE_VALUE_REQUIRED,
"<store>",
nullptr,
nullptr, -1,
nullptr,
901 "Unsupported - Specifies the issuer's certificate store name." },
902 {
"iv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>",
nullptr,
nullptr, -1,
nullptr,
903 "Unsupported - Specifies the issuer's .pvk private key file." },
904 {
"iy", COMMAND_LINE_VALUE_REQUIRED,
"<type>",
nullptr,
nullptr, -1,
nullptr,
905 "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
906 "CryptoAPI provider type, see the –sy option." },
907 {
"l", COMMAND_LINE_VALUE_REQUIRED,
"<link>",
nullptr,
nullptr, -1,
nullptr,
908 "Unsupported - Links to policy information (for example, to a URL)." },
909 {
"len", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
910 "Specifies the generated key length, in bits." },
911 {
"m", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
912 "Specifies the duration, in months, of the certificate validity period." },
913 {
"y", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
914 "Specifies the duration, in years, of the certificate validity period." },
915 {
"nscp", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
916 "Unsupported - Includes the Netscape client-authorization extension." },
917 {
"r", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
918 "Unsupported - Creates a self-signed certificate." },
919 {
"sc", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
920 "Unsupported - Specifies the subject's certificate file." },
921 {
"sky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>",
nullptr,
nullptr, -1,
nullptr,
922 "Unsupported - Specifies the subject's key type, which must be one of the following: "
923 "signature (which indicates that the key is used for a digital signature), "
924 "exchange (which indicates that the key is used for key encryption and key exchange), "
925 "or an integer that represents a provider type. "
926 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
927 {
"sp", COMMAND_LINE_VALUE_REQUIRED,
"<provider>",
nullptr,
nullptr, -1,
nullptr,
928 "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
930 "registry subkeys of "
931 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
934 "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
936 {
"sv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>",
nullptr,
nullptr, -1,
nullptr,
937 "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
940 {
"sy", COMMAND_LINE_VALUE_REQUIRED,
"<type>",
nullptr,
nullptr, -1,
nullptr,
941 "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
943 "registry subkeys of "
944 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
946 "–sy and –sp are present, "
947 "the name of the CryptoAPI provider must correspond to the Name value of the provider "
950 {
"tbs", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
951 "Unsupported - Specifies the certificate or CRL file to be signed." },
955 {
"?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP,
nullptr,
nullptr,
nullptr, -1,
956 "help",
"print help" },
957 {
"!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP,
nullptr,
nullptr,
nullptr, -1,
958 "help-ext",
"print extended help" },
959 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
963 X509_NAME* name =
nullptr;
964 const EVP_MD* md =
nullptr;
966 int ret = makecert_context_parse_arguments(context, args, argc, argv);
973 if (!context->default_name && !context->common_name)
975 context->default_name = x509_get_default_name();
977 if (!context->default_name)
982 context->default_name = _strdup(context->common_name);
984 if (!context->default_name)
988 if (!context->common_name)
990 context->common_name = _strdup(context->default_name);
992 if (!context->common_name)
997 context->pkey = EVP_PKEY_new();
1003 context->x509 = X509_new();
1008 size_t key_length = 2048;
1009 arg = CommandLineFindArgumentA(args,
"len");
1011 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1013 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1015 if ((errno != 0) || (val > INT_MAX))
1020 if (!makecert_create_rsa(&context->pkey, key_length))
1023 if (X509_set_version(context->x509, 2) != 1)
1026 arg = CommandLineFindArgumentA(args,
"#");
1028 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1030 serial = strtol(arg->Value,
nullptr, 0);
1036 serial = (long)GetTickCount64();
1038 if (ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial) != 1)
1042 ASN1_TIME* before =
nullptr;
1043 ASN1_TIME* after =
nullptr;
1044#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1045 before = X509_get_notBefore(context->x509);
1046 after = X509_get_notAfter(context->x509);
1048 before = X509_getm_notBefore(context->x509);
1049 after = X509_getm_notAfter(context->x509);
1051 X509_gmtime_adj(before, 0);
1053 long duration = context->duration_months * 31l + context->duration_years * 365l;
1054 duration *= 60l * 60l * 24l;
1055 X509_gmtime_adj(after, duration);
1057 if (X509_set_pubkey(context->x509, context->pkey) != 1)
1060 name = X509_get_subject_name(context->x509);
1061 arg = CommandLineFindArgumentA(args,
"n");
1063 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1065 BOOL haveCN = FALSE;
1067 const char* records[] = {
"CN",
"ST",
"OU",
"L",
"O",
"C" };
1069 for (
size_t x = 0; x < ARRAYSIZE(records); x++)
1071 const char* record = records[x];
1072 const int rc = add_entry(name, arg->Value, record);
1081 const int rc = add_entry(name, context->common_name,
"CN");
1088 const int rc = add_entry(name, context->common_name,
"CN");
1093 if (X509_set_issuer_name(context->x509, name) != 1)
1096 if (x509_add_ext(context->x509, NID_ext_key_usage,
"serverAuth") != 1)
1099 arg = CommandLineFindArgumentA(args,
"a");
1102 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1104 md = EVP_get_digestbyname(arg->Value);
1109 if (!X509_sign(context->x509, context->pkey, md))
1116 if (!context->silent)
1118 BIO* bio = BIO_new(BIO_s_mem());
1123 const int status = X509_print(bio, context->x509);
1131 char* x509_str = makecert_read_str(bio,
nullptr);
1138 printf(
"%s", x509_str);
1149 if (!winpr_PathFileExists(context->output_path))
1151 if (!winpr_PathMakePath(context->output_path,
nullptr))
1155 if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1158 if (context->crtFormat)
1160 if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1167 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
1172MAKECERT_CONTEXT* makecert_context_new(
void)
1174 if (!utils_set_umask())
1177 MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1,
sizeof(MAKECERT_CONTEXT));
1181 context->crtFormat = TRUE;
1182 context->duration_years = 1;
1188void makecert_context_free(MAKECERT_CONTEXT* context)
1192 winpr_zfree(context->password);
1193 free(context->default_name);
1194 free(context->common_name);
1195 free(context->output_file);
1196 free(context->output_path);
1198 X509_free(context->x509);
1199 EVP_PKEY_free(context->pkey);
1200#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1201 CRYPTO_cleanup_all_ex_data();