summaryrefslogtreecommitdiffstats
path: root/winpr/tools/makecert/makecert.c
blob: 85baeef95156cebb1f512ca506b5636f7691a2c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
/**
 * WinPR: Windows Portable Runtime
 * makecert replacement
 *
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>

#include <winpr/assert.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/file.h>
#include <winpr/cmdline.h>
#include <winpr/sysinfo.h>
#include <winpr/crypto.h>

#ifdef WITH_OPENSSL
#include <openssl/crypto.h>
#include <openssl/conf.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/pkcs12.h>
#include <openssl/x509v3.h>
#include <openssl/bn.h>
#endif

#include <winpr/tools/makecert.h>

struct S_MAKECERT_CONTEXT
{
	int argc;
	char** argv;

#ifdef WITH_OPENSSL
	X509* x509;
	EVP_PKEY* pkey;
	PKCS12* pkcs12;
#endif

	BOOL live;
	BOOL silent;

	BOOL crtFormat;
	BOOL pemFormat;
	BOOL pfxFormat;

	char* password;

	char* output_file;
	char* output_path;
	char* default_name;
	char* common_name;

	int duration_years;
	int duration_months;
};

static char* makecert_read_str(BIO* bio, size_t* pOffset)
{
	int status = -1;
	size_t offset = 0;
	size_t length = 0;
	char* x509_str = NULL;

	while (offset >= length)
	{
		size_t new_len = 0;
		size_t readBytes = 0;
		char* new_str = NULL;
		new_len = length * 2;
		if (new_len == 0)
			new_len = 2048;

		if (new_len > INT_MAX)
		{
			status = -1;
			break;
		}

		new_str = (char*)realloc(x509_str, new_len);

		if (!new_str)
		{
			status = -1;
			break;
		}

		length = new_len;
		x509_str = new_str;
		ERR_clear_error();
#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
		status = BIO_read_ex(bio, &x509_str[offset], length - offset, &readBytes);
#else
		status = BIO_read(bio, &x509_str[offset], length - offset);
		readBytes = status;
#endif
		if (status <= 0)
			break;

		offset += (size_t)readBytes;
	}

	if (status < 0)
	{
		free(x509_str);
		if (pOffset)
			*pOffset = 0;
		return NULL;
	}

	x509_str[offset] = '\0';
	if (pOffset)
		*pOffset = offset + 1;
	return x509_str;
}

static int makecert_print_command_line_help(COMMAND_LINE_ARGUMENT_A* args, int argc, char** argv)
{
	char* str = NULL;
	const COMMAND_LINE_ARGUMENT_A* arg = NULL;

	if (!argv || (argc < 1))
		return -1;

	printf("Usage: %s [options] [output file]\n", argv[0]);
	printf("\n");
	arg = args;

	do
	{
		if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
		{
			printf("    %s", "-");
			printf("%-20s", arg->Name);
			printf("\t%s\n", arg->Text);
		}
		else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
		         (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
		{
			printf("    %s", "-");

			if (arg->Format)
			{
				size_t length = strlen(arg->Name) + strlen(arg->Format) + 2;
				str = malloc(length + 1);

				if (!str)
					return -1;

				sprintf_s(str, length + 1, "%s %s", arg->Name, arg->Format);
				printf("%-20s", str);
				free(str);
			}
			else
			{
				printf("%-20s", arg->Name);
			}

			printf("\t%s\n", arg->Text);
		}
	} while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);

	return 1;
}

#ifdef WITH_OPENSSL
static int x509_add_ext(X509* cert, int nid, char* value)
{
	X509V3_CTX ctx;
	X509_EXTENSION* ext = NULL;

	if (!cert || !value)
		return 0;

	X509V3_set_ctx_nodb(&ctx) X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
	ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);

	if (!ext)
		return 0;

	X509_add_ext(cert, ext, -1);
	X509_EXTENSION_free(ext);
	return 1;
}
#endif

static char* x509_name_parse(char* name, char* txt, size_t* length)
{
	char* p = NULL;
	char* entry = NULL;

	if (!name || !txt || !length)
		return NULL;

	p = strstr(name, txt);

	if (!p)
		return NULL;

	entry = p + strlen(txt) + 1;
	p = strchr(entry, '=');

	if (!p)
		*length = strlen(entry);
	else
		*length = (size_t)(p - entry);

	return entry;
}

static char* x509_get_default_name(void)
{
	CHAR* computerName = NULL;
	DWORD nSize = 0;

	if (GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, NULL, &nSize) ||
	    GetLastError() != ERROR_MORE_DATA)
		goto fallback;

	computerName = (CHAR*)calloc(1, nSize);

	if (!computerName)
		goto fallback;

	if (!GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, computerName, &nSize))
		goto fallback;

	return computerName;
fallback:
	free(computerName);

	if (GetComputerNameExA(ComputerNamePhysicalNetBIOS, NULL, &nSize) ||
	    GetLastError() != ERROR_MORE_DATA)
		return NULL;

	computerName = (CHAR*)calloc(1, nSize);

	if (!computerName)
		return NULL;

	if (!GetComputerNameExA(ComputerNamePhysicalNetBIOS, computerName, &nSize))
	{
		free(computerName);
		return NULL;
	}

	return computerName;
}

static int command_line_pre_filter(MAKECERT_CONTEXT* context, int index, int argc, LPCSTR* argv)
{
	if (!context || !argv || (index < 0) || (argc < 0))
		return -1;

	if (index == (argc - 1))
	{
		if (argv[index][0] != '-')
		{
			context->output_file = _strdup(argv[index]);

			if (!context->output_file)
				return -1;

			return 1;
		}
	}

	return 0;
}

static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
                                            COMMAND_LINE_ARGUMENT_A* args, int argc, char** argv)
{
	int status = 0;
	DWORD flags = 0;
	const COMMAND_LINE_ARGUMENT_A* arg = NULL;

	if (!context || !argv || (argc < 0))
		return -1;

	/**
	 * makecert -r -pe -n "CN=%COMPUTERNAME%" -eku 1.3.6.1.5.5.7.3.1 -ss my -sr LocalMachine
	 * -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12
	 */
	CommandLineClearArgumentsA(args);
	flags = COMMAND_LINE_SEPARATOR_SPACE | COMMAND_LINE_SIGIL_DASH;
	status =
	    CommandLineParseArgumentsA(argc, argv, args, flags, context,
	                               (COMMAND_LINE_PRE_FILTER_FN_A)command_line_pre_filter, NULL);

	if (status & COMMAND_LINE_STATUS_PRINT_HELP)
	{
		makecert_print_command_line_help(args, argc, argv);
		return 0;
	}

	arg = args;
	errno = 0;

	do
	{
		if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
			continue;

		CommandLineSwitchStart(arg)
		    /* Basic Options */
		    CommandLineSwitchCase(arg, "silent")
		{
			context->silent = TRUE;
		}
		CommandLineSwitchCase(arg, "live")
		{
			context->live = TRUE;
		}
		CommandLineSwitchCase(arg, "format")
		{
			if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
				continue;

			if (strcmp(arg->Value, "crt") == 0)
			{
				context->crtFormat = TRUE;
				context->pemFormat = FALSE;
				context->pfxFormat = FALSE;
			}
			else if (strcmp(arg->Value, "pem") == 0)
			{
				context->crtFormat = FALSE;
				context->pemFormat = TRUE;
				context->pfxFormat = FALSE;
			}
			else if (strcmp(arg->Value, "pfx") == 0)
			{
				context->crtFormat = FALSE;
				context->pemFormat = FALSE;
				context->pfxFormat = TRUE;
			}
			else
				return -1;
		}
		CommandLineSwitchCase(arg, "path")
		{
			if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
				continue;

			context->output_path = _strdup(arg->Value);

			if (!context->output_path)
				return -1;
		}
		CommandLineSwitchCase(arg, "p")
		{
			if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
				continue;

			context->password = _strdup(arg->Value);

			if (!context->password)
				return -1;
		}
		CommandLineSwitchCase(arg, "n")
		{
			if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
				continue;

			context->common_name = _strdup(arg->Value);

			if (!context->common_name)
				return -1;
		}
		CommandLineSwitchCase(arg, "y")
		{
			long val = 0;

			if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
				continue;

			val = strtol(arg->Value, NULL, 0);

			if ((errno != 0) || (val < 0) || (val > INT32_MAX))
				return -1;

			context->duration_years = (int)val;
		}
		CommandLineSwitchCase(arg, "m")
		{
			long val = 0;

			if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
				continue;

			val = strtol(arg->Value, NULL, 0);

			if ((errno != 0) || (val < 1) || (val > 12))
				return -1;

			context->duration_months = (int)val;
		}
		CommandLineSwitchDefault(arg)
		{
		}
		CommandLineSwitchEnd(arg)
	} while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);

	return 1;
}

