nseq.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_ERR = -1, OPT_EOF = 0, OPT_HELP,
  17. OPT_TOSEQ, OPT_IN, OPT_OUT
  18. } OPTION_CHOICE;
  19. const OPTIONS nseq_options[] = {
  20. {"help", OPT_HELP, '-', "Display this summary"},
  21. {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
  22. {"in", OPT_IN, '<', "Input file"},
  23. {"out", OPT_OUT, '>', "Output file"},
  24. {NULL}
  25. };
  26. int nseq_main(int argc, char **argv)
  27. {
  28. BIO *in = NULL, *out = NULL;
  29. X509 *x509 = NULL;
  30. NETSCAPE_CERT_SEQUENCE *seq = NULL;
  31. OPTION_CHOICE o;
  32. int toseq = 0, ret = 1, i;
  33. char *infile = NULL, *outfile = NULL, *prog;
  34. prog = opt_init(argc, argv, nseq_options);
  35. while ((o = opt_next()) != OPT_EOF) {
  36. switch (o) {
  37. case OPT_EOF:
  38. case OPT_ERR:
  39. opthelp:
  40. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  41. goto end;
  42. case OPT_HELP:
  43. ret = 0;
  44. opt_help(nseq_options);
  45. goto end;
  46. case OPT_TOSEQ:
  47. toseq = 1;
  48. break;
  49. case OPT_IN:
  50. infile = opt_arg();
  51. break;
  52. case OPT_OUT:
  53. outfile = opt_arg();
  54. break;
  55. }
  56. }
  57. argc = opt_num_rest();
  58. if (argc != 0)
  59. goto opthelp;
  60. in = bio_open_default(infile, 'r', FORMAT_PEM);
  61. if (in == NULL)
  62. goto end;
  63. out = bio_open_default(outfile, 'w', FORMAT_PEM);
  64. if (out == NULL)
  65. goto end;
  66. if (toseq) {
  67. seq = NETSCAPE_CERT_SEQUENCE_new();
  68. if (seq == NULL)
  69. goto end;
  70. seq->certs = sk_X509_new_null();
  71. if (seq->certs == NULL)
  72. goto end;
  73. while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
  74. sk_X509_push(seq->certs, x509);
  75. if (!sk_X509_num(seq->certs)) {
  76. BIO_printf(bio_err, "%s: Error reading certs file %s\n",
  77. prog, infile);
  78. ERR_print_errors(bio_err);
  79. goto end;
  80. }
  81. PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
  82. ret = 0;
  83. goto end;
  84. }
  85. seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
  86. if (seq == NULL) {
  87. BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
  88. prog, infile);
  89. ERR_print_errors(bio_err);
  90. goto end;
  91. }
  92. for (i = 0; i < sk_X509_num(seq->certs); i++) {
  93. x509 = sk_X509_value(seq->certs, i);
  94. dump_cert_text(out, x509);
  95. PEM_write_bio_X509(out, x509);
  96. }
  97. ret = 0;
  98. end:
  99. BIO_free(in);
  100. BIO_free_all(out);
  101. NETSCAPE_CERT_SEQUENCE_free(seq);
  102. return ret;
  103. }