nseq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "apps.h"
  12. #include "progs.h"
  13. #include <openssl/pem.h>
  14. #include <openssl/err.h>
  15. typedef enum OPTION_choice {
  16. OPT_COMMON,
  17. OPT_TOSEQ, OPT_IN, OPT_OUT,
  18. OPT_PROV_ENUM
  19. } OPTION_CHOICE;
  20. const OPTIONS nseq_options[] = {
  21. OPT_SECTION("General"),
  22. {"help", OPT_HELP, '-', "Display this summary"},
  23. OPT_SECTION("Input"),
  24. {"in", OPT_IN, '<', "Input file"},
  25. OPT_SECTION("Output"),
  26. {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
  27. {"out", OPT_OUT, '>', "Output file"},
  28. OPT_PROV_OPTIONS,
  29. {NULL}
  30. };
  31. int nseq_main(int argc, char **argv)
  32. {
  33. BIO *in = NULL, *out = NULL;
  34. X509 *x509 = NULL;
  35. NETSCAPE_CERT_SEQUENCE *seq = NULL;
  36. OPTION_CHOICE o;
  37. int toseq = 0, ret = 1, i;
  38. char *infile = NULL, *outfile = NULL, *prog;
  39. prog = opt_init(argc, argv, nseq_options);
  40. while ((o = opt_next()) != OPT_EOF) {
  41. switch (o) {
  42. case OPT_EOF:
  43. case OPT_ERR:
  44. opthelp:
  45. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  46. goto end;
  47. case OPT_HELP:
  48. ret = 0;
  49. opt_help(nseq_options);
  50. goto end;
  51. case OPT_TOSEQ:
  52. toseq = 1;
  53. break;
  54. case OPT_IN:
  55. infile = opt_arg();
  56. break;
  57. case OPT_OUT:
  58. outfile = opt_arg();
  59. break;
  60. case OPT_PROV_CASES:
  61. if (!opt_provider(o))
  62. goto end;
  63. break;
  64. }
  65. }
  66. /* No extra arguments. */
  67. argc = opt_num_rest();
  68. if (argc != 0)
  69. goto opthelp;
  70. in = bio_open_default(infile, 'r', FORMAT_PEM);
  71. if (in == NULL)
  72. goto end;
  73. out = bio_open_default(outfile, 'w', FORMAT_PEM);
  74. if (out == NULL)
  75. goto end;
  76. if (toseq) {
  77. seq = NETSCAPE_CERT_SEQUENCE_new();
  78. if (seq == NULL)
  79. goto end;
  80. seq->certs = sk_X509_new_null();
  81. if (seq->certs == NULL)
  82. goto end;
  83. while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  84. if (!sk_X509_push(seq->certs, x509))
  85. goto end;
  86. }
  87. if (!sk_X509_num(seq->certs)) {
  88. BIO_printf(bio_err, "%s: Error reading certs file %s\n",
  89. prog, infile);
  90. ERR_print_errors(bio_err);
  91. goto end;
  92. }
  93. PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
  94. ret = 0;
  95. goto end;
  96. }
  97. seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
  98. if (seq == NULL) {
  99. BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
  100. prog, infile);
  101. ERR_print_errors(bio_err);
  102. goto end;
  103. }
  104. for (i = 0; i < sk_X509_num(seq->certs); i++) {
  105. x509 = sk_X509_value(seq->certs, i);
  106. dump_cert_text(out, x509);
  107. PEM_write_bio_X509(out, x509);
  108. }
  109. ret = 0;
  110. end:
  111. BIO_free(in);
  112. BIO_free_all(out);
  113. NETSCAPE_CERT_SEQUENCE_free(seq);
  114. return ret;
  115. }