int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context, const char* name)
{
	if (!context)
		return -1;

	free(context->output_file);
	context->output_file = NULL;

	if (name)
		context->output_file = _strdup(name);

	if (!context->output_file)
		return -1;

	return 1;
}

int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, const char* path)
{
#ifdef WITH_OPENSSL
	FILE* fp = NULL;
	int status = 0;
	size_t length = 0;
	size_t offset = 0;
	char* filename = NULL;
	char* fullpath = NULL;
	char* ext = NULL;
	int ret = -1;
	BIO* bio = NULL;
	char* x509_str = NULL;

	if (!context || !path)
		return -1;

	if (!context->output_file)
	{
		context->output_file = _strdup(context->default_name);

		if (!context->output_file)
			return -1;
	}

	/*
	 * Output Certificate File
	 */
	length = strlen(context->output_file);
	filename = malloc(length + 8);

	if (!filename)
		return -1;

	if (context->crtFormat)
		ext = "crt";
	else if (context->pemFormat)
		ext = "pem";
	else if (context->pfxFormat)
		ext = "pfx";
	else
		goto out_fail;

	sprintf_s(filename, length + 8, "%s.%s", context->output_file, ext);

	if (path)
		fullpath = GetCombinedPath(path, filename);
	else
		fullpath = _strdup(filename);

	if (!fullpath)
		goto out_fail;

	fp = winpr_fopen(fullpath, "w+");

	if (fp)
	{
		if (context->pfxFormat)
		{
			if (!context->password)
			{
				context->password = _strdup("password");

				if (!context->password)
					goto out_fail;

				printf("Using default export password \"password\"\n");
			}

#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
			OpenSSL_add_all_algorithms();
			OpenSSL_add_all_ciphers();
			OpenSSL_add_all_digests();
#else
			OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
			                        OPENSSL_INIT_LOAD_CONFIG,
			                    NULL);
#endif
			context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
			                                context->x509, NULL, 0, 0, 0, 0, 0);

			if (!context->pkcs12)
				goto out_fail;

			bio = BIO_new(BIO_s_mem());

			if (!bio)
				goto out_fail;

			status = i2d_PKCS12_bio(bio, context->pkcs12);

			if (status != 1)
				goto out_fail;

			x509_str = makecert_read_str(bio, &offset);

			if (!x509_str)
				goto out_fail;

			length = offset;

			if (fwrite((void*)x509_str, length, 1, fp) != 1)
				goto out_fail;
		}
		else
		{
			bio = BIO_new(BIO_s_mem());

			if (!bio)
				goto out_fail;

			if (!PEM_write_bio_X509(bio, context->x509))
				goto out_fail;

			x509_str = makecert_read_str(bio, &offset);

			if (!x509_str)
				goto out_fail;

			length = offset;

			if (fwrite(x509_str, length, 1, fp) != 1)
				goto out_fail;

			free(x509_str);
			x509_str = NULL;
			BIO_free_all(bio);
			bio = NULL;

			if (context->pemFormat)
			{
				bio = BIO_new(BIO_s_mem());

				if (!bio)
					goto out_fail;

				status = PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL);

				if (status < 0)
					goto out_fail;

				x509_str = makecert_read_str(bio, &offset);
				if (!x509_str)
					goto out_fail;

				length = offset;

				if (fwrite(x509_str, length, 1, fp) != 1)
					goto out_fail;
			}
		}
	}

	ret = 1;
