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)
88 if (new_len > INT_MAX)
94 new_str = (
char*)realloc(x509_str, new_len);
105#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
106 status = BIO_read_ex(bio, &x509_str[offset], length - offset, &readBytes);
108 status = BIO_read(bio, &x509_str[offset], length - offset);
125 x509_str[offset] =
'\0';
127 *pOffset = offset + 1;
136 if (!argv || (argc < 1))
139 printf(
"Usage: %s [options] [output file]\n", argv[0]);
145 if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
148 printf(
"%-20s", arg->Name);
149 printf(
"\t%s\n", arg->Text);
151 else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
152 (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
158 size_t length = strlen(arg->Name) + strlen(arg->Format) + 2;
159 str = malloc(length + 1);
164 (void)sprintf_s(str, length + 1,
"%s %s", arg->Name, arg->Format);
165 (void)printf(
"%-20s", str);
170 printf(
"%-20s", arg->Name);
173 printf(
"\t%s\n", arg->Text);
175 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
181static int x509_add_ext(X509* cert,
int nid,
char* value)
184 X509_EXTENSION* ext = NULL;
189 X509V3_set_ctx_nodb(&ctx) X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
190 ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
195 X509_add_ext(cert, ext, -1);
196 X509_EXTENSION_free(ext);
201static char* x509_name_parse(
char* name,
char* txt,
size_t* length)
206 if (!name || !txt || !length)
209 p = strstr(name, txt);
214 entry = p + strlen(txt) + 1;
215 p = strchr(entry,
'=');
218 *length = strlen(entry);
220 *length = (size_t)(p - entry);
225static char* get_name(COMPUTER_NAME_FORMAT type)
229 if (GetComputerNameExA(type, NULL, &nSize))
232 if (GetLastError() != ERROR_MORE_DATA)
235 char* computerName = calloc(1, nSize);
240 if (!GetComputerNameExA(type, computerName, &nSize))
249static char* x509_get_default_name(
void)
251 char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
253 computerName = get_name(ComputerNamePhysicalNetBIOS);
257static int command_line_pre_filter(
void* pvctx,
int index,
int argc, LPSTR* argv)
259 MAKECERT_CONTEXT* context = pvctx;
260 if (!context || !argv || (index < 0) || (argc < 0))
263 if (index == (argc - 1))
265 if (argv[index][0] !=
'-')
267 context->output_file = _strdup(argv[index]);
269 if (!context->output_file)
279static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
286 if (!context || !argv || (argc < 0))
293 CommandLineClearArgumentsA(args);
294 flags = COMMAND_LINE_SEPARATOR_SPACE | COMMAND_LINE_SIGIL_DASH;
296 CommandLineParseArgumentsA(argc, argv, args, flags, context, command_line_pre_filter, NULL);
298 if (status & COMMAND_LINE_STATUS_PRINT_HELP)
300 makecert_print_command_line_help(args, argc, argv);
309 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
312 CommandLineSwitchStart(arg)
314 CommandLineSwitchCase(arg,
"silent")
316 context->silent = TRUE;
318 CommandLineSwitchCase(arg,
"live")
320 context->live = TRUE;
322 CommandLineSwitchCase(arg,
"format")
324 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
327 if (strcmp(arg->Value,
"crt") == 0)
329 context->crtFormat = TRUE;
330 context->pemFormat = FALSE;
331 context->pfxFormat = FALSE;
333 else if (strcmp(arg->Value,
"pem") == 0)
335 context->crtFormat = FALSE;
336 context->pemFormat = TRUE;
337 context->pfxFormat = FALSE;
339 else if (strcmp(arg->Value,
"pfx") == 0)
341 context->crtFormat = FALSE;
342 context->pemFormat = FALSE;
343 context->pfxFormat = TRUE;
348 CommandLineSwitchCase(arg,
"path")
350 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
353 context->output_path = _strdup(arg->Value);
355 if (!context->output_path)
358 CommandLineSwitchCase(arg,
"p")
360 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
363 context->password = _strdup(arg->Value);
365 if (!context->password)
368 CommandLineSwitchCase(arg,
"n")
370 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
373 context->common_name = _strdup(arg->Value);
375 if (!context->common_name)
378 CommandLineSwitchCase(arg,
"y")
382 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
385 val = strtol(arg->Value, NULL, 0);
387 if ((errno != 0) || (val < 0) || (val > INT32_MAX))
390 context->duration_years = (int)val;
392 CommandLineSwitchCase(arg,
"m")
396 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
399 val = strtol(arg->Value, NULL, 0);
401 if ((errno != 0) || (val < 0))
404 context->duration_months = (int)val;
406 CommandLineSwitchDefault(arg)
409 CommandLineSwitchEnd(arg)
410 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
415int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context,
const char* name)
420 free(context->output_file);
421 context->output_file = NULL;
424 context->output_file = _strdup(name);
426 if (!context->output_file)
432int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context,
const char* path)
439 char* filename = NULL;
440 char* fullpath = NULL;
444 char* x509_str = NULL;
449 if (!context->output_file)
451 context->output_file = _strdup(context->default_name);
453 if (!context->output_file)
460 length = strlen(context->output_file);
461 filename = malloc(length + 8);
466 if (context->crtFormat)
468 else if (context->pemFormat)
470 else if (context->pfxFormat)
475 (void)sprintf_s(filename, length + 8,
"%s.%s", context->output_file, ext);
478 fullpath = GetCombinedPath(path, filename);
480 fullpath = _strdup(filename);
485 fp = winpr_fopen(fullpath,
"w+");
489 if (context->pfxFormat)
491 if (!context->password)
493 context->password = _strdup(
"password");
495 if (!context->password)
498 printf(
"Using default export password \"password\"\n");
501#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
502 OpenSSL_add_all_algorithms();
503 OpenSSL_add_all_ciphers();
504 OpenSSL_add_all_digests();
506 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
507 OPENSSL_INIT_LOAD_CONFIG,
510 context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
511 context->x509, NULL, 0, 0, 0, 0, 0);
513 if (!context->pkcs12)
516 bio = BIO_new(BIO_s_mem());
521 status = i2d_PKCS12_bio(bio, context->pkcs12);
526 x509_str = makecert_read_str(bio, &offset);
533 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
538 bio = BIO_new(BIO_s_mem());
543 if (!PEM_write_bio_X509(bio, context->x509))
546 x509_str = makecert_read_str(bio, &offset);
553 if (fwrite(x509_str, length, 1, fp) != 1)
561 if (context->pemFormat)
563 bio = BIO_new(BIO_s_mem());
568 status = PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL);
573 x509_str = makecert_read_str(bio, &offset);
579 if (fwrite(x509_str, length, 1, fp) != 1)
597 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
602int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context,
const char* path)
608 char* filename = NULL;
609 char* fullpath = NULL;
612 char* x509_str = NULL;
614 if (!context->crtFormat)
617 if (!context->output_file)
619 context->output_file = _strdup(context->default_name);
621 if (!context->output_file)
628 length = strlen(context->output_file);
629 filename = malloc(length + 8);
634 (void)sprintf_s(filename, length + 8,
"%s.key", context->output_file);
637 fullpath = GetCombinedPath(path, filename);
639 fullpath = _strdup(filename);
644 fp = winpr_fopen(fullpath,
"w+");
649 bio = BIO_new(BIO_s_mem());
654 if (!PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL))
657 x509_str = makecert_read_str(bio, &offset);
664 if (fwrite((
void*)x509_str, length, 1, fp) != 1)
679 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
685static BOOL makecert_create_rsa(EVP_PKEY** ppkey,
size_t key_length)
691#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
693#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
694 rsa = RSA_generate_key(key_length, RSA_F4, NULL, NULL);
697 BIGNUM* bn = BN_secure_new();
710 BN_set_word(bn, RSA_F4);
711 const int res = RSA_generate_key_ex(rsa, key_length, bn, NULL);
719 if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
726 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(NULL,
"RSA", NULL);
730 if (EVP_PKEY_keygen_init(pctx) != 1)
734 WINPR_ASSERT(key_length <= UINT_MAX);
735 unsigned int keylen = (
unsigned int)key_length;
736 const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint(
"bits", &keylen),
737 OSSL_PARAM_construct_end() };
738 if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
742 if (EVP_PKEY_generate(pctx, ppkey) != 1)
747 EVP_PKEY_CTX_free(pctx);
753int makecert_context_process(MAKECERT_CONTEXT* context,
int argc,
char** argv)
758 {
"rdp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
759 "Unsupported - Generate certificate with required options for RDP usage." },
760 {
"silent", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
761 "Silently generate certificate without verbose output." },
762 {
"live", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
763 "Generate certificate live in memory when used as a library." },
764 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<crt|pem|pfx>", NULL, NULL, -1, NULL,
765 "Specify certificate file format" },
766 {
"path", COMMAND_LINE_VALUE_REQUIRED,
"<path>", NULL, NULL, -1, NULL,
767 "Specify certificate file output path" },
768 {
"p", COMMAND_LINE_VALUE_REQUIRED,
"<password>", NULL, NULL, -1, NULL,
769 "Specify certificate export password" },
773 {
"n", COMMAND_LINE_VALUE_REQUIRED,
"<name>", NULL, NULL, -1, NULL,
774 "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
775 "The simplest method is to specify the name in double quotes, preceded by CN=; for "
777 "-n \"CN=myName\"." },
778 {
"pe", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
779 "Unsupported - Marks the generated private key as exportable. This allows the private "
781 "be included in the certificate." },
782 {
"sk", COMMAND_LINE_VALUE_REQUIRED,
"<keyname>", NULL, NULL, -1, NULL,
783 "Unsupported - Specifies the subject's key container location, which contains the "
786 "If a key container does not exist, it will be created." },
787 {
"sr", COMMAND_LINE_VALUE_REQUIRED,
"<location>", NULL, NULL, -1, NULL,
788 "Unsupported - Specifies the subject's certificate store location. location can be "
790 "currentuser (the default) or localmachine." },
791 {
"ss", COMMAND_LINE_VALUE_REQUIRED,
"<store>", NULL, NULL, -1, NULL,
792 "Unsupported - Specifies the subject's certificate store name that stores the output "
794 {
"#", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
795 "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
797 "by Makecert.exe." },
798 {
"$", COMMAND_LINE_VALUE_REQUIRED,
"<authority>", NULL, NULL, -1, NULL,
799 "Unsupported - Specifies the signing authority of the certificate, which must be set to "
801 "(for certificates used by commercial software publishers) or individual (for "
803 "used by individual software publishers)." },
807 {
"a", COMMAND_LINE_VALUE_REQUIRED,
"<algorithm>", NULL, NULL, -1, NULL,
808 "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
809 "sha384, or sha512." },
810 {
"b", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>", NULL, NULL, -1, NULL,
811 "Unsupported - Specifies the start of the validity period. Defaults to the current "
813 {
"crl", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
814 "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
815 {
"cy", COMMAND_LINE_VALUE_REQUIRED,
"<certType>", NULL, NULL, -1, NULL,
816 "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
817 "authority for certification authority." },
818 {
"e", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>", NULL, NULL, -1, NULL,
819 "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
821 {
"eku", COMMAND_LINE_VALUE_REQUIRED,
"<oid[,oid…]>", NULL, NULL, -1, NULL,
822 "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
823 "(OIDs) into the certificate." },
824 {
"h", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
825 "Unsupported - Specifies the maximum height of the tree below this certificate." },
826 {
"ic", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
827 "Unsupported - Specifies the issuer's certificate file." },
828 {
"ik", COMMAND_LINE_VALUE_REQUIRED,
"<keyName>", NULL, NULL, -1, NULL,
829 "Unsupported - Specifies the issuer's key container name." },
830 {
"iky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>", NULL, NULL, -1, NULL,
831 "Unsupported - Specifies the issuer's key type, which must be one of the following: "
832 "signature (which indicates that the key is used for a digital signature), "
833 "exchange (which indicates that the key is used for key encryption and key exchange), "
834 "or an integer that represents a provider type. "
835 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
836 {
"in", COMMAND_LINE_VALUE_REQUIRED,
"<name>", NULL, NULL, -1, NULL,
837 "Unsupported - Specifies the issuer's certificate common name." },
838 {
"ip", COMMAND_LINE_VALUE_REQUIRED,
"<provider>", NULL, NULL, -1, NULL,
839 "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
840 "CryptoAPI provider name, see the –sp option." },
841 {
"ir", COMMAND_LINE_VALUE_REQUIRED,
"<location>", NULL, NULL, -1, NULL,
842 "Unsupported - Specifies the location of the issuer's certificate store. location can be "
843 "either currentuser (the default) or localmachine." },
844 {
"is", COMMAND_LINE_VALUE_REQUIRED,
"<store>", NULL, NULL, -1, NULL,
845 "Unsupported - Specifies the issuer's certificate store name." },
846 {
"iv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>", NULL, NULL, -1, NULL,
847 "Unsupported - Specifies the issuer's .pvk private key file." },
848 {
"iy", COMMAND_LINE_VALUE_REQUIRED,
"<type>", NULL, NULL, -1, NULL,
849 "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
850 "CryptoAPI provider type, see the –sy option." },
851 {
"l", COMMAND_LINE_VALUE_REQUIRED,
"<link>", NULL, NULL, -1, NULL,
852 "Unsupported - Links to policy information (for example, to a URL)." },
853 {
"len", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
854 "Specifies the generated key length, in bits." },
855 {
"m", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
856 "Specifies the duration, in months, of the certificate validity period." },
857 {
"y", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
858 "Specifies the duration, in years, of the certificate validity period." },
859 {
"nscp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
860 "Unsupported - Includes the Netscape client-authorization extension." },
861 {
"r", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
862 "Unsupported - Creates a self-signed certificate." },
863 {
"sc", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
864 "Unsupported - Specifies the subject's certificate file." },
865 {
"sky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>", NULL, NULL, -1, NULL,
866 "Unsupported - Specifies the subject's key type, which must be one of the following: "
867 "signature (which indicates that the key is used for a digital signature), "
868 "exchange (which indicates that the key is used for key encryption and key exchange), "
869 "or an integer that represents a provider type. "
870 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
871 {
"sp", COMMAND_LINE_VALUE_REQUIRED,
"<provider>", NULL, NULL, -1, NULL,
872 "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
874 "registry subkeys of "
875 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
878 "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
880 {
"sv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>", NULL, NULL, -1, NULL,
881 "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
884 {
"sy", COMMAND_LINE_VALUE_REQUIRED,
"<type>", NULL, NULL, -1, NULL,
885 "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
887 "registry subkeys of "
888 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
890 "–sy and –sp are present, "
891 "the name of the CryptoAPI provider must correspond to the Name value of the provider "
894 {
"tbs", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
895 "Unsupported - Specifies the certificate or CRL file to be signed." },
899 {
"?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1,
"help",
901 {
"!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1,
"help-ext",
902 "print extended help" },
903 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
910 X509_NAME* name = NULL;
911 const EVP_MD* md = NULL;
914 ret = makecert_context_parse_arguments(context, args, argc, argv);
921 if (!context->default_name && !context->common_name)
923 context->default_name = x509_get_default_name();
925 if (!context->default_name)
930 context->default_name = _strdup(context->common_name);
932 if (!context->default_name)
936 if (!context->common_name)
938 context->common_name = _strdup(context->default_name);
940 if (!context->common_name)
945 context->pkey = EVP_PKEY_new();
951 context->x509 = X509_new();
957 arg = CommandLineFindArgumentA(args,
"len");
959 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
961 unsigned long val = strtoul(arg->Value, NULL, 0);
963 if ((errno != 0) || (val > INT_MAX))
965 key_length = (int)val;
968 if (!makecert_create_rsa(&context->pkey, WINPR_ASSERTING_INT_CAST(
size_t, key_length)))
971 X509_set_version(context->x509, 2);
972 arg = CommandLineFindArgumentA(args,
"#");
974 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
976 serial = strtol(arg->Value, NULL, 0);
982 serial = (long)GetTickCount64();
984 ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial);
986 ASN1_TIME* before = NULL;
987 ASN1_TIME* after = NULL;
988#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
989 before = X509_get_notBefore(context->x509);
990 after = X509_get_notAfter(context->x509);
992 before = X509_getm_notBefore(context->x509);
993 after = X509_getm_notAfter(context->x509);
995 X509_gmtime_adj(before, 0);
997 long duration = context->duration_months * 31l + context->duration_years * 365l;
998 duration *= 60l * 60l * 24l;
999 X509_gmtime_adj(after, duration);
1001 X509_set_pubkey(context->x509, context->pkey);
1002 name = X509_get_subject_name(context->x509);
1003 arg = CommandLineFindArgumentA(args,
"n");
1005 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1007 entry = x509_name_parse(arg->Value,
"C", &length);
1010 X509_NAME_add_entry_by_txt(name,
"C", MBSTRING_UTF8, (
const unsigned char*)entry,
1011 (
int)length, -1, 0);
1013 entry = x509_name_parse(arg->Value,
"ST", &length);
1016 X509_NAME_add_entry_by_txt(name,
"ST", MBSTRING_UTF8, (
const unsigned char*)entry,
1017 (
int)length, -1, 0);
1019 entry = x509_name_parse(arg->Value,
"L", &length);
1022 X509_NAME_add_entry_by_txt(name,
"L", MBSTRING_UTF8, (
const unsigned char*)entry,
1023 (
int)length, -1, 0);
1025 entry = x509_name_parse(arg->Value,
"O", &length);
1028 X509_NAME_add_entry_by_txt(name,
"O", MBSTRING_UTF8, (
const unsigned char*)entry,
1029 (
int)length, -1, 0);
1031 entry = x509_name_parse(arg->Value,
"OU", &length);
1034 X509_NAME_add_entry_by_txt(name,
"OU", MBSTRING_UTF8, (
const unsigned char*)entry,
1035 (
int)length, -1, 0);
1037 entry = context->common_name;
1038 length = strlen(entry);
1039 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1040 (
int)length, -1, 0);
1044 entry = context->common_name;
1045 length = strlen(entry);
1046 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1047 (
int)length, -1, 0);
1050 X509_set_issuer_name(context->x509, name);
1051 x509_add_ext(context->x509, NID_ext_key_usage,
"serverAuth");
1052 arg = CommandLineFindArgumentA(args,
"a");
1055 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1057 md = EVP_get_digestbyname(arg->Value);
1062 if (!X509_sign(context->x509, context->pkey, md))
1069 if (!context->silent)
1073 char* x509_str = NULL;
1074 bio = BIO_new(BIO_s_mem());
1079 status = X509_print(bio, context->x509);
1087 x509_str = makecert_read_str(bio, NULL);
1094 printf(
"%s", x509_str);
1105 if (!winpr_PathFileExists(context->output_path))
1107 if (!winpr_PathMakePath(context->output_path, NULL))
1111 if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1114 if (context->crtFormat)
1116 if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1123 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
1128MAKECERT_CONTEXT* makecert_context_new(
void)
1130 MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1,
sizeof(MAKECERT_CONTEXT));
1134 context->crtFormat = TRUE;
1135 context->duration_years = 1;
1141void makecert_context_free(MAKECERT_CONTEXT* context)
1145 free(context->password);
1146 free(context->default_name);
1147 free(context->common_name);
1148 free(context->output_file);
1149 free(context->output_path);
1151 X509_free(context->x509);
1152 EVP_PKEY_free(context->pkey);
1153#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1154 CRYPTO_cleanup_all_ex_data();