22 #include <winpr/assert.h>
23 #include <winpr/crt.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>
43 struct S_MAKECERT_CONTEXT
72 static 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);
181 static 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);
201 static 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);
225 static 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))
249 static char* x509_get_default_name(
void)
251 char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
253 computerName = get_name(ComputerNamePhysicalNetBIOS);
257 static 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)
279 static 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);
415 int 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)
432 int 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__);
602 int 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__);
685 static 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)
733 WINPR_ASSERT(key_length <= UINT_MAX);
734 unsigned int keylen = (
unsigned int)key_length;
735 const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint(
"bits", &keylen),
736 OSSL_PARAM_construct_end() };
737 if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
740 if (EVP_PKEY_generate(pctx, ppkey) != 1)
745 EVP_PKEY_CTX_free(pctx);
751 int makecert_context_process(MAKECERT_CONTEXT* context,
int argc,
char** argv)
756 {
"rdp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
757 "Unsupported - Generate certificate with required options for RDP usage." },
758 {
"silent", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
759 "Silently generate certificate without verbose output." },
760 {
"live", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
761 "Generate certificate live in memory when used as a library." },
762 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<crt|pem|pfx>", NULL, NULL, -1, NULL,
763 "Specify certificate file format" },
764 {
"path", COMMAND_LINE_VALUE_REQUIRED,
"<path>", NULL, NULL, -1, NULL,
765 "Specify certificate file output path" },
766 {
"p", COMMAND_LINE_VALUE_REQUIRED,
"<password>", NULL, NULL, -1, NULL,
767 "Specify certificate export password" },
771 {
"n", COMMAND_LINE_VALUE_REQUIRED,
"<name>", NULL, NULL, -1, NULL,
772 "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
773 "The simplest method is to specify the name in double quotes, preceded by CN=; for "
775 "-n \"CN=myName\"." },
776 {
"pe", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
777 "Unsupported - Marks the generated private key as exportable. This allows the private "
779 "be included in the certificate." },
780 {
"sk", COMMAND_LINE_VALUE_REQUIRED,
"<keyname>", NULL, NULL, -1, NULL,
781 "Unsupported - Specifies the subject's key container location, which contains the "
784 "If a key container does not exist, it will be created." },
785 {
"sr", COMMAND_LINE_VALUE_REQUIRED,
"<location>", NULL, NULL, -1, NULL,
786 "Unsupported - Specifies the subject's certificate store location. location can be "
788 "currentuser (the default) or localmachine." },
789 {
"ss", COMMAND_LINE_VALUE_REQUIRED,
"<store>", NULL, NULL, -1, NULL,
790 "Unsupported - Specifies the subject's certificate store name that stores the output "
792 {
"#", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
793 "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
795 "by Makecert.exe." },
796 {
"$", COMMAND_LINE_VALUE_REQUIRED,
"<authority>", NULL, NULL, -1, NULL,
797 "Unsupported - Specifies the signing authority of the certificate, which must be set to "
799 "(for certificates used by commercial software publishers) or individual (for "
801 "used by individual software publishers)." },
805 {
"a", COMMAND_LINE_VALUE_REQUIRED,
"<algorithm>", NULL, NULL, -1, NULL,
806 "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
807 "sha384, or sha512." },
808 {
"b", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>", NULL, NULL, -1, NULL,
809 "Unsupported - Specifies the start of the validity period. Defaults to the current "
811 {
"crl", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
812 "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
813 {
"cy", COMMAND_LINE_VALUE_REQUIRED,
"<certType>", NULL, NULL, -1, NULL,
814 "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
815 "authority for certification authority." },
816 {
"e", COMMAND_LINE_VALUE_REQUIRED,
"<mm/dd/yyyy>", NULL, NULL, -1, NULL,
817 "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
819 {
"eku", COMMAND_LINE_VALUE_REQUIRED,
"<oid[,oid…]>", NULL, NULL, -1, NULL,
820 "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
821 "(OIDs) into the certificate." },
822 {
"h", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
823 "Unsupported - Specifies the maximum height of the tree below this certificate." },
824 {
"ic", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
825 "Unsupported - Specifies the issuer's certificate file." },
826 {
"ik", COMMAND_LINE_VALUE_REQUIRED,
"<keyName>", NULL, NULL, -1, NULL,
827 "Unsupported - Specifies the issuer's key container name." },
828 {
"iky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>", NULL, NULL, -1, NULL,
829 "Unsupported - Specifies the issuer's key type, which must be one of the following: "
830 "signature (which indicates that the key is used for a digital signature), "
831 "exchange (which indicates that the key is used for key encryption and key exchange), "
832 "or an integer that represents a provider type. "
833 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
834 {
"in", COMMAND_LINE_VALUE_REQUIRED,
"<name>", NULL, NULL, -1, NULL,
835 "Unsupported - Specifies the issuer's certificate common name." },
836 {
"ip", COMMAND_LINE_VALUE_REQUIRED,
"<provider>", NULL, NULL, -1, NULL,
837 "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
838 "CryptoAPI provider name, see the –sp option." },
839 {
"ir", COMMAND_LINE_VALUE_REQUIRED,
"<location>", NULL, NULL, -1, NULL,
840 "Unsupported - Specifies the location of the issuer's certificate store. location can be "
841 "either currentuser (the default) or localmachine." },
842 {
"is", COMMAND_LINE_VALUE_REQUIRED,
"<store>", NULL, NULL, -1, NULL,
843 "Unsupported - Specifies the issuer's certificate store name." },
844 {
"iv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>", NULL, NULL, -1, NULL,
845 "Unsupported - Specifies the issuer's .pvk private key file." },
846 {
"iy", COMMAND_LINE_VALUE_REQUIRED,
"<type>", NULL, NULL, -1, NULL,
847 "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
848 "CryptoAPI provider type, see the –sy option." },
849 {
"l", COMMAND_LINE_VALUE_REQUIRED,
"<link>", NULL, NULL, -1, NULL,
850 "Unsupported - Links to policy information (for example, to a URL)." },
851 {
"len", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
852 "Specifies the generated key length, in bits." },
853 {
"m", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
854 "Specifies the duration, in months, of the certificate validity period." },
855 {
"y", COMMAND_LINE_VALUE_REQUIRED,
"<number>", NULL, NULL, -1, NULL,
856 "Specifies the duration, in years, of the certificate validity period." },
857 {
"nscp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
858 "Unsupported - Includes the Netscape client-authorization extension." },
859 {
"r", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
860 "Unsupported - Creates a self-signed certificate." },
861 {
"sc", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
862 "Unsupported - Specifies the subject's certificate file." },
863 {
"sky", COMMAND_LINE_VALUE_REQUIRED,
"<keyType>", NULL, NULL, -1, NULL,
864 "Unsupported - Specifies the subject's key type, which must be one of the following: "
865 "signature (which indicates that the key is used for a digital signature), "
866 "exchange (which indicates that the key is used for key encryption and key exchange), "
867 "or an integer that represents a provider type. "
868 "By default, you can pass 1 for an exchange key or 2 for a signature key." },
869 {
"sp", COMMAND_LINE_VALUE_REQUIRED,
"<provider>", NULL, NULL, -1, NULL,
870 "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
872 "registry subkeys of "
873 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
876 "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
878 {
"sv", COMMAND_LINE_VALUE_REQUIRED,
"<pvkFile>", NULL, NULL, -1, NULL,
879 "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
882 {
"sy", COMMAND_LINE_VALUE_REQUIRED,
"<type>", NULL, NULL, -1, NULL,
883 "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
885 "registry subkeys of "
886 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
888 "–sy and –sp are present, "
889 "the name of the CryptoAPI provider must correspond to the Name value of the provider "
892 {
"tbs", COMMAND_LINE_VALUE_REQUIRED,
"<file>", NULL, NULL, -1, NULL,
893 "Unsupported - Specifies the certificate or CRL file to be signed." },
897 {
"?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1,
"help",
899 {
"!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1,
"help-ext",
900 "print extended help" },
901 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
908 X509_NAME* name = NULL;
909 const EVP_MD* md = NULL;
912 ret = makecert_context_parse_arguments(context, args, argc, argv);
919 if (!context->default_name && !context->common_name)
921 context->default_name = x509_get_default_name();
923 if (!context->default_name)
928 context->default_name = _strdup(context->common_name);
930 if (!context->default_name)
934 if (!context->common_name)
936 context->common_name = _strdup(context->default_name);
938 if (!context->common_name)
943 context->pkey = EVP_PKEY_new();
949 context->x509 = X509_new();
955 arg = CommandLineFindArgumentA(args,
"len");
957 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
959 unsigned long val = strtoul(arg->Value, NULL, 0);
961 if ((errno != 0) || (val > INT_MAX))
963 key_length = (int)val;
966 if (!makecert_create_rsa(&context->pkey, WINPR_ASSERTING_INT_CAST(
size_t, key_length)))
969 X509_set_version(context->x509, 2);
970 arg = CommandLineFindArgumentA(args,
"#");
972 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
974 serial = strtol(arg->Value, NULL, 0);
980 serial = (long)GetTickCount64();
982 ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial);
984 ASN1_TIME* before = NULL;
985 ASN1_TIME* after = NULL;
986 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
987 before = X509_get_notBefore(context->x509);
988 after = X509_get_notAfter(context->x509);
990 before = X509_getm_notBefore(context->x509);
991 after = X509_getm_notAfter(context->x509);
993 X509_gmtime_adj(before, 0);
995 long duration = context->duration_months * 31l + context->duration_years * 365l;
996 duration *= 60l * 60l * 24l;
997 X509_gmtime_adj(after, duration);
999 X509_set_pubkey(context->x509, context->pkey);
1000 name = X509_get_subject_name(context->x509);
1001 arg = CommandLineFindArgumentA(args,
"n");
1003 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1005 entry = x509_name_parse(arg->Value,
"C", &length);
1008 X509_NAME_add_entry_by_txt(name,
"C", MBSTRING_UTF8, (
const unsigned char*)entry,
1009 (
int)length, -1, 0);
1011 entry = x509_name_parse(arg->Value,
"ST", &length);
1014 X509_NAME_add_entry_by_txt(name,
"ST", MBSTRING_UTF8, (
const unsigned char*)entry,
1015 (
int)length, -1, 0);
1017 entry = x509_name_parse(arg->Value,
"L", &length);
1020 X509_NAME_add_entry_by_txt(name,
"L", MBSTRING_UTF8, (
const unsigned char*)entry,
1021 (
int)length, -1, 0);
1023 entry = x509_name_parse(arg->Value,
"O", &length);
1026 X509_NAME_add_entry_by_txt(name,
"O", MBSTRING_UTF8, (
const unsigned char*)entry,
1027 (
int)length, -1, 0);
1029 entry = x509_name_parse(arg->Value,
"OU", &length);
1032 X509_NAME_add_entry_by_txt(name,
"OU", MBSTRING_UTF8, (
const unsigned char*)entry,
1033 (
int)length, -1, 0);
1035 entry = context->common_name;
1036 length = strlen(entry);
1037 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1038 (
int)length, -1, 0);
1042 entry = context->common_name;
1043 length = strlen(entry);
1044 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_UTF8, (
const unsigned char*)entry,
1045 (
int)length, -1, 0);
1048 X509_set_issuer_name(context->x509, name);
1049 x509_add_ext(context->x509, NID_ext_key_usage,
"serverAuth");
1050 arg = CommandLineFindArgumentA(args,
"a");
1053 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1055 md = EVP_get_digestbyname(arg->Value);
1060 if (!X509_sign(context->x509, context->pkey, md))
1067 if (!context->silent)
1071 char* x509_str = NULL;
1072 bio = BIO_new(BIO_s_mem());
1077 status = X509_print(bio, context->x509);
1085 x509_str = makecert_read_str(bio, NULL);
1092 printf(
"%s", x509_str);
1103 if (!winpr_PathFileExists(context->output_path))
1105 if (!CreateDirectoryA(context->output_path, NULL))
1109 if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1112 if (context->crtFormat)
1114 if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1121 WLog_ERR(TAG,
"%s only supported with OpenSSL", __func__);
1126 MAKECERT_CONTEXT* makecert_context_new(
void)
1128 MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1,
sizeof(MAKECERT_CONTEXT));
1132 context->crtFormat = TRUE;
1133 context->duration_years = 1;
1139 void makecert_context_free(MAKECERT_CONTEXT* context)
1143 free(context->password);
1144 free(context->default_name);
1145 free(context->common_name);
1146 free(context->output_file);
1147 free(context->output_path);
1149 X509_free(context->x509);
1150 EVP_PKEY_free(context->pkey);
1151 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1152 CRYPTO_cleanup_all_ex_data();