out_fail:
	BIO_free_all(bio);

	if (fp)
		fclose(fp);

	free(x509_str);
	free(filename);
	free(fullpath);
	return ret;
#else
	WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
	return -1;
#endif
}

int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, const char* path)
{
#ifdef WITH_OPENSSL
	FILE* fp = NULL;
	size_t length = 0;
	size_t offset = 0;
	char* filename = NULL;
	char* fullpath = NULL;
	int ret = -1;
	BIO* bio = NULL;
	char* x509_str = NULL;

	if (!context->crtFormat)
		return 1;

	if (!context->output_file)
	{
		context->output_file = _strdup(context->default_name);

		if (!context->output_file)
			return -1;
	}

	/**
	 * Output Private Key File
	 */
	length = strlen(context->output_file);
	filename = malloc(length + 8);

	if (!filename)
		return -1;

	sprintf_s(filename, length + 8, "%s.key", context->output_file);

	if (path)
		fullpath = GetCombinedPath(path, filename);
	else
		fullpath = _strdup(filename);

	if (!fullpath)
		goto out_fail;

	fp = winpr_fopen(fullpath, "w+");

	if (!fp)
		goto out_fail;

	bio = BIO_new(BIO_s_mem());

	if (!bio)
		goto out_fail;

	if (!PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL))
		goto out_fail;

	x509_str = makecert_read_str(bio, &offset);

	if (!x509_str)
		goto out_fail;

	length = offset;

	if (fwrite((void*)x509_str, length, 1, fp) != 1)
		goto out_fail;

	ret = 1;
