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>
30#include <winpr/wlog.h>
32#define TAG "com.winpr.tools.makecert"
35#include <openssl/crypto.h>
36#include <openssl/conf.h>
37#include <openssl/pem.h>
38#include <openssl/err.h>
39#include <openssl/rsa.h>
40#include <openssl/pkcs12.h>
41#include <openssl/x509v3.h>
42#include <openssl/bn.h>
45#include <winpr/tools/makecert.h>
51struct S_MAKECERT_CONTEXT
81static BOOL utils_set_umask(
void)
84 (void)umask(S_IRWXG | S_IRWXO);
89WINPR_ATTR_MALLOC(winpr_zfree, 1)
90static
char* makecert_read_str(BIO* bio,
size_t* pOffset)
95 char* x509_str =
nullptr;
97 while (offset >= length)
100 char* new_str =
nullptr;
101 size_t new_len = length + 2048ull;
103 if (new_len > INT_MAX)
109 new_str = (
char*)realloc(x509_str, new_len);
120#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
121 status = BIO_read_ex(bio, &x509_str[offset], length - offset, &readBytes);
123 status = BIO_read(bio, &x509_str[offset], length - offset);
140 x509_str[offset] =
'\0';
142 *pOffset = offset + 1;
151 if (!argv || (argc < 1))
154 printf(
"Usage: %s [options] [output file]\n", argv[0]);
160 if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
163 printf(
"%-20s", arg->Name);
164 printf(
"\t%s\n", arg->Text);
166 else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
167 (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
173 size_t length = strlen(arg->Name) + strlen(arg->Format) + 2;
174 str = malloc(length + 1);
179 (void)sprintf_s(str, length + 1,
"%s %s", arg->Name, arg->Format);
180 (void)printf(
"%-20s", str);
185 printf(
"%-20s", arg->Name);
188 printf(
"\t%s\n", arg->Text);
190 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
197static int x509_add_ext(X509* cert,
int nid,
char* value)
200 X509_EXTENSION* ext =
nullptr;
205 X509V3_set_ctx_nodb(&ctx) X509V3_set_ctx(&ctx, cert, cert,
nullptr,
nullptr, 0);
206 ext = X509V3_EXT_conf_nid(
nullptr, &ctx, nid, value);
211 X509_add_ext(cert, ext, -1);
212 X509_EXTENSION_free(ext);
218static const char* x509_name_parse(
const char* name,
const char* txt,
size_t* length)
220 if (!name || !txt || !length)
223 const char* entry =
nullptr;
226 (void)winpr_asprintf(&fmt, &fmtlen,
"%s=", txt);
230 const char* p = strstr(name, fmt);
236 const char* sep1 = strchr(entry,
';');
237 const char* sep2 = strchr(entry,
',');
238 const char* sep3 = strchr(entry,
' ');
239 if (!sep1 && !sep2 && !sep3)
240 *length = strlen(entry);
242 *length = WINPR_ASSERTING_INT_CAST(
size_t, sep1 - entry);
244 *length = WINPR_ASSERTING_INT_CAST(
size_t, sep2 - entry);
246 *length = WINPR_ASSERTING_INT_CAST(
size_t, sep3 - entry);
253WINPR_ATTR_MALLOC(free, 1)
254static
char* get_name(COMPUTER_NAME_FORMAT type)
258 if (GetComputerNameExA(type,
nullptr, &nSize))
261 if (GetLastError() != ERROR_MORE_DATA)
264 char* computerName = calloc(1, nSize);
269 if (!GetComputerNameExA(type, computerName, &nSize))
278WINPR_ATTR_MALLOC(free, 1)
279static
char* x509_get_default_name(
void)
281 char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
283 computerName = get_name(ComputerNamePhysicalNetBIOS);
288static int command_line_pre_filter(
void* pvctx,
int index,
int argc, LPSTR* argv)
290 MAKECERT_CONTEXT* context = pvctx;
291 if (!context || !argv || (index < 0) || (argc < 0))
294 if (index == (argc - 1))
296 if (argv[index][0] !=
'-')
298 context->output_file = _strdup(argv[index]);
300 if (!context->output_file)
311static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
318 if (!context || !argv || (argc < 0))
325 CommandLineClearArgumentsA(args);
326 flags = COMMAND_LINE_SEPARATOR_SPACE | COMMAND_LINE_SIGIL_DASH;
327 status = CommandLineParseArgumentsA(argc, argv, args, flags, context, command_line_pre_filter,
330 if (status & COMMAND_LINE_STATUS_PRINT_HELP)
332 makecert_print_command_line_help(args, argc, argv);
341 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
344 CommandLineSwitchStart(arg)
346 CommandLineSwitchCase(arg,
"silent")
348 context->silent = TRUE;
350 CommandLineSwitchCase(arg,
"live")
352 context->live = TRUE;
354 CommandLineSwitchCase(arg,
"format")
356 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
359 if (strcmp(arg->Value,
"crt") == 0)
361 context->crtFormat = TRUE;
362 context->pemFormat = FALSE;
363 context->pfxFormat = FALSE;
365 else if (strcmp(arg->Value,
"pem") == 0)
367 context->crtFormat = FALSE;
368 context->pemFormat = TRUE;
369 context->pfxFormat = FALSE;
371 else if (strcmp(arg->Value,
"pfx") == 0)
373 context->crtFormat = FALSE;
374 context->pemFormat = FALSE;
375 context->pfxFormat = TRUE;
380 CommandLineSwitchCase(arg,
"path")
382 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
385 context->output_path = _strdup(arg->Value);
387 if (!context->output_path)
390 CommandLineSwitchCase(arg,
"p")
392 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
395 context->password = _strdup(arg->Value);
397 if (!context->password)
400 CommandLineSwitchCase(arg,
"n")
402 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
405 context->common_name = _strdup(arg->Value);
407 if (!context->common_name)
410 CommandLineSwitchCase(arg,
"y")
414 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
417 val = strtol(arg->Value,
nullptr, 0);
419 if ((errno != 0) || (val < 0) || (val > INT32_MAX))
422 context->duration_years = (int)val;
424 CommandLineSwitchCase(arg,
"m")
428 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
431 val = strtol(arg->Value,
nullptr, 0);
433 if ((errno != 0) || (val < 0))
436 context->duration_months = (int)val;
438 CommandLineSwitchDefault(arg)
441 CommandLineSwitchEnd(arg)
442 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
447int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context,
const char* name)
452 free(context->output_file);
453 context->output_file =
nullptr;
456 context->output_file = _strdup(name);
458 if (!context->output_file)
464int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context,
const char* path)
470 char* fullpath =
nullptr;
474 char* x509_str =
nullptr;
479 if (!context->output_file)
481 context->output_file = _strdup(context->default_name);
483 if (!context->output_file)
490 size_t length = strlen(context->output_file);
491 char* filename = malloc(length + 8);
496 if (context->crtFormat)
498 else if (context->pemFormat)
500 else if (context->pfxFormat)
505 (void)sprintf_s(filename, length + 8,
"%s.%s", context->output_file, ext);
508 fullpath = GetCombinedPath(path, filename);
510 fullpath = _strdup(filename);
515 fp = winpr_fopen(fullpath,
"w+");
519 if (context->pfxFormat)
521 if (!context->password)
523 BYTE random[32] = WINPR_C_ARRAY_INIT;
524 if (winpr_RAND(random,
sizeof(random)) < 0)
527 context->password = winpr_BinToHexString(random,
sizeof(random), FALSE);
529 if (!context->password)
532 printf(
"Using random export password \"%s\"\n", context->password);
535#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
536 OpenSSL_add_all_algorithms();
537 OpenSSL_add_all_ciphers();
538 OpenSSL_add_all_digests();
540 if (OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
541 OPENSSL_INIT_LOAD_CONFIG,
545 context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
546 context->x509,
nullptr, 0, 0, 0, 0, 0);
548 if (!context->pkcs12)
551 bio = BIO_new(BIO_s_mem());
556 status = i2d_PKCS12_bio(bio, context->pkcs12);
561 x509_str = makecert_read_str(bio, &offset);
568 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
573 bio = BIO_new(BIO_s_mem());
578 if (!PEM_write_bio_X509(bio, context->x509))
581 x509_str = makecert_read_str(bio, &offset);
588 if (fwrite(x509_str, length, 1, fp) != 1)
596 if (context->pemFormat)
598 bio = BIO_new(BIO_s_mem());
603 status = PEM_write_bio_PrivateKey(bio, context->pkey,
nullptr,
nullptr, 0,
nullptr,
609 x509_str = makecert_read_str(bio, &offset);
615 if (fwrite(x509_str, length, 1, fp) != 1)
633 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
638int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context,
const char* path)
644 char* filename =
nullptr;
645 char* fullpath =
nullptr;
648 char* x509_str =
nullptr;
650 if (!context->crtFormat)
653 if (!context->output_file)
655 context->output_file = _strdup(context->default_name);
657 if (!context->output_file)
664 length = strlen(context->output_file);
665 filename = malloc(length + 8);
670 (void)sprintf_s(filename, length + 8,
"%s.key", context->output_file);
673 fullpath = GetCombinedPath(path, filename);
675 fullpath = _strdup(filename);
680 fp = winpr_fopen(fullpath,
"w+");
685 bio = BIO_new(BIO_s_mem());
690 if (!PEM_write_bio_PrivateKey(bio, context->pkey,
nullptr,
nullptr, 0,
nullptr,
nullptr))
693 x509_str = makecert_read_str(bio, &offset);
700 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
715 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
722static BOOL makecert_create_rsa(EVP_PKEY** ppkey,
size_t key_length)
728#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
730#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
731 rsa = RSA_generate_key(key_length, RSA_F4,
nullptr,
nullptr);
734 BIGNUM* bn = BN_secure_new();
747 BN_set_word(bn, RSA_F4);
748 const int res = RSA_generate_key_ex(rsa, key_length, bn,
nullptr);
756 if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
763 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(
nullptr,
"RSA",
nullptr);
767 if (EVP_PKEY_keygen_init(pctx) != 1)
771 WINPR_ASSERT(key_length <= UINT_MAX);
772 unsigned int keylen = (
unsigned int)key_length;
773 const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint(
"bits", &keylen),
774 OSSL_PARAM_construct_end() };
775 if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
779 if (EVP_PKEY_generate(pctx, ppkey) != 1)
784 EVP_PKEY_CTX_free(pctx);
790static int add_entry(
const X509_NAME* name,
const char* value,
const char* txt)
797 const char* entry = x509_name_parse(value, txt, &length);
800 if (length > INT32_MAX)
802 const int rc = X509_NAME_add_entry_by_txt(WINPR_CAST_CONST_PTR_AWAY(name, X509_NAME*), txt,
803 MBSTRING_UTF8, (
const unsigned char*)entry,
804 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0);
813#define MAKECERT_CN_MAX 64
819static int add_common_name(
const X509_NAME* name,
const char* value)
824 size_t length = strlen(value);
827 WLog_WARN(TAG,
"no name for the certificate: it gets no common name");
830 if ((length > MAKECERT_CN_MAX) && winpr_str_is_valid_hostname(value))
832 const char* dot = strchr(value,
'.');
833 if (dot && (dot > value))
834 length = WINPR_ASSERTING_INT_CAST(
size_t, dot - value);
835 WLog_WARN(TAG,
"the name %s is longer than a common name may be: %.*s instead", value,
836 WINPR_ASSERTING_INT_CAST(
int, length), value);
839 if (X509_NAME_add_entry_by_txt(WINPR_CAST_CONST_PTR_AWAY(name, X509_NAME*),
"CN", MBSTRING_UTF8,
840 (
const unsigned char*)value,
841 WINPR_ASSERTING_INT_CAST(
int, length), -1, 0) != 1)
846int makecert_context_process(MAKECERT_CONTEXT* context,
int argc,
char** argv)
851 {
"rdp", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
852 "Unsupported - Generate certificate with required options for RDP usage." },
853 {
"silent", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
854 "Silently generate certificate without verbose output." },
855 {
"live", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
856 "Generate certificate live in memory when used as a library." },
857 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<crt|pem|pfx>",
nullptr,
nullptr, -1,
nullptr,
858 "Specify certificate file format" },
859 {
"path", COMMAND_LINE_VALUE_REQUIRED,
"<path>",
nullptr,
nullptr, -1,
nullptr,
860 "Specify certificate file output path" },
861 {
"p", COMMAND_LINE_VALUE_REQUIRED,
"<password>",
nullptr,
nullptr, -1,
nullptr,
862 "Specify certificate export password" },
866 {
"n", COMMAND_LINE_VALUE_REQUIRED,
"<name>",
nullptr,
nullptr, -1,
nullptr,
867 "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
868 "The simplest method is to specify the name in double quotes, preceded by CN=; for "
870 "-n \"CN=myName\"." },
871 {
"pe", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
872 "Unsupported - Marks the generated private key as exportable. This allows the private "
874 "be included in the certificate." },
875 {
"sk", COMMAND_LINE_VALUE_REQUIRED,
"<keyname>",
nullptr,
nullptr, -1,
nullptr,
876 "Unsupported - Specifies the subject's key container location, which contains the "
879 "If a key container does not exist, it will be created." },
880 {
"sr", COMMAND_LINE_VALUE_REQUIRED,
"<location>",
nullptr,
nullptr, -1,
nullptr,
881 "Unsupported - Specifies the subject's certificate store location. location can be "
883 "currentuser (the default) or localmachine." },
884 {
"ss", COMMAND_LINE_VALUE_REQUIRED,
"<store>",
nullptr,
nullptr, -1,
nullptr,
885 "Unsupported - Specifies the subject's certificate store name that stores the output "
887 {
"#", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
888 "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
890 "by Makecert.exe." },
891 {
"$", COMMAND_LINE_VALUE_REQUIRED,
"<authority>",
nullptr,
nullptr, -1,
nullptr,
892 "Unsupported - Specifies the signing authority of the certificate, which must be set to "
894 "(for certificates used by commercial software publishers) or individual (for "
896 "used by individual software publishers)." },
900 {
"a", COMMAND_LINE_VALUE_REQUIRED,
"<algorithm>",
nullptr,
nullptr, -1,
nullptr,
901 "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
902 "sha384, or sha512." },
903 {
"b", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>",
nullptr,
nullptr, -1,
nullptr,
904 "Unsupported - Specifies the start of the validity period. Defaults to the current "
906 {
"crl", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
907 "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
908 {
"cy", COMMAND_LINE_VALUE_REQUIRED,
"<certType>",
nullptr,
nullptr, -1,
nullptr,
909 "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
910 "authority for certification authority." },
911 {
"e", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>",
nullptr,
nullptr, -1,
nullptr,
912 "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
914 {
"eku", COMMAND_LINE_VALUE_REQUIRED,
"<oid[,oid…]>",
nullptr,
nullptr, -1,
nullptr,
915 "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
916 "(OIDs) into the certificate." },
917 {
"h", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
918 "Unsupported - Specifies the maximum height of the tree below this certificate." },
919 {
"ic", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
920 "Unsupported - Specifies the issuer's certificate file." },
921 {
"ik", COMMAND_LINE_VALUE_REQUIRED,
"<keyName>",
nullptr,
nullptr, -1,
nullptr,
922 "Unsupported - Specifies the issuer's key container name." },
923 {
"iky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>",
nullptr,
nullptr, -1,
nullptr,
924 "Unsupported - Specifies the issuer's key type, which must be one of the following: "
925 "signature (which indicates that the key is used for a digital signature), "
926 "exchange (which indicates that the key is used for key encryption and key exchange), "
927 "or an integer that represents a provider type. "
928 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
929 {
"in", COMMAND_LINE_VALUE_REQUIRED,
"<name>",
nullptr,
nullptr, -1,
nullptr,
930 "Unsupported - Specifies the issuer's certificate common name." },
931 {
"ip", COMMAND_LINE_VALUE_REQUIRED,
"<provider>",
nullptr,
nullptr, -1,
nullptr,
932 "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
933 "CryptoAPI provider name, see the –sp option." },
934 {
"ir", COMMAND_LINE_VALUE_REQUIRED,
"<location>",
nullptr,
nullptr, -1,
nullptr,
935 "Unsupported - Specifies the location of the issuer's certificate store. location can be "
936 "either currentuser (the default) or localmachine." },
937 {
"is", COMMAND_LINE_VALUE_REQUIRED,
"<store>",
nullptr,
nullptr, -1,
nullptr,
938 "Unsupported - Specifies the issuer's certificate store name." },
939 {
"iv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>",
nullptr,
nullptr, -1,
nullptr,
940 "Unsupported - Specifies the issuer's .pvk private key file." },
941 {
"iy", COMMAND_LINE_VALUE_REQUIRED,
"<type>",
nullptr,
nullptr, -1,
nullptr,
942 "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
943 "CryptoAPI provider type, see the –sy option." },
944 {
"l", COMMAND_LINE_VALUE_REQUIRED,
"<link>",
nullptr,
nullptr, -1,
nullptr,
945 "Unsupported - Links to policy information (for example, to a URL)." },
946 {
"len", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
947 "Specifies the generated key length, in bits." },
948 {
"m", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
949 "Specifies the duration, in months, of the certificate validity period." },
950 {
"y", COMMAND_LINE_VALUE_REQUIRED,
"<number>",
nullptr,
nullptr, -1,
nullptr,
951 "Specifies the duration, in years, of the certificate validity period." },
952 {
"nscp", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
953 "Unsupported - Includes the Netscape client-authorization extension." },
954 {
"r", COMMAND_LINE_VALUE_FLAG,
nullptr,
nullptr,
nullptr, -1,
nullptr,
955 "Unsupported - Creates a self-signed certificate." },
956 {
"sc", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
957 "Unsupported - Specifies the subject's certificate file." },
958 {
"sky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>",
nullptr,
nullptr, -1,
nullptr,
959 "Unsupported - Specifies the subject's key type, which must be one of the following: "
960 "signature (which indicates that the key is used for a digital signature), "
961 "exchange (which indicates that the key is used for key encryption and key exchange), "
962 "or an integer that represents a provider type. "
963 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
964 {
"sp", COMMAND_LINE_VALUE_REQUIRED,
"<provider>",
nullptr,
nullptr, -1,
nullptr,
965 "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
967 "registry subkeys of "
968 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
971 "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
973 {
"sv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>",
nullptr,
nullptr, -1,
nullptr,
974 "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
977 {
"sy", COMMAND_LINE_VALUE_REQUIRED,
"<type>",
nullptr,
nullptr, -1,
nullptr,
978 "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
980 "registry subkeys of "
981 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
983 "–sy and –sp are present, "
984 "the name of the CryptoAPI provider must correspond to the Name value of the provider "
987 {
"tbs", COMMAND_LINE_VALUE_REQUIRED,
"<file>",
nullptr,
nullptr, -1,
nullptr,
988 "Unsupported - Specifies the certificate or CRL file to be signed." },
992 {
"?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP,
nullptr,
nullptr,
nullptr, -1,
993 "help",
"print help" },
994 {
"!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP,
nullptr,
nullptr,
nullptr, -1,
995 "help-ext",
"print extended help" },
996 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
1000 const EVP_MD* md =
nullptr;
1002 int ret = makecert_context_parse_arguments(context, args, argc, argv);
1009 if (!context->default_name && !context->common_name)
1011 context->default_name = x509_get_default_name();
1013 if (!context->default_name)
1018 context->default_name = _strdup(context->common_name);
1020 if (!context->default_name)
1024 if (!context->common_name)
1026 context->common_name = _strdup(context->default_name);
1028 if (!context->common_name)
1033 context->pkey = EVP_PKEY_new();
1039 context->x509 = X509_new();
1044 size_t key_length = 2048;
1045 arg = CommandLineFindArgumentA(args,
"len");
1047 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1049 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1051 if ((errno != 0) || (val > INT_MAX))
1056 if (!makecert_create_rsa(&context->pkey, key_length))
1059 if (X509_set_version(context->x509, 2) != 1)
1062 arg = CommandLineFindArgumentA(args,
"#");
1064 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1066 serial = strtol(arg->Value,
nullptr, 0);
1072 serial = (long)GetTickCount64();
1074 if (ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial) != 1)
1078 ASN1_TIME* before =
nullptr;
1079 ASN1_TIME* after =
nullptr;
1080#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1081 before = X509_get_notBefore(context->x509);
1082 after = X509_get_notAfter(context->x509);
1084 before = X509_getm_notBefore(context->x509);
1085 after = X509_getm_notAfter(context->x509);
1087 X509_gmtime_adj(before, 0);
1089 long duration = context->duration_months * 31l + context->duration_years * 365l;
1090 duration *= 60l * 60l * 24l;
1091 X509_gmtime_adj(after, duration);
1093 if (X509_set_pubkey(context->x509, context->pkey) != 1)
1096 const X509_NAME* name = X509_get_subject_name(context->x509);
1097 arg = CommandLineFindArgumentA(args,
"n");
1099 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1101 BOOL haveCN = FALSE;
1103 const char* records[] = {
"CN",
"ST",
"OU",
"L",
"O",
"C" };
1105 for (
size_t x = 0; x < ARRAYSIZE(records); x++)
1107 const char* record = records[x];
1108 const int rc = add_entry(name, arg->Value, record);
1117 const int rc = add_common_name(name, context->common_name);
1124 const int rc = add_common_name(name, context->common_name);
1129 if (X509_set_issuer_name(context->x509, name) != 1)
1132 if (x509_add_ext(context->x509, NID_ext_key_usage,
"serverAuth") != 1)
1135 arg = CommandLineFindArgumentA(args,
"a");
1138 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1140 md = EVP_get_digestbyname(arg->Value);
1145 if (!X509_sign(context->x509, context->pkey, md))
1152 if (!context->silent)
1154 BIO* bio = BIO_new(BIO_s_mem());
1159 const int status = X509_print(bio, context->x509);
1167 char* x509_str = makecert_read_str(bio,
nullptr);
1174 printf(
"%s", x509_str);
1185 if (!winpr_PathFileExists(context->output_path))
1187 if (!winpr_PathMakePath(context->output_path,
nullptr))
1191 if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1194 if (context->crtFormat)
1196 if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1203 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
1208MAKECERT_CONTEXT* makecert_context_new(
void)
1210 if (!utils_set_umask())
1213 MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1,
sizeof(MAKECERT_CONTEXT));
1217 context->crtFormat = TRUE;
1218 context->duration_years = 1;
1224void makecert_context_free(MAKECERT_CONTEXT* context)
1228 winpr_zfree(context->password);
1229 free(context->default_name);
1230 free(context->common_name);
1231 free(context->output_file);
1232 free(context->output_path);
1234 X509_free(context->x509);
1235 EVP_PKEY_free(context->pkey);
1236#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1237 CRYPTO_cleanup_all_ex_data();