ts_rsp_print.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2006-2020 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 "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/x509v3.h>
  14. #include <openssl/ts.h>
  15. #include "ts_local.h"
  16. DEFINE_STACK_OF(ASN1_UTF8STRING)
  17. DEFINE_STACK_OF(CONF_VALUE)
  18. struct status_map_st {
  19. int bit;
  20. const char *text;
  21. };
  22. static int ts_status_map_print(BIO *bio, const struct status_map_st *a,
  23. const ASN1_BIT_STRING *v);
  24. static int ts_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
  25. int TS_RESP_print_bio(BIO *bio, TS_RESP *a)
  26. {
  27. BIO_printf(bio, "Status info:\n");
  28. TS_STATUS_INFO_print_bio(bio, a->status_info);
  29. BIO_printf(bio, "\nTST info:\n");
  30. if (a->tst_info != NULL)
  31. TS_TST_INFO_print_bio(bio, a->tst_info);
  32. else
  33. BIO_printf(bio, "Not included.\n");
  34. return 1;
  35. }
  36. int TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
  37. {
  38. static const char *status_map[] = {
  39. "Granted.",
  40. "Granted with modifications.",
  41. "Rejected.",
  42. "Waiting.",
  43. "Revocation warning.",
  44. "Revoked."
  45. };
  46. static const struct status_map_st failure_map[] = {
  47. {TS_INFO_BAD_ALG,
  48. "unrecognized or unsupported algorithm identifier"},
  49. {TS_INFO_BAD_REQUEST,
  50. "transaction not permitted or supported"},
  51. {TS_INFO_BAD_DATA_FORMAT,
  52. "the data submitted has the wrong format"},
  53. {TS_INFO_TIME_NOT_AVAILABLE,
  54. "the TSA's time source is not available"},
  55. {TS_INFO_UNACCEPTED_POLICY,
  56. "the requested TSA policy is not supported by the TSA"},
  57. {TS_INFO_UNACCEPTED_EXTENSION,
  58. "the requested extension is not supported by the TSA"},
  59. {TS_INFO_ADD_INFO_NOT_AVAILABLE,
  60. "the additional information requested could not be understood "
  61. "or is not available"},
  62. {TS_INFO_SYSTEM_FAILURE,
  63. "the request cannot be handled due to system failure"},
  64. {-1, NULL}
  65. };
  66. long status;
  67. int i, lines = 0;
  68. BIO_printf(bio, "Status: ");
  69. status = ASN1_INTEGER_get(a->status);
  70. if (0 <= status && status < (long)OSSL_NELEM(status_map))
  71. BIO_printf(bio, "%s\n", status_map[status]);
  72. else
  73. BIO_printf(bio, "out of bounds\n");
  74. BIO_printf(bio, "Status description: ");
  75. for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
  76. if (i > 0)
  77. BIO_puts(bio, "\t");
  78. ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i), 0);
  79. BIO_puts(bio, "\n");
  80. }
  81. if (i == 0)
  82. BIO_printf(bio, "unspecified\n");
  83. BIO_printf(bio, "Failure info: ");
  84. if (a->failure_info != NULL)
  85. lines = ts_status_map_print(bio, failure_map, a->failure_info);
  86. if (lines == 0)
  87. BIO_printf(bio, "unspecified");
  88. BIO_printf(bio, "\n");
  89. return 1;
  90. }
  91. static int ts_status_map_print(BIO *bio, const struct status_map_st *a,
  92. const ASN1_BIT_STRING *v)
  93. {
  94. int lines = 0;
  95. for (; a->bit >= 0; ++a) {
  96. if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
  97. if (++lines > 1)
  98. BIO_printf(bio, ", ");
  99. BIO_printf(bio, "%s", a->text);
  100. }
  101. }
  102. return lines;
  103. }
  104. int TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
  105. {
  106. int v;
  107. if (a == NULL)
  108. return 0;
  109. v = ASN1_INTEGER_get(a->version);
  110. BIO_printf(bio, "Version: %d\n", v);
  111. BIO_printf(bio, "Policy OID: ");
  112. TS_OBJ_print_bio(bio, a->policy_id);
  113. TS_MSG_IMPRINT_print_bio(bio, a->msg_imprint);
  114. BIO_printf(bio, "Serial number: ");
  115. if (a->serial == NULL)
  116. BIO_printf(bio, "unspecified");
  117. else
  118. TS_ASN1_INTEGER_print_bio(bio, a->serial);
  119. BIO_write(bio, "\n", 1);
  120. BIO_printf(bio, "Time stamp: ");
  121. ASN1_GENERALIZEDTIME_print(bio, a->time);
  122. BIO_write(bio, "\n", 1);
  123. BIO_printf(bio, "Accuracy: ");
  124. if (a->accuracy == NULL)
  125. BIO_printf(bio, "unspecified");
  126. else
  127. ts_ACCURACY_print_bio(bio, a->accuracy);
  128. BIO_write(bio, "\n", 1);
  129. BIO_printf(bio, "Ordering: %s\n", a->ordering ? "yes" : "no");
  130. BIO_printf(bio, "Nonce: ");
  131. if (a->nonce == NULL)
  132. BIO_printf(bio, "unspecified");
  133. else
  134. TS_ASN1_INTEGER_print_bio(bio, a->nonce);
  135. BIO_write(bio, "\n", 1);
  136. BIO_printf(bio, "TSA: ");
  137. if (a->tsa == NULL)
  138. BIO_printf(bio, "unspecified");
  139. else {
  140. STACK_OF(CONF_VALUE) *nval;
  141. if ((nval = i2v_GENERAL_NAME(NULL, a->tsa, NULL)))
  142. X509V3_EXT_val_prn(bio, nval, 0, 0);
  143. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  144. }
  145. BIO_write(bio, "\n", 1);
  146. TS_ext_print_bio(bio, a->extensions);
  147. return 1;
  148. }
  149. static int ts_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *a)
  150. {
  151. if (a->seconds != NULL)
  152. TS_ASN1_INTEGER_print_bio(bio, a->seconds);
  153. else
  154. BIO_printf(bio, "unspecified");
  155. BIO_printf(bio, " seconds, ");
  156. if (a->millis != NULL)
  157. TS_ASN1_INTEGER_print_bio(bio, a->millis);
  158. else
  159. BIO_printf(bio, "unspecified");
  160. BIO_printf(bio, " millis, ");
  161. if (a->micros != NULL)
  162. TS_ASN1_INTEGER_print_bio(bio, a->micros);
  163. else
  164. BIO_printf(bio, "unspecified");
  165. BIO_printf(bio, " micros");
  166. return 1;
  167. }