out_fail:

	if (fp)
		fclose(fp);

	BIO_free_all(bio);
	free(x509_str);
	free(filename);
	free(fullpath);
	return ret;
#else
	WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
	return -1;
#endif
}

#ifdef WITH_OPENSSL
static BOOL makecert_create_rsa(EVP_PKEY** ppkey, size_t key_length)
{
	BOOL rc = FALSE;

	WINPR_ASSERT(ppkey);

#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
	RSA* rsa = NULL;
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
	rsa = RSA_generate_key(key_length, RSA_F4, NULL, NULL);
#else
	{
		BIGNUM* bn = BN_secure_new();

		if (!bn)
			return FALSE;

		rsa = RSA_new();

		if (!rsa)
		{
			BN_clear_free(bn);
			return FALSE;
		}

		BN_set_word(bn, RSA_F4);
		const int res = RSA_generate_key_ex(rsa, key_length, bn, NULL);
		BN_clear_free(bn);

		if (res != 1)
			return FALSE;
	}
#endif

	if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
	{
		RSA_free(rsa);
		return FALSE;
	}
	rc = TRUE;
#else
	EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
	if (!pctx)
		return FALSE;

	if (EVP_PKEY_keygen_init(pctx) != 1)
		goto fail;

	WINPR_ASSERT(key_length <= UINT_MAX);
	unsigned int keylen = (unsigned int)key_length;
	const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint("bits", &keylen),
		                          OSSL_PARAM_construct_end() };
	if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
		goto fail;

	if (EVP_PKEY_generate(pctx, ppkey) != 1)
		goto fail;

	rc = TRUE;
fail:
	EVP_PKEY_CTX_free(pctx);
#endif
	return rc;
}
#endif

