FreeRDP
makecert.c
1 
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 
30 #ifdef WITH_OPENSSL
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>
39 #endif
40 
41 #include <winpr/tools/makecert.h>
42 
43 struct S_MAKECERT_CONTEXT
44 {
45  int argc;
46  char** argv;
47 
48 #ifdef WITH_OPENSSL
49  X509* x509;
50  EVP_PKEY* pkey;
51  PKCS12* pkcs12;
52 #endif
53 
54  BOOL live;
55  BOOL silent;
56 
57  BOOL crtFormat;
58  BOOL pemFormat;
59  BOOL pfxFormat;
60 
61  char* password;
62 
63  char* output_file;
64  char* output_path;
65  char* default_name;
66  char* common_name;
67 
68  int duration_years;
69  int duration_months;
70 };
71 
72 static char* makecert_read_str(BIO* bio, size_t* pOffset)
73 {
74  int status = -1;
75  size_t offset = 0;
76  size_t length = 0;
77  char* x509_str = NULL;
78 
79  while (offset >= length)
80  {
81  size_t new_len = 0;
82  size_t readBytes = 0;
83  char* new_str = NULL;
84  new_len = length * 2;
85  if (new_len == 0)
86  new_len = 2048;
87 
88  if (new_len > INT_MAX)
89  {
90  status = -1;
91  break;
92  }
93 
94  new_str = (char*)realloc(x509_str, new_len);
95 
96  if (!new_str)
97  {
98  status = -1;
99  break;
100  }
101 
102  length = new_len;
103  x509_str = new_str;
104  ERR_clear_error();
105 #if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
106  status = BIO_read_ex(bio, &x509_str[offset], length - offset, &readBytes);
107 #else
108  status = BIO_read(bio, &x509_str[offset], length - offset);
109  readBytes = status;
110 #endif
111  if (status <= 0)
112  break;
113 
114  offset += readBytes;
115  }
116 
117  if (status < 0)
118  {
119  free(x509_str);
120  if (pOffset)
121  *pOffset = 0;
122  return NULL;
123  }
124 
125  x509_str[offset] = '\0';
126  if (pOffset)
127  *pOffset = offset + 1;
128  return x509_str;
129 }
130 
131 static int makecert_print_command_line_help(COMMAND_LINE_ARGUMENT_A* args, int argc, char** argv)
132 {
133  char* str = NULL;
134  const COMMAND_LINE_ARGUMENT_A* arg = NULL;
135 
136  if (!argv || (argc < 1))
137  return -1;
138 
139  printf("Usage: %s [options] [output file]\n", argv[0]);
140  printf("\n");
141  arg = args;
142 
143  do
144  {
145  if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
146  {
147  printf(" %s", "-");
148  printf("%-20s", arg->Name);
149  printf("\t%s\n", arg->Text);
150  }
151  else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
152  (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
153  {
154  printf(" %s", "-");
155 
156  if (arg->Format)
157  {
158  size_t length = strlen(arg->Name) + strlen(arg->Format) + 2;
159  str = malloc(length + 1);
160 
161  if (!str)
162  return -1;
163 
164  (void)sprintf_s(str, length + 1, "%s %s", arg->Name, arg->Format);
165  (void)printf("%-20s", str);
166  free(str);
167  }
168  else
169  {
170  printf("%-20s", arg->Name);
171  }
172 
173  printf("\t%s\n", arg->Text);
174  }
175  } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
176 
177  return 1;
178 }
179 
180 #ifdef WITH_OPENSSL
181 static int x509_add_ext(X509* cert, int nid, char* value)
182 {
183  X509V3_CTX ctx;
184  X509_EXTENSION* ext = NULL;
185 
186  if (!cert || !value)
187  return 0;
188 
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);
191 
192  if (!ext)
193  return 0;
194 
195  X509_add_ext(cert, ext, -1);
196  X509_EXTENSION_free(ext);
197  return 1;
198 }
199 #endif
200 
201 static char* x509_name_parse(char* name, char* txt, size_t* length)
202 {
203  char* p = NULL;
204  char* entry = NULL;
205 
206  if (!name || !txt || !length)
207  return NULL;
208 
209  p = strstr(name, txt);
210 
211  if (!p)
212  return NULL;
213 
214  entry = p + strlen(txt) + 1;
215  p = strchr(entry, '=');
216 
217  if (!p)
218  *length = strlen(entry);
219  else
220  *length = (size_t)(p - entry);
221 
222  return entry;
223 }
224 
225 static char* x509_get_default_name(void)
226 {
227  CHAR* computerName = NULL;
228  DWORD nSize = 0;
229 
230  if (GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, NULL, &nSize) ||
231  GetLastError() != ERROR_MORE_DATA)
232  goto fallback;
233 
234  computerName = (CHAR*)calloc(1, nSize);
235 
236  if (!computerName)
237  goto fallback;
238 
239  if (!GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, computerName, &nSize))
240  goto fallback;
241 
242  return computerName;
243 fallback:
244  free(computerName);
245 
246  if (GetComputerNameExA(ComputerNamePhysicalNetBIOS, NULL, &nSize) ||
247  GetLastError() != ERROR_MORE_DATA)
248  return NULL;
249 
250  computerName = (CHAR*)calloc(1, nSize);
251 
252  if (!computerName)
253  return NULL;
254 
255  if (!GetComputerNameExA(ComputerNamePhysicalNetBIOS, computerName, &nSize))
256  {
257  free(computerName);
258  return NULL;
259  }
260 
261  return computerName;
262 }
263 
264 static int command_line_pre_filter(void* pvctx, int index, int argc, LPSTR* argv)
265 {
266  MAKECERT_CONTEXT* context = pvctx;
267  if (!context || !argv || (index < 0) || (argc < 0))
268  return -1;
269 
270  if (index == (argc - 1))
271  {
272  if (argv[index][0] != '-')
273  {
274  context->output_file = _strdup(argv[index]);
275 
276  if (!context->output_file)
277  return -1;
278 
279  return 1;
280  }
281  }
282 
283  return 0;
284 }
285 
286 static int makecert_context_parse_arguments(MAKECERT_CONTEXT* context,
287  COMMAND_LINE_ARGUMENT_A* args, int argc, char** argv)
288 {
289  int status = 0;
290  DWORD flags = 0;
291  const COMMAND_LINE_ARGUMENT_A* arg = NULL;
292 
293  if (!context || !argv || (argc < 0))
294  return -1;
295 
300  CommandLineClearArgumentsA(args);
301  flags = COMMAND_LINE_SEPARATOR_SPACE | COMMAND_LINE_SIGIL_DASH;
302  status =
303  CommandLineParseArgumentsA(argc, argv, args, flags, context, command_line_pre_filter, NULL);
304 
305  if (status & COMMAND_LINE_STATUS_PRINT_HELP)
306  {
307  makecert_print_command_line_help(args, argc, argv);
308  return 0;
309  }
310 
311  arg = args;
312  errno = 0;
313 
314  do
315  {
316  if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
317  continue;
318 
319  CommandLineSwitchStart(arg)
320  /* Basic Options */
321  CommandLineSwitchCase(arg, "silent")
322  {
323  context->silent = TRUE;
324  }
325  CommandLineSwitchCase(arg, "live")
326  {
327  context->live = TRUE;
328  }
329  CommandLineSwitchCase(arg, "format")
330  {
331  if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
332  continue;
333 
334  if (strcmp(arg->Value, "crt") == 0)
335  {
336  context->crtFormat = TRUE;
337  context->pemFormat = FALSE;
338  context->pfxFormat = FALSE;
339  }
340  else if (strcmp(arg->Value, "pem") == 0)
341  {
342  context->crtFormat = FALSE;
343  context->pemFormat = TRUE;
344  context->pfxFormat = FALSE;
345  }
346  else if (strcmp(arg->Value, "pfx") == 0)
347  {
348  context->crtFormat = FALSE;
349  context->pemFormat = FALSE;
350  context->pfxFormat = TRUE;
351  }
352  else
353  return -1;
354  }
355  CommandLineSwitchCase(arg, "path")
356  {
357  if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
358  continue;
359 
360  context->output_path = _strdup(arg->Value);
361 
362  if (!context->output_path)
363  return -1;
364  }
365  CommandLineSwitchCase(arg, "p")
366  {
367  if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
368  continue;
369 
370  context->password = _strdup(arg->Value);
371 
372  if (!context->password)
373  return -1;
374  }
375  CommandLineSwitchCase(arg, "n")
376  {
377  if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
378  continue;
379 
380  context->common_name = _strdup(arg->Value);
381 
382  if (!context->common_name)
383  return -1;
384  }
385  CommandLineSwitchCase(arg, "y")
386  {
387  long val = 0;
388 
389  if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
390  continue;
391 
392  val = strtol(arg->Value, NULL, 0);
393 
394  if ((errno != 0) || (val < 0) || (val > INT32_MAX))
395  return -1;
396 
397  context->duration_years = (int)val;
398  }
399  CommandLineSwitchCase(arg, "m")
400  {
401  long val = 0;
402 
403  if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
404  continue;
405 
406  val = strtol(arg->Value, NULL, 0);
407 
408  if ((errno != 0) || (val < 0))
409  return -1;
410 
411  context->duration_months = (int)val;
412  }
413  CommandLineSwitchDefault(arg)
414  {
415  }
416  CommandLineSwitchEnd(arg)
417  } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
418 
419  return 1;
420 }
421 
422 int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context, const char* name)
423 {
424  if (!context)
425  return -1;
426 
427  free(context->output_file);
428  context->output_file = NULL;
429 
430  if (name)
431  context->output_file = _strdup(name);
432 
433  if (!context->output_file)
434  return -1;
435 
436  return 1;
437 }
438 
439 int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, const char* path)
440 {
441 #ifdef WITH_OPENSSL
442  FILE* fp = NULL;
443  int status = 0;
444  size_t length = 0;
445  size_t offset = 0;
446  char* filename = NULL;
447  char* fullpath = NULL;
448  char* ext = NULL;
449  int ret = -1;
450  BIO* bio = NULL;
451  char* x509_str = NULL;
452 
453  if (!context)
454  return -1;
455 
456  if (!context->output_file)
457  {
458  context->output_file = _strdup(context->default_name);
459 
460  if (!context->output_file)
461  return -1;
462  }
463 
464  /*
465  * Output Certificate File
466  */
467  length = strlen(context->output_file);
468  filename = malloc(length + 8);
469 
470  if (!filename)
471  return -1;
472 
473  if (context->crtFormat)
474  ext = "crt";
475  else if (context->pemFormat)
476  ext = "pem";
477  else if (context->pfxFormat)
478  ext = "pfx";
479  else
480  goto out_fail;
481 
482  (void)sprintf_s(filename, length + 8, "%s.%s", context->output_file, ext);
483 
484  if (path)
485  fullpath = GetCombinedPath(path, filename);
486  else
487  fullpath = _strdup(filename);
488 
489  if (!fullpath)
490  goto out_fail;
491 
492  fp = winpr_fopen(fullpath, "w+");
493 
494  if (fp)
495  {
496  if (context->pfxFormat)
497  {
498  if (!context->password)
499  {
500  context->password = _strdup("password");
501 
502  if (!context->password)
503  goto out_fail;
504 
505  printf("Using default export password \"password\"\n");
506  }
507 
508 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
509  OpenSSL_add_all_algorithms();
510  OpenSSL_add_all_ciphers();
511  OpenSSL_add_all_digests();
512 #else
513  OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
514  OPENSSL_INIT_LOAD_CONFIG,
515  NULL);
516 #endif
517  context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey,
518  context->x509, NULL, 0, 0, 0, 0, 0);
519 
520  if (!context->pkcs12)
521  goto out_fail;
522 
523  bio = BIO_new(BIO_s_mem());
524 
525  if (!bio)
526  goto out_fail;
527 
528  status = i2d_PKCS12_bio(bio, context->pkcs12);
529 
530  if (status != 1)
531  goto out_fail;
532 
533  x509_str = makecert_read_str(bio, &offset);
534 
535  if (!x509_str)
536  goto out_fail;
537 
538  length = offset;
539 
540  if (fwrite((void*)x509_str, length, 1, fp) != 1)
541  goto out_fail;
542  }
543  else
544  {
545  bio = BIO_new(BIO_s_mem());
546 
547  if (!bio)
548  goto out_fail;
549 
550  if (!PEM_write_bio_X509(bio, context->x509))
551  goto out_fail;
552 
553  x509_str = makecert_read_str(bio, &offset);
554 
555  if (!x509_str)
556  goto out_fail;
557 
558  length = offset;
559 
560  if (fwrite(x509_str, length, 1, fp) != 1)
561  goto out_fail;
562 
563  free(x509_str);
564  x509_str = NULL;
565  BIO_free_all(bio);
566  bio = NULL;
567 
568  if (context->pemFormat)
569  {
570  bio = BIO_new(BIO_s_mem());
571 
572  if (!bio)
573  goto out_fail;
574 
575  status = PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL);
576 
577  if (status < 0)
578  goto out_fail;
579 
580  x509_str = makecert_read_str(bio, &offset);
581  if (!x509_str)
582  goto out_fail;
583 
584  length = offset;
585 
586  if (fwrite(x509_str, length, 1, fp) != 1)
587  goto out_fail;
588  }
589  }
590  }
591 
592  ret = 1;
593 out_fail:
594  BIO_free_all(bio);
595 
596  if (fp)
597  (void)fclose(fp);
598 
599  free(x509_str);
600  free(filename);
601  free(fullpath);
602  return ret;
603 #else
604  WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
605  return -1;
606 #endif
607 }
608 
609 int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, const char* path)
610 {
611 #ifdef WITH_OPENSSL
612  FILE* fp = NULL;
613  size_t length = 0;
614  size_t offset = 0;
615  char* filename = NULL;
616  char* fullpath = NULL;
617  int ret = -1;
618  BIO* bio = NULL;
619  char* x509_str = NULL;
620 
621  if (!context->crtFormat)
622  return 1;
623 
624  if (!context->output_file)
625  {
626  context->output_file = _strdup(context->default_name);
627 
628  if (!context->output_file)
629  return -1;
630  }
631 
635  length = strlen(context->output_file);
636  filename = malloc(length + 8);
637 
638  if (!filename)
639  return -1;
640 
641  (void)sprintf_s(filename, length + 8, "%s.key", context->output_file);
642 
643  if (path)
644  fullpath = GetCombinedPath(path, filename);
645  else
646  fullpath = _strdup(filename);
647 
648  if (!fullpath)
649  goto out_fail;
650 
651  fp = winpr_fopen(fullpath, "w+");
652 
653  if (!fp)
654  goto out_fail;
655 
656  bio = BIO_new(BIO_s_mem());
657 
658  if (!bio)
659  goto out_fail;
660 
661  if (!PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL))
662  goto out_fail;
663 
664  x509_str = makecert_read_str(bio, &offset);
665 
666  if (!x509_str)
667  goto out_fail;
668 
669  length = offset;
670 
671  if (fwrite((void*)x509_str, length, 1, fp) != 1)
672  goto out_fail;
673 
674  ret = 1;
675 out_fail:
676 
677  if (fp)
678  (void)fclose(fp);
679 
680  BIO_free_all(bio);
681  free(x509_str);
682  free(filename);
683  free(fullpath);
684  return ret;
685 #else
686  WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
687  return -1;
688 #endif
689 }
690 
691 #ifdef WITH_OPENSSL
692 static BOOL makecert_create_rsa(EVP_PKEY** ppkey, size_t key_length)
693 {
694  BOOL rc = FALSE;
695 
696  WINPR_ASSERT(ppkey);
697 
698 #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
699  RSA* rsa = NULL;
700 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
701  rsa = RSA_generate_key(key_length, RSA_F4, NULL, NULL);
702 #else
703  {
704  BIGNUM* bn = BN_secure_new();
705 
706  if (!bn)
707  return FALSE;
708 
709  rsa = RSA_new();
710 
711  if (!rsa)
712  {
713  BN_clear_free(bn);
714  return FALSE;
715  }
716 
717  BN_set_word(bn, RSA_F4);
718  const int res = RSA_generate_key_ex(rsa, key_length, bn, NULL);
719  BN_clear_free(bn);
720 
721  if (res != 1)
722  return FALSE;
723  }
724 #endif
725 
726  if (!EVP_PKEY_assign_RSA(*ppkey, rsa))
727  {
728  RSA_free(rsa);
729  return FALSE;
730  }
731  rc = TRUE;
732 #else
733  EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
734  if (!pctx)
735  return FALSE;
736 
737  if (EVP_PKEY_keygen_init(pctx) != 1)
738  goto fail;
739 
740  WINPR_ASSERT(key_length <= UINT_MAX);
741  unsigned int keylen = (unsigned int)key_length;
742  const OSSL_PARAM params[] = { OSSL_PARAM_construct_uint("bits", &keylen),
743  OSSL_PARAM_construct_end() };
744  if (EVP_PKEY_CTX_set_params(pctx, params) != 1)
745  goto fail;
746 
747  if (EVP_PKEY_generate(pctx, ppkey) != 1)
748  goto fail;
749 
750  rc = TRUE;
751 fail:
752  EVP_PKEY_CTX_free(pctx);
753 #endif
754  return rc;
755 }
756 #endif
757 
758 int makecert_context_process(MAKECERT_CONTEXT* context, int argc, char** argv)
759 {
760  COMMAND_LINE_ARGUMENT_A args[] = {
761  /* Custom Options */
762 
763  { "rdp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
764  "Unsupported - Generate certificate with required options for RDP usage." },
765  { "silent", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
766  "Silently generate certificate without verbose output." },
767  { "live", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
768  "Generate certificate live in memory when used as a library." },
769  { "format", COMMAND_LINE_VALUE_REQUIRED, "<crt|pem|pfx>", NULL, NULL, -1, NULL,
770  "Specify certificate file format" },
771  { "path", COMMAND_LINE_VALUE_REQUIRED, "<path>", NULL, NULL, -1, NULL,
772  "Specify certificate file output path" },
773  { "p", COMMAND_LINE_VALUE_REQUIRED, "<password>", NULL, NULL, -1, NULL,
774  "Specify certificate export password" },
775 
776  /* Basic Options */
777 
778  { "n", COMMAND_LINE_VALUE_REQUIRED, "<name>", NULL, NULL, -1, NULL,
779  "Specifies the subject's certificate name. This name must conform to the X.500 standard. "
780  "The simplest method is to specify the name in double quotes, preceded by CN=; for "
781  "example, "
782  "-n \"CN=myName\"." },
783  { "pe", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
784  "Unsupported - Marks the generated private key as exportable. This allows the private "
785  "key to "
786  "be included in the certificate." },
787  { "sk", COMMAND_LINE_VALUE_REQUIRED, "<keyname>", NULL, NULL, -1, NULL,
788  "Unsupported - Specifies the subject's key container location, which contains the "
789  "private "
790  "key. "
791  "If a key container does not exist, it will be created." },
792  { "sr", COMMAND_LINE_VALUE_REQUIRED, "<location>", NULL, NULL, -1, NULL,
793  "Unsupported - Specifies the subject's certificate store location. location can be "
794  "either "
795  "currentuser (the default) or localmachine." },
796  { "ss", COMMAND_LINE_VALUE_REQUIRED, "<store>", NULL, NULL, -1, NULL,
797  "Unsupported - Specifies the subject's certificate store name that stores the output "
798  "certificate." },
799  { "#", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
800  "Specifies a serial number from 1 to 2,147,483,647. The default is a unique value "
801  "generated "
802  "by Makecert.exe." },
803  { "$", COMMAND_LINE_VALUE_REQUIRED, "<authority>", NULL, NULL, -1, NULL,
804  "Unsupported - Specifies the signing authority of the certificate, which must be set to "
805  "either commercial "
806  "(for certificates used by commercial software publishers) or individual (for "
807  "certificates "
808  "used by individual software publishers)." },
809 
810  /* Extended Options */
811 
812  { "a", COMMAND_LINE_VALUE_REQUIRED, "<algorithm>", NULL, NULL, -1, NULL,
813  "Specifies the signature algorithm. algorithm must be md5, sha1, sha256 (the default), "
814  "sha384, or sha512." },
815  { "b", COMMAND_LINE_VALUE_REQUIRED, "<mm/dd/yyyy>", NULL, NULL, -1, NULL,
816  "Unsupported - Specifies the start of the validity period. Defaults to the current "
817  "date." },
818  { "crl", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
819  "Unsupported - Generates a certificate relocation list (CRL) instead of a certificate." },
820  { "cy", COMMAND_LINE_VALUE_REQUIRED, "<certType>", NULL, NULL, -1, NULL,
821  "Unsupported - Specifies the certificate type. Valid values are end for end-entity and "
822  "authority for certification authority." },
823  { "e", COMMAND_LINE_VALUE_REQUIRED, "<mm/dd/yyyy>", NULL, NULL, -1, NULL,
824  "Unsupported - Specifies the end of the validity period. Defaults to 12/31/2039 11:59:59 "
825  "GMT." },
826  { "eku", COMMAND_LINE_VALUE_REQUIRED, "<oid[,oid…]>", NULL, NULL, -1, NULL,
827  "Unsupported - Inserts a list of comma-separated, enhanced key usage object identifiers "
828  "(OIDs) into the certificate." },
829  { "h", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
830  "Unsupported - Specifies the maximum height of the tree below this certificate." },
831  { "ic", COMMAND_LINE_VALUE_REQUIRED, "<file>", NULL, NULL, -1, NULL,
832  "Unsupported - Specifies the issuer's certificate file." },
833  { "ik", COMMAND_LINE_VALUE_REQUIRED, "<keyName>", NULL, NULL, -1, NULL,
834  "Unsupported - Specifies the issuer's key container name." },
835  { "iky", COMMAND_LINE_VALUE_REQUIRED, "<keyType>", NULL, NULL, -1, NULL,
836  "Unsupported - Specifies the issuer's key type, which must be one of the following: "
837  "signature (which indicates that the key is used for a digital signature), "
838  "exchange (which indicates that the key is used for key encryption and key exchange), "
839  "or an integer that represents a provider type. "
840  "By default, you can pass 1 for an exchange key or 2 for a signature key." },
841  { "in", COMMAND_LINE_VALUE_REQUIRED, "<name>", NULL, NULL, -1, NULL,
842  "Unsupported - Specifies the issuer's certificate common name." },
843  { "ip", COMMAND_LINE_VALUE_REQUIRED, "<provider>", NULL, NULL, -1, NULL,
844  "Unsupported - Specifies the issuer's CryptoAPI provider name. For information about the "
845  "CryptoAPI provider name, see the –sp option." },
846  { "ir", COMMAND_LINE_VALUE_REQUIRED, "<location>", NULL, NULL, -1, NULL,
847  "Unsupported - Specifies the location of the issuer's certificate store. location can be "
848  "either currentuser (the default) or localmachine." },
849  { "is", COMMAND_LINE_VALUE_REQUIRED, "<store>", NULL, NULL, -1, NULL,
850  "Unsupported - Specifies the issuer's certificate store name." },
851  { "iv", COMMAND_LINE_VALUE_REQUIRED, "<pvkFile>", NULL, NULL, -1, NULL,
852  "Unsupported - Specifies the issuer's .pvk private key file." },
853  { "iy", COMMAND_LINE_VALUE_REQUIRED, "<type>", NULL, NULL, -1, NULL,
854  "Unsupported - Specifies the issuer's CryptoAPI provider type. For information about the "
855  "CryptoAPI provider type, see the –sy option." },
856  { "l", COMMAND_LINE_VALUE_REQUIRED, "<link>", NULL, NULL, -1, NULL,
857  "Unsupported - Links to policy information (for example, to a URL)." },
858  { "len", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
859  "Specifies the generated key length, in bits." },
860  { "m", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
861  "Specifies the duration, in months, of the certificate validity period." },
862  { "y", COMMAND_LINE_VALUE_REQUIRED, "<number>", NULL, NULL, -1, NULL,
863  "Specifies the duration, in years, of the certificate validity period." },
864  { "nscp", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
865  "Unsupported - Includes the Netscape client-authorization extension." },
866  { "r", COMMAND_LINE_VALUE_FLAG, NULL, NULL, NULL, -1, NULL,
867  "Unsupported - Creates a self-signed certificate." },
868  { "sc", COMMAND_LINE_VALUE_REQUIRED, "<file>", NULL, NULL, -1, NULL,
869  "Unsupported - Specifies the subject's certificate file." },
870  { "sky", COMMAND_LINE_VALUE_REQUIRED, "<keyType>", NULL, NULL, -1, NULL,
871  "Unsupported - Specifies the subject's key type, which must be one of the following: "
872  "signature (which indicates that the key is used for a digital signature), "
873  "exchange (which indicates that the key is used for key encryption and key exchange), "
874  "or an integer that represents a provider type. "
875  "By default, you can pass 1 for an exchange key or 2 for a signature key." },
876  { "sp", COMMAND_LINE_VALUE_REQUIRED, "<provider>", NULL, NULL, -1, NULL,
877  "Unsupported - Specifies the subject's CryptoAPI provider name, which must be defined in "
878  "the "
879  "registry subkeys of "
880  "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider. If both –sp "
881  "and "
882  "–sy are present, "
883  "the type of the CryptoAPI provider must correspond to the Type value of the provider's "
884  "subkey." },
885  { "sv", COMMAND_LINE_VALUE_REQUIRED, "<pvkFile>", NULL, NULL, -1, NULL,
886  "Unsupported - Specifies the subject's .pvk private key file. The file is created if "
887  "none "
888  "exists." },
889  { "sy", COMMAND_LINE_VALUE_REQUIRED, "<type>", NULL, NULL, -1, NULL,
890  "Unsupported - Specifies the subject's CryptoAPI provider type, which must be defined in "
891  "the "
892  "registry subkeys of "
893  "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types. If "
894  "both "
895  "–sy and –sp are present, "
896  "the name of the CryptoAPI provider must correspond to the Name value of the provider "
897  "type "
898  "subkey." },
899  { "tbs", COMMAND_LINE_VALUE_REQUIRED, "<file>", NULL, NULL, -1, NULL,
900  "Unsupported - Specifies the certificate or CRL file to be signed." },
901 
902  /* Help */
903 
904  { "?", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1, "help",
905  "print help" },
906  { "!", COMMAND_LINE_VALUE_FLAG | COMMAND_LINE_PRINT_HELP, NULL, NULL, NULL, -1, "help-ext",
907  "print extended help" },
908  { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
909  };
910 #ifdef WITH_OPENSSL
911  size_t length = 0;
912  char* entry = NULL;
913  int key_length = 0;
914  long serial = 0;
915  X509_NAME* name = NULL;
916  const EVP_MD* md = NULL;
917  const COMMAND_LINE_ARGUMENT_A* arg = NULL;
918  int ret = 0;
919  ret = makecert_context_parse_arguments(context, args, argc, argv);
920 
921  if (ret < 1)
922  {
923  return ret;
924  }
925 
926  if (!context->default_name && !context->common_name)
927  {
928  context->default_name = x509_get_default_name();
929 
930  if (!context->default_name)
931  return -1;
932  }
933  else
934  {
935  context->default_name = _strdup(context->common_name);
936 
937  if (!context->default_name)
938  return -1;
939  }
940 
941  if (!context->common_name)
942  {
943  context->common_name = _strdup(context->default_name);
944 
945  if (!context->common_name)
946  return -1;
947  }
948 
949  if (!context->pkey)
950  context->pkey = EVP_PKEY_new();
951 
952  if (!context->pkey)
953  return -1;
954 
955  if (!context->x509)
956  context->x509 = X509_new();
957 
958  if (!context->x509)
959  return -1;
960 
961  key_length = 2048;
962  arg = CommandLineFindArgumentA(args, "len");
963 
964  if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
965  {
966  unsigned long val = strtoul(arg->Value, NULL, 0);
967 
968  if ((errno != 0) || (val > INT_MAX))
969  return -1;
970  key_length = (int)val;
971  }
972 
973  if (!makecert_create_rsa(&context->pkey, key_length))
974  return -1;
975 
976  X509_set_version(context->x509, 2);
977  arg = CommandLineFindArgumentA(args, "#");
978 
979  if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
980  {
981  serial = strtol(arg->Value, NULL, 0);
982 
983  if (errno != 0)
984  return -1;
985  }
986  else
987  serial = (long)GetTickCount64();
988 
989  ASN1_INTEGER_set(X509_get_serialNumber(context->x509), serial);
990  {
991  ASN1_TIME* before = NULL;
992  ASN1_TIME* after = NULL;
993 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
994  before = X509_get_notBefore(context->x509);
995  after = X509_get_notAfter(context->x509);
996 #else
997  before = X509_getm_notBefore(context->x509);
998  after = X509_getm_notAfter(context->x509);
999 #endif
1000  X509_gmtime_adj(before, 0);
1001 
1002  long duration = context->duration_months * 31l + context->duration_years * 365l;
1003  duration *= 60l * 60l * 24l;
1004  X509_gmtime_adj(after, duration);
1005  }
1006  X509_set_pubkey(context->x509, context->pkey);
1007  name = X509_get_subject_name(context->x509);
1008  arg = CommandLineFindArgumentA(args, "n");
1009 
1010  if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1011  {
1012  entry = x509_name_parse(arg->Value, "C", &length);
1013 
1014  if (entry)
1015  X509_NAME_add_entry_by_txt(name, "C", MBSTRING_UTF8, (const unsigned char*)entry,
1016  (int)length, -1, 0);
1017 
1018  entry = x509_name_parse(arg->Value, "ST", &length);
1019 
1020  if (entry)
1021  X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_UTF8, (const unsigned char*)entry,
1022  (int)length, -1, 0);
1023 
1024  entry = x509_name_parse(arg->Value, "L", &length);
1025 
1026  if (entry)
1027  X509_NAME_add_entry_by_txt(name, "L", MBSTRING_UTF8, (const unsigned char*)entry,
1028  (int)length, -1, 0);
1029 
1030  entry = x509_name_parse(arg->Value, "O", &length);
1031 
1032  if (entry)
1033  X509_NAME_add_entry_by_txt(name, "O", MBSTRING_UTF8, (const unsigned char*)entry,
1034  (int)length, -1, 0);
1035 
1036  entry = x509_name_parse(arg->Value, "OU", &length);
1037 
1038  if (entry)
1039  X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8, (const unsigned char*)entry,
1040  (int)length, -1, 0);
1041 
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);
1046  }
1047  else
1048  {
1049  entry = context->common_name;
1050  length = strlen(entry);
1051  X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_UTF8, (const unsigned char*)entry,
1052  (int)length, -1, 0);
1053  }
1054 
1055  X509_set_issuer_name(context->x509, name);
1056  x509_add_ext(context->x509, NID_ext_key_usage, "serverAuth");
1057  arg = CommandLineFindArgumentA(args, "a");
1058  md = EVP_sha256();
1059 
1060  if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
1061  {
1062  md = EVP_get_digestbyname(arg->Value);
1063  if (!md)
1064  return -1;
1065  }
1066 
1067  if (!X509_sign(context->x509, context->pkey, md))
1068  return -1;
1069 
1074  if (!context->silent)
1075  {
1076  BIO* bio = NULL;
1077  int status = 0;
1078  char* x509_str = NULL;
1079  bio = BIO_new(BIO_s_mem());
1080 
1081  if (!bio)
1082  return -1;
1083 
1084  status = X509_print(bio, context->x509);
1085 
1086  if (status < 0)
1087  {
1088  BIO_free_all(bio);
1089  return -1;
1090  }
1091 
1092  x509_str = makecert_read_str(bio, NULL);
1093  if (!x509_str)
1094  {
1095  BIO_free_all(bio);
1096  return -1;
1097  }
1098 
1099  printf("%s", x509_str);
1100  free(x509_str);
1101  BIO_free_all(bio);
1102  }
1103 
1108  if (!context->live)
1109  {
1110  if (!winpr_PathFileExists(context->output_path))
1111  {
1112  if (!CreateDirectoryA(context->output_path, NULL))
1113  return -1;
1114  }
1115 
1116  if (makecert_context_output_certificate_file(context, context->output_path) != 1)
1117  return -1;
1118 
1119  if (context->crtFormat)
1120  {
1121  if (makecert_context_output_private_key_file(context, context->output_path) < 0)
1122  return -1;
1123  }
1124  }
1125 
1126  return 0;
1127 #else
1128  WLog_ERR(TAG, "%s only supported with OpenSSL", __func__);
1129  return -1;
1130 #endif
1131 }
1132 
1133 MAKECERT_CONTEXT* makecert_context_new(void)
1134 {
1135  MAKECERT_CONTEXT* context = (MAKECERT_CONTEXT*)calloc(1, sizeof(MAKECERT_CONTEXT));
1136 
1137  if (context)
1138  {
1139  context->crtFormat = TRUE;
1140  context->duration_years = 1;
1141  }
1142 
1143  return context;
1144 }
1145 
1146 void makecert_context_free(MAKECERT_CONTEXT* context)
1147 {
1148  if (context)
1149  {
1150  free(context->password);
1151  free(context->default_name);
1152  free(context->common_name);
1153  free(context->output_file);
1154  free(context->output_path);
1155 #ifdef WITH_OPENSSL
1156  X509_free(context->x509);
1157  EVP_PKEY_free(context->pkey);
1158 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1159  CRYPTO_cleanup_all_ex_data();
1160 #endif
1161 #endif
1162  free(context);
1163  }
1164 }