FreeRDP
Loading...
Searching...
No Matches
makecert.c
1
19
20#include <errno.h>
21
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>
29#include <winpr/print.h>
30#include <winpr/wlog.h>
31
32#define TAG "com.winpr.tools.makecert"
33
34#ifdef WITH_OPENSSL
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>
43#endif
44
45#include <winpr/tools/makecert.h>
46
47#if !defined(_WIN32)
48#include <sys/stat.h>
49#endif
50
51struct S_MAKECERT_CONTEXT
52{
53 int argc;
54 char** argv;
55
56#ifdef WITH_OPENSSL
57 X509* x509;
58 EVP_PKEY* pkey;
59 PKCS12* pkcs12;
60#endif
61
62 BOOL live;
63 BOOL silent;
64
65 BOOL crtFormat;
66 BOOL pemFormat;
67 BOOL pfxFormat;
68
69 char* password;
70
71 char* output_file;
72 char* output_path;
73 char* default_name;
74 char* common_name;
75
76 int duration_years;
77 int duration_months;
78};
79
80WINPR_ATTR_NODISCARD
81static BOOL utils_set_umask(void)
82{
83#if !defined(_WIN32)
84 (void)umask(S_IRWXG | S_IRWXO);
85#endif
86 return TRUE;
87}
88
89WINPR_ATTR_MALLOC(winpr_zfree, 1)
90static char* makecert_read_str(BIO* bio, size_t* pOffset)
91{
92 int status = -1;
93 size_t offset = 0;
94 size_t length = 0;
95 char* x509_str = nullptr;
96
97 while (offset >= length)
98 {
99 size_t readBytes = 0;
100 char* new_str = nullptr;
101 size_t new_len = length + 2048ull;
102
103 if (new_len > INT_MAX)
104 {
105 status = -1;
106 break;
107 }
108
109 new_str = (char*)realloc(x509_str, new_len);
110
111 if (!new_str)
112 {
113 status = -1;
114 break;
115 }
116
117 length = new_len;
118 x509_str = new_str;
119 ERR_clear_error();
120#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
121 status = BIO_read_ex(bio, &x509_str[offset], length - offset, &readBytes);
122#else
123 status = BIO_read(bio, &x509_str[offset], length - offset);
124 readBytes = status;
125#endif
126 if (status <= 0)
127 break;
128
129 offset += readBytes;
130 }
131
132 if (status < 0)
133 {
134 free(x509_str);
135 if (pOffset)
136 *pOffset = 0;
137 return nullptr;
138 }
139
140 x509_str[offset] = '\0';
141 if (pOffset)
142 *pOffset = offset + 1;
143 return x509_str;
144}
145
146static int makecert_print_command_line_help(COMMAND_LINE_ARGUMENT_A* args, int argc, char** argv)
147{
148 char* str = nullptr;
149 const COMMAND_LINE_ARGUMENT_A* arg = nullptr;
150
151 if (!argv || (argc < 1))
152 return -1;
153
154 printf("Usage: %s [options] [output file]\n", argv[0]);
155 printf("\n");
156 arg = args;
157
158 do
159 {
160 if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
161 {
162 printf(" %s", "-");
163 printf("%-20s", arg->Name);
164 printf("\t%s\n", arg->Text);
165 }
166 else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
167 (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
168 {
169 printf(" %s", "-");
170
171 if (arg->Format)
172 {
173 size_t length = strlen(arg->Name) + strlen(arg->Format) + 2;
174 str = malloc(length + 1);
175
176 if (!str)
177 return -1;
178
179 (void)sprintf_s(str, length + 1, "%s %s", arg->Name, arg->Format);
180 (void)printf("%-20s", str);
181 free(str);
182 }
183 else
184 {
185 printf("%-20s", arg->Name);
186 }
187
188 printf("\t%s\n", arg->Text);
189 }
190 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
191
192 return 1;
193}
194
195#ifdef WITH_OPENSSL
196WINPR_ATTR_NODISCARD
197static int x509_add_ext(X509* cert, int nid, char* value)
198{
199 X509V3_CTX ctx;
200 X509_EXTENSION* ext = nullptr;
201
202 if (!cert || !value)
203 return 0;
204
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);
207
208 if (!ext)
209 return 0;
210
211 X509_add_ext(cert, ext, -1);
212 X509_EXTENSION_free(ext);
213 return 1;
214}
215#endif
216
217WINPR_ATTR_NODISCARD
218static const char* x509_name_parse(const char* name, const char* txt, size_t* length)
219{
220 if (!name || !txt || !length)
221 return nullptr;
222
223 const char* entry = nullptr;
224 char* fmt = nullptr;
225 size_t fmtlen = 0;
226 (void)winpr_asprintf(&fmt, &fmtlen, "%s=", txt);
227 if (!fmt)
228 goto fail;
229
230 const char* p = strstr(name, fmt);
231
232 if (!p)
233 goto fail;
234
235 entry = &p[fmtlen];
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);
241 else if (sep1)
242 *length = WINPR_ASSERTING_INT_CAST(size_t, sep1 - entry);
243 else if (sep2)
244 *length = WINPR_ASSERTING_INT_CAST(size_t, sep2 - entry);
245 else
246 *length = WINPR_ASSERTING_INT_CAST(size_t, sep3 - entry);
247
248fail:
249 free(fmt);
250 return entry;
251}
252
253WINPR_ATTR_MALLOC(free, 1)
254static char* get_name(COMPUTER_NAME_FORMAT type)
255{
256 DWORD nSize = 0;
257
258 if (GetComputerNameExA(type, nullptr, &nSize))
259 return nullptr;
260
261 if (GetLastError() != ERROR_MORE_DATA)
262 return nullptr;
263
264 char* computerName = calloc(1, nSize);
265
266 if (!computerName)
267 return nullptr;
268
269 if (!GetComputerNameExA(type, computerName, &nSize))
270 {
271 free(computerName);
272 return nullptr;
273 }
274
275 return computerName;
276}
277
278WINPR_ATTR_MALLOC(free, 1)
279static char* x509_get_default_name(void)
280{
281 char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
282 if (!computerName)
283 computerName = get_name(ComputerNamePhysicalNetBIOS);
284 return computerName;
285}
286
287WINPR_ATTR_NODISCARD
288static int command_line_pre_filter(void* pvctx, int index, int argc, LPSTR* argv)
289{
290 MAKECERT_CONTEXT* context = pvctx;
291 if (!context || !argv || (index < 0) || (argc < 0))
292 return -1;
293
294 if (index == (argc - 1))
295 {
296 if (argv[index][0] != '-')
297 {
298 context->output_file = _strdup(argv[index]);
299
300 if (!context->output_file)
301 return -1;
302
303 return 1;
304 }
305 }
306
307 return 0;
308}
309
310WINPR_ATTR_NODISCARD
311static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
312 COMMAND_LINE_ARGUMENT_A* args, int argc, char** argv)
313{
314 int status = 0;
315 DWORD flags = 0;
316 const COMMAND_LINE_ARGUMENT_A* arg = nullptr;
317
318 if (!context || !argv || (argc < 0))
319 return -1;
320
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,
328 nullptr);
329
330 if (status & COMMAND_LINE_STATUS_PRINT_HELP)
331 {
332 makecert_print_command_line_help(args, argc, argv);
333 return 0;
334 }
335
336 arg = args;
337 errno = 0;
338
339 do
340 {
341 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
342 continue;
343
344 CommandLineSwitchStart(arg)
345 /* Basic Options */
346 CommandLineSwitchCase(arg, "silent")
347 {
348 context->silent = TRUE;
349 }
350 CommandLineSwitchCase(arg, "live")
351 {
352 context->live = TRUE;
353 }
354 CommandLineSwitchCase(arg, "format")
355 {
356 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
357 continue;
358
359 if (strcmp(arg->Value, "crt") == 0)
360 {
361 context->crtFormat = TRUE;
362 context->pemFormat = FALSE;
363 context->pfxFormat = FALSE;
364 }
365 else if (strcmp(arg->Value, "pem") == 0)
366 {
367 context->crtFormat = FALSE;
368 context->pemFormat = TRUE;
369 context->pfxFormat = FALSE;
370 }
371 else if (strcmp(arg->Value, "pfx") == 0)
372 {
373 context->crtFormat = FALSE;
374 context->pemFormat = FALSE;
375 context->pfxFormat = TRUE;
376 }
377 else
378 return -1;
379 }
380 CommandLineSwitchCase(arg, "path")
381 {
382 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
383 continue;
384
385 context->output_path = _strdup(arg->Value);
386
387 if (!context->output_path)
388 return -1;
389 }
390 CommandLineSwitchCase(arg, "p")
391 {
392 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
393 continue;
394
395 context->password = _strdup(arg->Value);
396
397 if (!context->password)
398 return -1;
399 }
400 CommandLineSwitchCase(arg, "n")
401 {
402 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
403 continue;
404
405 context->common_name = _strdup(arg->Value);
406
407 if (!context->common_name)
408 return -1;
409 }
410 CommandLineSwitchCase(arg, "y")
411 {
412 long val = 0;
413
414 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
415 continue;
416
417 val = strtol(arg->Value, nullptr, 0);
418
419 if ((errno != 0) || (val < 0) || (val > INT32_MAX))
420 return -1;
421
422 context->duration_years = (int)val;
423 }
424 CommandLineSwitchCase(arg, "m")
425 {
426 long val = 0;
427
428 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
429 continue;
430
431 val = strtol(arg->Value, nullptr, 0);
432
433 if ((errno != 0) || (val < 0))
434 return -1;
435
436 context->duration_months = (int)val;
437 }
438 CommandLineSwitchDefault(arg)
439 {
440 }
441 CommandLineSwitchEnd(arg)
442 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
443
444 return 1;
445}
446
447int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context, const char* name)
448{
449 if (!context)
450 return -1;
451
452 free(context->output_file);
453 context->output_file = nullptr;
454
455 if (name)
456 context->output_file = _strdup(name);
457
458 if (!context->output_file)
459 return -1;
460
461 return 1;
462}
463
464int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, const char* path)
465{
466#ifdef WITH_OPENSSL
467 FILE* fp = nullptr;
468 int status = 0;
469 size_t offset = 0;
470 char* fullpath = nullptr;
471 char* ext = nullptr;
472 int ret = -1;
473 BIO* bio = nullptr;
474 char* x509_str = nullptr;
475
476 if (!context)
477 return -1;
478
479 if (!context->output_file)
480 {
481 context->output_file = _strdup(context->default_name);
482
483 if (!context->output_file)
484 return -1;
485 }
486
487 /*
488 * Output Certificate File
489 */
490 size_t length = strlen(context->output_file);
491 char* filename = malloc(length + 8);
492
493 if (!filename)
494 return -1;
495
496 if (context->crtFormat)
497 ext = "crt";
498 else if (context->pemFormat)
499 ext = "pem";
500 else if (context->pfxFormat)
501 ext = "pfx";
502 else
503 goto out_fail;
504
505 (void)sprintf_s(filename, length + 8, "%s.%s", context->output_file, ext);
506
507 if (path)
508 fullpath = GetCombinedPath(path, filename);
509 else
510 fullpath = _strdup(filename);
511
512 if (!fullpath)
513 goto out_fail;
514
515 fp = winpr_fopen(fullpath, "w+");
516
517 if (fp)
518 {
519 if (context->pfxFormat)
520 {
521 if (!context->password)
522 {
523 BYTE random[32] = WINPR_C_ARRAY_INIT;
524 if (winpr_RAND(random, sizeof(random)) < 0)
525 goto out_fail;
526
527 context->password = winpr_BinToHexString(random, sizeof(random), FALSE);
528
529 if (!context->password)
530 goto out_fail;
531
532 printf("Using random export password \"%s\"\n", context->password);
533 }
534
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();
539#else
540 if (OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
541 OPENSSL_INIT_LOAD_CONFIG,
542 nullptr) != 1)
543 goto out_fail;
544#endif
545 context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
546 context->x509, nullptr, 0, 0, 0, 0, 0);
547
548 if (!context->pkcs12)
549 goto out_fail;
550
551 bio = BIO_new(BIO_s_mem());
552
553 if (!bio)
554 goto out_fail;
555
556 status = i2d_PKCS12_bio(bio, context->pkcs12);
557
558 if (status != 1)
559 goto out_fail;
560
561 x509_str = makecert_read_str(bio, &offset);
562
563 if (!x509_str)
564 goto out_fail;
565
566 length = offset;
567
568 if (fwrite((void*)x509_str, length, 1, fp) != 1)
569 goto out_fail;
570 }
571 else
572 {
573 bio = BIO_new(BIO_s_mem());
574
575 if (!bio)
576 goto out_fail;
577
578 if (!PEM_write_bio_X509(bio, context->x509))
579 goto out_fail;
580
581 x509_str = makecert_read_str(bio, &offset);
582
583 if (!x509_str)
584 goto out_fail;
585
586 length = offset;
587
588 if (fwrite(x509_str, length, 1, fp) != 1)
589 goto out_fail;
590
591 free(x509_str);
592 x509_str = nullptr;
593 BIO_free_all(bio);
594 bio = nullptr;
595
596 if (context->pemFormat)
597 {
598 bio = BIO_new(BIO_s_mem());
599
600 if (!bio)
601 goto out_fail;
602
603 status = PEM_write_bio_PrivateKey(bio, context->pkey, nullptr, nullptr, 0, nullptr,
604 nullptr);
605
606 if (status < 0)
607 goto out_fail;
608
609 x509_str = makecert_read_str(bio, &offset);
610 if (!x509_str)
611 goto out_fail;
612
613 length = offset;
614
615 if (fwrite(x509_str, length, 1, fp) != 1)
616 goto out_fail;
617 }
618 }
619 }
620
621 ret = 1;
622out_fail:
623 BIO_free_all(bio);
624
625 if (fp)
626 (void)fclose(fp);
627
628 free(x509_str);
629 free(filename);
630 free(fullpath);
631 return ret;
632#else
633 WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
634 return -1;
635#endif
636}
637
638int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, const char* path)
639{
640#ifdef WITH_OPENSSL
641 FILE* fp = nullptr;
642 size_t length = 0;
643 size_t offset = 0;
644 char* filename = nullptr;
645 char* fullpath = nullptr;
646 int ret = -1;
647 BIO* bio = nullptr;
648 char* x509_str = nullptr;
649
650 if (!context->crtFormat)
651 return 1;
652
653 if (!context->output_file)
654 {
655 context->output_file = _strdup(context->default_name);
656
657 if (!context->output_file)
658 return -1;
659 }
660
664 length = strlen(context->output_file);
665 filename = malloc(length + 8);
666
667 if (!filename)
668 return -1;
669
670 (void)sprintf_s(filename, length + 8, "%s.key", context->output_file);
671
672 if (path)
673 fullpath = GetCombinedPath(path, filename);
674 else
675 fullpath = _strdup(filename);
676
677 if (!fullpath)
678 goto out_fail;
679
680 fp = winpr_fopen(fullpath, "w+");
681
682 if (!fp)
683 goto out_fail;
684
685 bio = BIO_new(BIO_s_mem());
686
687 if (!bio)
688 goto out_fail;
689
690 if (!PEM_write_bio_PrivateKey(bio, context->pkey, nullptr, nullptr, 0, nullptr, nullptr))
691 goto out_fail;
692
693 x509_str = makecert_read_str(bio, &offset);
694
695 if (!x509_str)
696 goto out_fail;
697
698 length = offset;
699
700 if (fwrite((void*)x509_str, length, 1, fp) != 1)
701 goto out_fail;
702
703 ret = 1;
704out_fail:
705
706 if (fp)
707 (void)fclose(fp);
708
709 BIO_free_all(bio);
710 free(x509_str);
711 free(filename);
712 free(fullpath);
713 return ret;
714#else
715 WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
716 return -1;
717#endif
718}
719
720#ifdef WITH_OPENSSL
721WINPR_ATTR_NODISCARD
722static BOOL makecert_create_rsa(EVP_PKEY** ppkey, size_t key_length)
723{
724 BOOL rc = FALSE;
725
726 WINPR_ASSERT(ppkey);
727
728#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
729 RSA* rsa = nullptr;
730#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
731 rsa = RSA_generate_key(key_length, RSA_F4, nullptr, nullptr);
732#else
733 {
734 BIGNUM* bn = BN_secure_new();
735
736 if (!bn)
737 return FALSE;
738
739 rsa = RSA_new();
740
741 if (!rsa)
742 {
743 BN_clear_free(bn);
744 return FALSE;
745 }
746
747 BN_set_word(bn, RSA_F4);
748 const int res = RSA_generate_key_ex(rsa, key_length, bn, nullptr);
749 BN_clear_free(bn);
750
751 if (res != 1)
752 return FALSE;
753 }
754#endif
755
756 if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
757 {
758 RSA_free(rsa);
759 return FALSE;
760 }
761 rc = TRUE;
762#else
763 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(nullptr, "RSA", nullptr);
764 if (!pctx)
765 return FALSE;
766
767 if (EVP_PKEY_keygen_init(pctx) != 1)
768 goto fail;
769
770 {
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)
776 goto fail;
777 }
778
779 if (EVP_PKEY_generate(pctx, ppkey) != 1)
780 goto fail;
781
782 rc = TRUE;
783fail:
784 EVP_PKEY_CTX_free(pctx);
785#endif
786 return rc;
787}
788#endif
789
790static int add_entry(const X509_NAME* name, const char* value, const char* txt)
791{
792 WINPR_ASSERT(name);
793 WINPR_ASSERT(value);
794 WINPR_ASSERT(txt);
795
796 size_t length = 0;
797 const char* entry = x509_name_parse(value, txt, &length);
798 if (entry)
799 {
800 if (length > INT32_MAX)
801 return -1;
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);
805 if (rc != 1)
806 return -1;
807 return 1;
808 }
809 return 0;
810}
811
812/* A common name holds at most 64 characters (X.520 ub-common-name): OpenSSL refuses a longer one */
813#define MAKECERT_CN_MAX 64
814
815/* The common name as it is (the host name, or -n without CN=), not a list of records. A longer
816 * name, such as the fully qualified name of a cloud machine, keeps its first label (the host
817 * name). A first label longer than a label may be is no host name: an error. */
818WINPR_ATTR_NODISCARD
819static int add_common_name(const X509_NAME* name, const char* value)
820{
821 WINPR_ASSERT(name);
822 WINPR_ASSERT(value);
823
824 size_t length = strlen(value);
825 if (length == 0)
826 {
827 WLog_WARN(TAG, "no name for the certificate: it gets no common name");
828 return 0;
829 }
830 if ((length > MAKECERT_CN_MAX) && winpr_str_is_valid_hostname(value))
831 {
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);
837 }
838
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)
842 return -1;
843 return 1;
844}
845
846int makecert_context_process(MAKECERT_CONTEXT* context, int argc, char** argv)
847{
848 COMMAND_LINE_ARGUMENT_A args[] = {
849 /* Custom Options */
850
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" },
863
864 /* Basic Options */
865
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 "
869 "example, "
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 "
873 "key to "
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 "
877 "private "
878 "key. "
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 "
882 "either "
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 "
886 "certificate." },
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 "
889 "generated "
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 "
893 "either commercial "
894 "(for certificates used by commercial software publishers) or individual (for "
895 "certificates "
896 "used by individual software publishers)." },
897
898 /* Extended Options */
899
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 "
905 "date." },
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 "
913 "GMT." },
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 "
966 "the "
967 "registry subkeys of "
968 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
969 "and "
970 "–sy are present, "
971 "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
972 "subkey." },
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 "
975 "none "
976 "exists." },
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 "
979 "the "
980 "registry subkeys of "
981 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
982 "both "
983 "–sy and –sp are present, "
984 "the name of the CryptoAPI provider must correspond to the Name value of the provider "
985 "type "
986 "subkey." },
987 { "tbs", COMMAND_LINE_VALUE_REQUIRED, "<file>", nullptr, nullptr, -1, nullptr,
988 "Unsupported - Specifies the certificate or CRL file to be signed." },
989
990 /* Help */
991
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 }
997 };
998#ifdef WITH_OPENSSL
999 long serial = 0;
1000 const EVP_MD* md = nullptr;
1001 const COMMAND_LINE_ARGUMENT_A* arg = nullptr;
1002 int ret = makecert_context_parse_arguments(context, args, argc, argv);
1003
1004 if (ret < 1)
1005 {
1006 return ret;
1007 }
1008
1009 if (!context->default_name && !context->common_name)
1010 {
1011 context->default_name = x509_get_default_name();
1012
1013 if (!context->default_name)
1014 return -1;
1015 }
1016 else
1017 {
1018 context->default_name = _strdup(context->common_name);
1019
1020 if (!context->default_name)
1021 return -1;
1022 }
1023
1024 if (!context->common_name)
1025 {
1026 context->common_name = _strdup(context->default_name);
1027
1028 if (!context->common_name)
1029 return -1;
1030 }
1031
1032 if (!context->pkey)
1033 context->pkey = EVP_PKEY_new();
1034
1035 if (!context->pkey)
1036 return -1;
1037
1038 if (!context->x509)
1039 context->x509 = X509_new();
1040
1041 if (!context->x509)
1042 return -1;
1043
1044 size_t key_length = 2048;
1045 arg = CommandLineFindArgumentA(args, "len");
1046
1047 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1048 {
1049 unsigned long val = strtoul(arg->Value, nullptr, 0);
1050
1051 if ((errno != 0) || (val > INT_MAX))
1052 return -1;
1053 key_length = val;
1054 }
1055
1056 if (!makecert_create_rsa(&context->pkey, key_length))
1057 return -1;
1058
1059 if (X509_set_version(context->x509, 2) != 1)
1060 return -1;
1061
1062 arg = CommandLineFindArgumentA(args, "#");
1063
1064 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1065 {
1066 serial = strtol(arg->Value, nullptr, 0);
1067
1068 if (errno != 0)
1069 return -1;
1070 }
1071 else
1072 serial = (long)GetTickCount64();
1073
1074 if (ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial) != 1)
1075 return -1;
1076
1077 {
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);
1083#else
1084 before = X509_getm_notBefore(context->x509);
1085 after = X509_getm_notAfter(context->x509);
1086#endif
1087 X509_gmtime_adj(before, 0);
1088
1089 long duration = context->duration_months * 31l + context->duration_years * 365l;
1090 duration *= 60l * 60l * 24l;
1091 X509_gmtime_adj(after, duration);
1092 }
1093 if (X509_set_pubkey(context->x509, context->pkey) != 1)
1094 return -1;
1095
1096 const X509_NAME* name = X509_get_subject_name(context->x509);
1097 arg = CommandLineFindArgumentA(args, "n");
1098
1099 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1100 {
1101 BOOL haveCN = FALSE;
1102
1103 const char* records[] = { "CN", "ST", "OU", "L", "O", "C" };
1104
1105 for (size_t x = 0; x < ARRAYSIZE(records); x++)
1106 {
1107 const char* record = records[x];
1108 const int rc = add_entry(name, arg->Value, record);
1109 if (rc < 0)
1110 return rc;
1111 if (x == 0)
1112 haveCN = rc > 0;
1113 }
1114
1115 if (!haveCN)
1116 {
1117 const int rc = add_common_name(name, context->common_name);
1118 if (rc < 0)
1119 return rc;
1120 }
1121 }
1122 else
1123 {
1124 const int rc = add_common_name(name, context->common_name);
1125 if (rc < 0)
1126 return rc;
1127 }
1128
1129 if (X509_set_issuer_name(context->x509, name) != 1)
1130 return -1;
1131
1132 if (x509_add_ext(context->x509, NID_ext_key_usage, "serverAuth") != 1)
1133 return -1;
1134
1135 arg = CommandLineFindArgumentA(args, "a");
1136 md = EVP_sha256();
1137
1138 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1139 {
1140 md = EVP_get_digestbyname(arg->Value);
1141 if (!md)
1142 return -1;
1143 }
1144
1145 if (!X509_sign(context->x509, context->pkey, md))
1146 return -1;
1147
1151
1152 if (!context->silent)
1153 {
1154 BIO* bio = BIO_new(BIO_s_mem());
1155
1156 if (!bio)
1157 return -1;
1158
1159 const int status = X509_print(bio, context->x509);
1160
1161 if (status < 0)
1162 {
1163 BIO_free_all(bio);
1164 return -1;
1165 }
1166
1167 char* x509_str = makecert_read_str(bio, nullptr);
1168 if (!x509_str)
1169 {
1170 BIO_free_all(bio);
1171 return -1;
1172 }
1173
1174 printf("%s", x509_str);
1175 free(x509_str);
1176 BIO_free_all(bio);
1177 }
1178
1182
1183 if (!context->live)
1184 {
1185 if (!winpr_PathFileExists(context->output_path))
1186 {
1187 if (!winpr_PathMakePath(context->output_path, nullptr))
1188 return -1;
1189 }
1190
1191 if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1192 return -1;
1193
1194 if (context->crtFormat)
1195 {
1196 if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1197 return -1;
1198 }
1199 }
1200
1201 return 0;
1202#else
1203 WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
1204 return -1;
1205#endif
1206}
1207
1208MAKECERT_CONTEXT* makecert_context_new(void)
1209{
1210 if (!utils_set_umask())
1211 return nullptr;
1212
1213 MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1, sizeof(MAKECERT_CONTEXT));
1214
1215 if (context)
1216 {
1217 context->crtFormat = TRUE;
1218 context->duration_years = 1;
1219 }
1220
1221 return context;
1222}
1223
1224void makecert_context_free(MAKECERT_CONTEXT* context)
1225{
1226 if (context)
1227 {
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);
1233#ifdef WITH_OPENSSL
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();
1238#endif
1239#endif
1240 free(context);
1241 }
1242}