int makecert_context_process(MAKECERT_CONTEXT* context, int argc, char** argv)
{
	COMMAND_LINE_ARGUMENT_A args[] = {
		/* Custom Options */

		{ "rdp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
		  "Unsupported - Generate certificate with required options for RDP usage." },
		{ "silent", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
		  "Silently generate certificate without verbose output." },
		{ "live", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
		  "Generate certificate live in memory when used as a library." },
		{ "format", COMMAND_LINE_VALUE_REQUIRED, "<crt|pem|pfx>", NULL, NULL, -1, NULL,
		  "Specify certificate file format" },
		{ "path", COMMAND_LINE_VALUE_REQUIRED, "<path>", NULL, NULL, -1, NULL,
		  "Specify certificate file output path" },
		{ "p", COMMAND_LINE_VALUE_REQUIRED, "<password>", NULL, NULL, -1, NULL,
		  "Specify certificate export password" },

		/* Basic Options */

		{ "n", COMMAND_LINE_VALUE_REQUIRED, "<name>", NULL, NULL, -1, NULL,
		  "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
		  "The simplest method is to specify the name in double quotes, preceded by CN=; for "
		  "example, "
		  "-n \"CN=myName\"." },
		{ "pe", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
		  "Unsupported - Marks the generated private key as exportable. This allows the private "
		  "key to "
		  "be included in the certificate." },
		{ "sk", COMMAND_LINE_VALUE_REQUIRED, "<keyname>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's key container location, which contains the "
		  "private "
		  "key. "
		  "If a key container does not exist, it will be created." },
		{ "sr", COMMAND_LINE_VALUE_REQUIRED, "<location>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's certificate store location. location can be "
		  "either "
		  "currentuser (the default) or localmachine." },
		{ "ss", COMMAND_LINE_VALUE_REQUIRED, "<store>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's certificate store name that stores the output "
		  "certificate." },
		{ "#", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
		  "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
		  "generated "
		  "by Makecert.exe." },
		{ "$", COMMAND_LINE_VALUE_REQUIRED, "<authority>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the signing authority of the certificate, which must be set to "
		  "either commercial "
		  "(for certificates used by commercial software publishers) or individual (for "
		  "certificates "
		  "used by individual software publishers)." },

		/* Extended Options */

		{ "a", COMMAND_LINE_VALUE_REQUIRED, "<algorithm>", NULL, NULL, -1, NULL,
		  "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
		  "sha384, or sha512." },
		{ "b", COMMAND_LINE_VALUE_REQUIRED, "<mm/dd/yyyy>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the start of the validity period. Defaults to the current "
		  "date." },
		{ "crl", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
		  "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
		{ "cy", COMMAND_LINE_VALUE_REQUIRED, "<certType>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
		  "authority for certification authority." },
		{ "e", COMMAND_LINE_VALUE_REQUIRED, "<mm/dd/yyyy>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
		  "GMT." },
		{ "eku", COMMAND_LINE_VALUE_REQUIRED, "<oid[,oid…]>", NULL, NULL, -1, NULL,
		  "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
		  "(OIDs) into the certificate." },
		{ "h", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the maximum height of the tree below this certificate." },
		{ "ic", COMMAND_LINE_VALUE_REQUIRED, "<file>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's certificate file." },
		{ "ik", COMMAND_LINE_VALUE_REQUIRED, "<keyName>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's key container name." },
		{ "iky", COMMAND_LINE_VALUE_REQUIRED, "<keyType>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's key type, which must be one of the following: "
		  "signature (which indicates that the key is used for a digital signature), "
		  "exchange (which indicates that the key is used for key encryption and key exchange), "
		  "or an integer that represents a provider type. "
		  "By default, you can pass 1 for an exchange key or 2 for a signature key." },
		{ "in", COMMAND_LINE_VALUE_REQUIRED, "<name>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's certificate common name." },
		{ "ip", COMMAND_LINE_VALUE_REQUIRED, "<provider>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
		  "CryptoAPI provider name, see the –sp option." },
		{ "ir", COMMAND_LINE_VALUE_REQUIRED, "<location>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the location of the issuer's certificate store. location can be "
		  "either currentuser (the default) or localmachine." },
		{ "is", COMMAND_LINE_VALUE_REQUIRED, "<store>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's certificate store name." },
		{ "iv", COMMAND_LINE_VALUE_REQUIRED, "<pvkFile>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's .pvk private key file." },
		{ "iy", COMMAND_LINE_VALUE_REQUIRED, "<type>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
		  "CryptoAPI provider type, see the –sy option." },
		{ "l", COMMAND_LINE_VALUE_REQUIRED, "<link>", NULL, NULL, -1, NULL,
		  "Unsupported - Links to policy information (for example, to a URL)." },
		{ "len", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
		  "Specifies the generated key length, in bits." },
		{ "m", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
		  "Specifies the duration, in months, of the certificate validity period." },
		{ "y", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
		  "Specifies the duration, in years, of the certificate validity period." },
		{ "nscp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
		  "Unsupported - Includes the Netscape client-authorization extension." },
		{ "r", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
		  "Unsupported - Creates a self-signed certificate." },
		{ "sc", COMMAND_LINE_VALUE_REQUIRED, "<file>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's certificate file." },
		{ "sky", COMMAND_LINE_VALUE_REQUIRED, "<keyType>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's key type, which must be one of the following: "
		  "signature (which indicates that the key is used for a digital signature), "
		  "exchange (which indicates that the key is used for key encryption and key exchange), "
		  "or an integer that represents a provider type. "
		  "By default, you can pass 1 for an exchange key or 2 for a signature key." },
		{ "sp", COMMAND_LINE_VALUE_REQUIRED, "<provider>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
		  "the "
		  "registry subkeys of "
		  "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
		  "and "
		  "–sy are present, "
		  "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
		  "subkey." },
		{ "sv", COMMAND_LINE_VALUE_REQUIRED, "<pvkFile>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
		  "none "
		  "exists." },
		{ "sy", COMMAND_LINE_VALUE_REQUIRED, "<type>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
		  "the "
		  "registry subkeys of "
		  "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
		  "both "
		  "–sy and –sp are present, "
		  "the name of the CryptoAPI provider must correspond to the Name value of the provider "
		  "type "
		  "subkey." },
		{ "tbs", COMMAND_LINE_VALUE_REQUIRED, "<file>", NULL, NULL, -1, NULL,
		  "Unsupported - Specifies the certificate or CRL file to be signed." },

		/* Help */

		{ "?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1, "help",
		  "print help" },
		{ "!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1, "help-ext",
		  "print extended help" },
		{ NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
	};
#ifdef WITH_OPENSSL
	size_t length = 0;
	char* entry = NULL;
	int key_length = 0;
	long serial = 0;
	X509_NAME* name = NULL;
	const EVP_MD* md = NULL;
	const COMMAND_LINE_ARGUMENT_A* arg = NULL;
	int ret = 0;
	ret = makecert_context_parse_arguments(context, args, argc, argv);

	if (ret < 1)
	{
		return ret;
	}

	if (!context->default_name && !context->common_name)
	{
		context->default_name = x509_get_default_name();

		if (!context->default_name)
			return -1;
	}
	else
	{
		context->default_name = _strdup(context->common_name);

		if (!context->default_name)
			return -1;
	}

	if (!context->common_name)
	{
		context->common_name = _strdup(context->default_name);

		if (!context->common_name)
			return -1;
	}

	if (!context->pkey)
		context->pkey = EVP_PKEY_new();

	if (!context->pkey)
		return -1;

	if (!context->x509)
		context->x509 = X509_new();

	if (!context->x509)
		return -1;

	key_length = 2048;
	arg = CommandLineFindArgumentA(args, "len");

	if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
	{
		unsigned long val = strtoul(arg->Value, NULL, 0);

		if ((errno != 0) || (val > INT_MAX))
			return -1;
		key_length = (int)val;
	}

	if (!makecert_create_rsa(&context->pkey, key_length))
		return -1;

	X509_set_version(context->x509, 2);
	arg = CommandLineFindArgumentA(args, "#");

	if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
	{
		serial = strtol(arg->Value, NULL, 0);

		if (errno != 0)
			return -1;
	}
	else
		serial = (long)GetTickCount64();

	ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial);
	{
		ASN1_TIME* before = NULL;
		ASN1_TIME* after = NULL;
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
		before = X509_get_notBefore(context->x509);
		after = X509_get_notAfter(context->x509);
#else
		before = X509_getm_notBefore(context->x509);
		after = X509_getm_notAfter(context->x509);
#endif
		X509_gmtime_adj(before, 0);

		if (context->duration_months)
			X509_gmtime_adj(after, (long)(60 * 60 * 24 * 31 * context->duration_months));
		else if (context->duration_years)
			X509_gmtime_adj(after, (long)(60 * 60 * 24 * 365 * context->duration_years));
	}
	X509_set_pubkey(context->x509, context->pkey);
	name = X509_get_subject_name(context->x509);
	arg = CommandLineFindArgumentA(args, "n");

	if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
	{
		entry = x509_name_parse(arg->Value, "C", &length);

		if (entry)
			X509_NAME_add_entry_by_txt(name, "C", MBSTRING_UTF8, (const unsigned char*)entry,
			                           (int)length, -1, 0);

		entry = x509_name_parse(arg->Value, "ST", &length);

		if (entry)
			X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_UTF8, (const unsigned char*)entry,
			                           (int)length, -1, 0);

		entry = x509_name_parse(arg->Value, "L", &length);

		if (entry)
			X509_NAME_add_entry_by_txt(name, "L", MBSTRING_UTF8, (const unsigned char*)entry,
			                           (int)length, -1, 0);

		entry = x509_name_parse(arg->Value, "O", &length);

		if (entry)
			X509_NAME_add_entry_by_txt(name, "O", MBSTRING_UTF8, (const unsigned char*)entry,
			                           (int)length, -1, 0);

		entry = x509_name_parse(arg->Value, "OU", &length);

		if (entry)
			X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8, (const unsigned char*)entry,
			                           (int)length, -1, 0);

		entry = context->common_name;
		length = strlen(entry);
		X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_UTF8, (const unsigned char*)entry,
		                           (int)length, -1, 0);
	}
	else
	{
		entry = context->common_name;
		length = strlen(entry);
		X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_UTF8, (const unsigned char*)entry,
		                           (int)length, -1, 0);
	}

	X509_set_issuer_name(context->x509, name);
	x509_add_ext(context->x509, NID_ext_key_usage, "serverAuth");
	arg = CommandLineFindArgumentA(args, "a");
	md = EVP_sha256();

	if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
	{
		md = EVP_get_digestbyname(arg->Value);
		if (!md)
			return -1;
	}

	if (!X509_sign(context->x509, context->pkey, md))
		return -1;

	/**
	 * Print certificate
	 */

	if (!context->silent)
	{
		BIO* bio = NULL;
		int status = 0;
		char* x509_str = NULL;
		bio = BIO_new(BIO_s_mem());

		if (!bio)
			return -1;

		status = X509_print(bio, context->x509);

		if (status < 0)
		{
			BIO_free_all(bio);
			return -1;
		}

		x509_str = makecert_read_str(bio, NULL);
		if (!x509_str)
		{
			BIO_free_all(bio);
			return -1;
		}

		printf("%s", x509_str);
		free(x509_str);
		BIO_free_all(bio);
	}

	/**
	 * Output certificate and private key to files
	 */

	if (!context->live)
	{
		if (!winpr_PathFileExists(context->output_path))
		{
			if (!CreateDirectoryA(context->output_path, NULL))
				return -1;
		}

		if (makecert_context_output_certificate_file(context, context->output_path) != 1)
			return -1;

		if (context->crtFormat)
		{
			if (makecert_context_output_private_key_file(context, context->output_path) < 0)
				return -1;
		}
	}

	return 0;
#else
	WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
	return -1;
#endif
}

MAKECERT_CONTEXT* makecert_context_new(void)
{
	MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1, sizeof(MAKECERT_CONTEXT));

	if (context)
	{
		context->crtFormat = TRUE;
		context->duration_years = 1;
	}

	return context;
}

void makecert_context_free(MAKECERT_CONTEXT* context)
{
	if (context)
	{
		free(context->password);
		free(context->default_name);
		free(context->common_name);
		free(context->output_file);
		free(context->output_path);
#ifdef WITH_OPENSSL
		X509_free(context->x509);
		EVP_PKEY_free(context->pkey);
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
		CRYPTO_cleanup_all_ex_data();
#endif
#endif
		free(context);
	}
}