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>
31#include <openssl/crypto.h>
32#include <openssl/conf.h>
33#include <openssl/pem.h>
34#include <openssl/err.h>
35#include <openssl/rsa.h>
36#include <openssl/pkcs12.h>
37#include <openssl/x509v3.h>
38#include <openssl/bn.h>
41#include <winpr/tools/makecert.h>
43struct S_MAKECERT_CONTEXT
72static char* makecert_read_str(BIO* bio,
size_t* pOffset)
77 char* x509_str = NULL;
79 while (offset >= length)
83 size_t new_len = length + 2048ull;
85 if (new_len > INT_MAX)
91 new_str = (
char*)realloc(x509_str, new_len);
102#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
103 status = BIO_read_ex(bio, &x509_str[offset], length - offset, &readBytes);
105 status = BIO_read(bio, &x509_str[offset], length - offset);
122 x509_str[offset] =
'\0';
124 *pOffset = offset + 1;
133 if (!argv || (argc < 1))
136 printf(
"Usage: %s [options] [output file]\n", argv[0]);
142 if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
145 printf(
"%-20s", arg->Name);
146 printf(
"\t%s\n", arg->Text);
148 else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
149 (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
155 size_t length = strlen(arg->Name) + strlen(arg->Format) + 2;
156 str = malloc(length + 1);
161 (void)sprintf_s(str, length + 1,
"%s %s", arg->Name, arg->Format);
162 (void)printf(
"%-20s", str);
167 printf(
"%-20s", arg->Name);
170 printf(
"\t%s\n", arg->Text);
172 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
178static int x509_add_ext(X509* cert,
int nid,
char* value)
181 X509_EXTENSION* ext = NULL;
186 X509V3_set_ctx_nodb(&ctx) X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
187 ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
192 X509_add_ext(cert, ext, -1);
193 X509_EXTENSION_free(ext);
198static char* x509_name_parse(
char* name,
char* txt,
size_t* length)
203 if (!name || !txt || !length)
206 p = strstr(name, txt);
211 entry = p + strlen(txt) + 1;
212 p = strchr(entry,
'=');
215 *length = strlen(entry);
217 *length = (size_t)(p - entry);
222static char* get_name(COMPUTER_NAME_FORMAT type)
226 if (GetComputerNameExA(type, NULL, &nSize))
229 if (GetLastError() != ERROR_MORE_DATA)
232 char* computerName = calloc(1, nSize);
237 if (!GetComputerNameExA(type, computerName, &nSize))
246static char* x509_get_default_name(
void)
248 char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
250 computerName = get_name(ComputerNamePhysicalNetBIOS);
254static int command_line_pre_filter(
void* pvctx,
int index,
int argc, LPSTR* argv)
256 MAKECERT_CONTEXT* context = pvctx;
257 if (!context || !argv || (index < 0) || (argc < 0))
260 if (index == (argc - 1))
262 if (argv[index][0] !=
'-')
264 context->output_file = _strdup(argv[index]);
266 if (!context->output_file)
276static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
283 if (!context || !argv || (argc < 0))
290 CommandLineClearArgumentsA(args);
291 flags = COMMAND_LINE_SEPARATOR_SPACE | COMMAND_LINE_SIGIL_DASH;
293 CommandLineParseArgumentsA(argc, argv, args, flags, context, command_line_pre_filter, NULL);
295 if (status & COMMAND_LINE_STATUS_PRINT_HELP)
297 makecert_print_command_line_help(args, argc, argv);
306 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
309 CommandLineSwitchStart(arg)
311 CommandLineSwitchCase(arg,
"silent")
313 context->silent = TRUE;
315 CommandLineSwitchCase(arg,
"live")
317 context->live = TRUE;
319 CommandLineSwitchCase(arg,
"format")
321 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
324 if (strcmp(arg->Value,
"crt") == 0)
326 context->crtFormat = TRUE;
327 context->pemFormat = FALSE;
328 context->pfxFormat = FALSE;
330 else if (strcmp(arg->Value,
"pem") == 0)
332 context->crtFormat = FALSE;
333 context->pemFormat = TRUE;
334 context->pfxFormat = FALSE;
336 else if (strcmp(arg->Value,
"pfx") == 0)
338 context->crtFormat = FALSE;
339 context->pemFormat = FALSE;
340 context->pfxFormat = TRUE;
345 CommandLineSwitchCase(arg,
"path")
347 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
350 context->output_path = _strdup(arg->Value);
352 if (!context->output_path)
355 CommandLineSwitchCase(arg,
"p")
357 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
360 context->password = _strdup(arg->Value);
362 if (!context->password)
365 CommandLineSwitchCase(arg,
"n")
367 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
370 context->common_name = _strdup(arg->Value);
372 if (!context->common_name)
375 CommandLineSwitchCase(arg,
"y")
379 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
382 val = strtol(arg->Value, NULL, 0);
384 if ((errno != 0) || (val < 0) || (val > INT32_MAX))
387 context->duration_years = (int)val;
389 CommandLineSwitchCase(arg,
"m")
393 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
396 val = strtol(arg->Value, NULL, 0);
398 if ((errno != 0) || (val < 0))
401 context->duration_months = (int)val;
403 CommandLineSwitchDefault(arg)
406 CommandLineSwitchEnd(arg)
407 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
412int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context,
const char* name)
417 free(context->output_file);
418 context->output_file = NULL;
421 context->output_file = _strdup(name);
423 if (!context->output_file)
429int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context,
const char* path)
436 char* filename = NULL;
437 char* fullpath = NULL;
441 char* x509_str = NULL;
446 if (!context->output_file)
448 context->output_file = _strdup(context->default_name);
450 if (!context->output_file)
457 length = strlen(context->output_file);
458 filename = malloc(length + 8);
463 if (context->crtFormat)
465 else if (context->pemFormat)
467 else if (context->pfxFormat)
472 (void)sprintf_s(filename, length + 8,
"%s.%s", context->output_file, ext);
475 fullpath = GetCombinedPath(path, filename);
477 fullpath = _strdup(filename);
482 fp = winpr_fopen(fullpath,
"w+");
486 if (context->pfxFormat)
488 if (!context->password)
490 context->password = _strdup(
"password");
492 if (!context->password)
495 printf(
"Using default export password \"password\"\n");
498#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
499 OpenSSL_add_all_algorithms();
500 OpenSSL_add_all_ciphers();
501 OpenSSL_add_all_digests();
503 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
504 OPENSSL_INIT_LOAD_CONFIG,
507 context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
508 context->x509, NULL, 0, 0, 0, 0, 0);
510 if (!context->pkcs12)
513 bio = BIO_new(BIO_s_mem());
518 status = i2d_PKCS12_bio(bio, context->pkcs12);
523 x509_str = makecert_read_str(bio, &offset);
530 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
535 bio = BIO_new(BIO_s_mem());
540 if (!PEM_write_bio_X509(bio, context->x509))
543 x509_str = makecert_read_str(bio, &offset);
550 if (fwrite(x509_str, length, 1, fp) != 1)
558 if (context->pemFormat)
560 bio = BIO_new(BIO_s_mem());
565 status = PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL);
570 x509_str = makecert_read_str(bio, &offset);
576 if (fwrite(x509_str, length, 1, fp) != 1)
594 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
599int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context,
const char* path)
605 char* filename = NULL;
606 char* fullpath = NULL;
609 char* x509_str = NULL;
611 if (!context->crtFormat)
614 if (!context->output_file)
616 context->output_file = _strdup(context->default_name);
618 if (!context->output_file)
625 length = strlen(context->output_file);
626 filename = malloc(length + 8);
631 (void)sprintf_s(filename, length + 8,
"%s.key", context->output_file);
634 fullpath = GetCombinedPath(path, filename);
636 fullpath = _strdup(filename);
641 fp = winpr_fopen(fullpath,
"w+");
646 bio = BIO_new(BIO_s_mem());
651 if (!PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL))
654 x509_str = makecert_read_str(bio, &offset);
661 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
676 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
682static BOOL makecert_create_rsa(EVP_PKEY** ppkey,
size_t key_length)
688#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
690#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
691 rsa = RSA_generate_key(key_length, RSA_F4, NULL, NULL);
694 BIGNUM* bn = BN_secure_new();
707 BN_set_word(bn, RSA_F4);
708 const int res = RSA_generate_key_ex(rsa, key_length, bn, NULL);
716 if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
723 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(NULL,
"RSA", NULL);
727 if (EVP_PKEY_keygen_init(pctx) != 1)
731 WINPR_ASSERT(key_length <= UINT_MAX);
732 unsigned int keylen = (
unsigned int)key_length;
733 const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint(
"bits", &keylen),
734 OSSL_PARAM_construct_end() };
735 if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
739 if (EVP_PKEY_generate(pctx, ppkey) != 1)
744 EVP_PKEY_CTX_free(pctx);
750int makecert_context_process(MAKECERT_CONTEXT* context,
int argc,
char** argv)
755 {
"rdp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
756 "Unsupported - Generate certificate with required options for RDP usage." },
757 {
"silent", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
758 "Silently generate certificate without verbose output." },
759 {
"live", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
760 "Generate certificate live in memory when used as a library." },
761 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<crt|pem|pfx>", NULL, NULL, -1, NULL,
762 "Specify certificate file format" },
763 {
"path", COMMAND_LINE_VALUE_REQUIRED,
"<path>", NULL, NULL, -1, NULL,
764 "Specify certificate file output path" },
765 {
"p", COMMAND_LINE_VALUE_REQUIRED,
"<password>", NULL, NULL, -1, NULL,
766 "Specify certificate export password" },
770 {
"n", COMMAND_LINE_VALUE_REQUIRED,
"<name>", NULL, NULL, -1, NULL,
771 "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
772 "The simplest method is to specify the name in double quotes, preceded by CN=; for "
774 "-n \"CN=myName\"." },
775 {
"pe", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
776 "Unsupported - Marks the generated private key as exportable. This allows the private "
778 "be included in the certificate." },
779 {
"sk", COMMAND_LINE_VALUE_REQUIRED,
"<keyname>", NULL, NULL, -1, NULL,
780 "Unsupported - Specifies the subject's key container location, which contains the "
783 "If a key container does not exist, it will be created." },
784 {
"sr", COMMAND_LINE_VALUE_REQUIRED,
"<location>", NULL, NULL, -1, NULL,
785 "Unsupported - Specifies the subject's certificate store location. location can be "
787 "currentuser (the default) or localmachine." },
788 {
"ss", COMMAND_LINE_VALUE_REQUIRED,
"<store>", NULL, NULL, -1, NULL,
789 "Unsupported - Specifies the subject's certificate store name that stores the output "
791 {
"#", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
792 "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
794 "by Makecert.exe." },
795 {
"$", COMMAND_LINE_VALUE_REQUIRED,
"<authority>", NULL, NULL, -1, NULL,
796 "Unsupported - Specifies the signing authority of the certificate, which must be set to "
798 "(for certificates used by commercial software publishers) or individual (for "
800 "used by individual software publishers)." },
804 {
"a", COMMAND_LINE_VALUE_REQUIRED,
"<algorithm>", NULL, NULL, -1, NULL,
805 "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
806 "sha384, or sha512." },
807 {
"b", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>", NULL, NULL, -1, NULL,
808 "Unsupported - Specifies the start of the validity period. Defaults to the current "
810 {
"crl", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
811 "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
812 {
"cy", COMMAND_LINE_VALUE_REQUIRED,
"<certType>", NULL, NULL, -1, NULL,
813 "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
814 "authority for certification authority." },
815 {
"e", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>", NULL, NULL, -1, NULL,
816 "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
818 {
"eku", COMMAND_LINE_VALUE_REQUIRED,
"<oid[,oid…]>", NULL, NULL, -1, NULL,
819 "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
820 "(OIDs) into the certificate." },
821 {
"h", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
822 "Unsupported - Specifies the maximum height of the tree below this certificate." },
823 {
"ic", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
824 "Unsupported - Specifies the issuer's certificate file." },
825 {
"ik", COMMAND_LINE_VALUE_REQUIRED,
"<keyName>", NULL, NULL, -1, NULL,
826 "Unsupported - Specifies the issuer's key container name." },
827 {
"iky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>", NULL, NULL, -1, NULL,
828 "Unsupported - Specifies the issuer's key type, which must be one of the following: "
829 "signature (which indicates that the key is used for a digital signature), "
830 "exchange (which indicates that the key is used for key encryption and key exchange), "
831 "or an integer that represents a provider type. "
832 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
833 {
"in", COMMAND_LINE_VALUE_REQUIRED,
"<name>", NULL, NULL, -1, NULL,
834 "Unsupported - Specifies the issuer's certificate common name." },
835 {
"ip", COMMAND_LINE_VALUE_REQUIRED,
"<provider>", NULL, NULL, -1, NULL,
836 "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
837 "CryptoAPI provider name, see the –sp option." },
838 {
"ir", COMMAND_LINE_VALUE_REQUIRED,
"<location>", NULL, NULL, -1, NULL,
839 "Unsupported - Specifies the location of the issuer's certificate store. location can be "
840 "either currentuser (the default) or localmachine." },
841 {
"is", COMMAND_LINE_VALUE_REQUIRED,
"<store>", NULL, NULL, -1, NULL,
842 "Unsupported - Specifies the issuer's certificate store name." },
843 {
"iv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>", NULL, NULL, -1, NULL,
844 "Unsupported - Specifies the issuer's .pvk private key file." },
845 {
"iy", COMMAND_LINE_VALUE_REQUIRED,
"<type>", NULL, NULL, -1, NULL,
846 "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
847 "CryptoAPI provider type, see the –sy option." },
848 {
"l", COMMAND_LINE_VALUE_REQUIRED,
"<link>", NULL, NULL, -1, NULL,
849 "Unsupported - Links to policy information (for example, to a URL)." },
850 {
"len", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
851 "Specifies the generated key length, in bits." },
852 {
"m", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
853 "Specifies the duration, in months, of the certificate validity period." },
854 {
"y", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
855 "Specifies the duration, in years, of the certificate validity period." },
856 {
"nscp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
857 "Unsupported - Includes the Netscape client-authorization extension." },
858 {
"r", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
859 "Unsupported - Creates a self-signed certificate." },
860 {
"sc", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
861 "Unsupported - Specifies the subject's certificate file." },
862 {
"sky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>", NULL, NULL, -1, NULL,
863 "Unsupported - Specifies the subject's key type, which must be one of the following: "
864 "signature (which indicates that the key is used for a digital signature), "
865 "exchange (which indicates that the key is used for key encryption and key exchange), "
866 "or an integer that represents a provider type. "
867 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
868 {
"sp", COMMAND_LINE_VALUE_REQUIRED,
"<provider>", NULL, NULL, -1, NULL,
869 "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
871 "registry subkeys of "
872 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
875 "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
877 {
"sv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>", NULL, NULL, -1, NULL,
878 "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
881 {
"sy", COMMAND_LINE_VALUE_REQUIRED,
"<type>", NULL, NULL, -1, NULL,
882 "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
884 "registry subkeys of "
885 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
887 "–sy and –sp are present, "
888 "the name of the CryptoAPI provider must correspond to the Name value of the provider "
891 {
"tbs", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
892 "Unsupported - Specifies the certificate or CRL file to be signed." },
896 {
"?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1,
"help",
898 {
"!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1,
"help-ext",
899 "print extended help" },
900 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
907 X509_NAME* name = NULL;
908 const EVP_MD* md = NULL;
911 ret = makecert_context_parse_arguments(context, args, argc, argv);
918 if (!context->default_name && !context->common_name)
920 context->default_name = x509_get_default_name();
922 if (!context->default_name)
927 context->default_name = _strdup(context->common_name);
929 if (!context->default_name)
933 if (!context->common_name)
935 context->common_name = _strdup(context->default_name);
937 if (!context->common_name)
942 context->pkey = EVP_PKEY_new();
948 context->x509 = X509_new();
954 arg = CommandLineFindArgumentA(args,
"len");
956 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
958 unsigned long val = strtoul(arg->Value, NULL, 0);
960 if ((errno != 0) || (val > INT_MAX))
962 key_length = (int)val;
965 if (!makecert_create_rsa(&context->pkey, WINPR_ASSERTING_INT_CAST(
size_t, key_length)))
968 X509_set_version(context->x509, 2);
969 arg = CommandLineFindArgumentA(args,
"#");
971 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
973 serial = strtol(arg->Value, NULL, 0);
979 serial = (long)GetTickCount64();
981 ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial);
983 ASN1_TIME* before = NULL;
984 ASN1_TIME* after = NULL;
985#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
986 before = X509_get_notBefore(context->x509);
987 after = X509_get_notAfter(context->x509);
989 before = X509_getm_notBefore(context->x509);
990 after = X509_getm_notAfter(context->x509);
992 X509_gmtime_adj(before, 0);
994 long duration = context->duration_months * 31l + context->duration_years * 365l;
995 duration *= 60l * 60l * 24l;
996 X509_gmtime_adj(after, duration);
998 X509_set_pubkey(context->x509, context->pkey);
999 name = X509_get_subject_name(context->x509);
1000 arg = CommandLineFindArgumentA(args,
"n");
1002 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1004 entry = x509_name_parse(arg->Value,
"C", &length);
1007 X509_NAME_add_entry_by_txt(name,
"C", MBSTRING_UTF8, (
const unsigned char*)entry,
1008 (
int)length, -1, 0);
1010 entry = x509_name_parse(arg->Value,
"ST", &length);
1013 X509_NAME_add_entry_by_txt(name,
"ST", MBSTRING_UTF8, (
const unsigned char*)entry,
1014 (
int)length, -1, 0);
1016 entry = x509_name_parse(arg->Value,
"L", &length);
1019 X509_NAME_add_entry_by_txt(name,
"L", MBSTRING_UTF8, (
const unsigned char*)entry,
1020 (
int)length, -1, 0);
1022 entry = x509_name_parse(arg->Value,
"O", &length);
1025 X509_NAME_add_entry_by_txt(name,
"O", MBSTRING_UTF8, (
const unsigned char*)entry,
1026 (
int)length, -1, 0);
1028 entry = x509_name_parse(arg->Value,
"OU", &length);
1031 X509_NAME_add_entry_by_txt(name,
"OU", MBSTRING_UTF8, (
const unsigned char*)entry,
1032 (
int)length, -1, 0);
1034 entry = context->common_name;
1035 length = strlen(entry);
1036 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1037 (
int)length, -1, 0);
1041 entry = context->common_name;
1042 length = strlen(entry);
1043 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1044 (
int)length, -1, 0);
1047 X509_set_issuer_name(context->x509, name);
1048 x509_add_ext(context->x509, NID_ext_key_usage,
"serverAuth");
1049 arg = CommandLineFindArgumentA(args,
"a");
1052 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1054 md = EVP_get_digestbyname(arg->Value);
1059 if (!X509_sign(context->x509, context->pkey, md))
1066 if (!context->silent)
1070 char* x509_str = NULL;
1071 bio = BIO_new(BIO_s_mem());
1076 status = X509_print(bio, context->x509);
1084 x509_str = makecert_read_str(bio, NULL);
1091 printf(
"%s", x509_str);
1102 if (!winpr_PathFileExists(context->output_path))
1104 if (!winpr_PathMakePath(context->output_path, NULL))
1108 if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1111 if (context->crtFormat)
1113 if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1120 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
1125MAKECERT_CONTEXT* makecert_context_new(
void)
1127 MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1,
sizeof(MAKECERT_CONTEXT));
1131 context->crtFormat = TRUE;
1132 context->duration_years = 1;
1138void makecert_context_free(MAKECERT_CONTEXT* context)
1142 free(context->password);
1143 free(context->default_name);
1144 free(context->common_name);
1145 free(context->output_file);
1146 free(context->output_path);
1148 X509_free(context->x509);
1149 EVP_PKEY_free(context->pkey);
1150#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1151 CRYPTO_cleanup_all_ex_data();