ts_lib.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2006-2016 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 "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/x509v3.h>
  15. #include <openssl/ts.h>
  16. #include "ts_lcl.h"
  17. int TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num)
  18. {
  19. BIGNUM *num_bn;
  20. int result = 0;
  21. char *hex;
  22. num_bn = BN_new();
  23. if (num_bn == NULL)
  24. return -1;
  25. ASN1_INTEGER_to_BN(num, num_bn);
  26. if ((hex = BN_bn2hex(num_bn))) {
  27. result = BIO_write(bio, "0x", 2) > 0;
  28. result = result && BIO_write(bio, hex, strlen(hex)) > 0;
  29. OPENSSL_free(hex);
  30. }
  31. BN_free(num_bn);
  32. return result;
  33. }
  34. int TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj)
  35. {
  36. char obj_txt[128];
  37. OBJ_obj2txt(obj_txt, sizeof(obj_txt), obj, 0);
  38. BIO_printf(bio, "%s\n", obj_txt);
  39. return 1;
  40. }
  41. int TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions)
  42. {
  43. int i, critical, n;
  44. X509_EXTENSION *ex;
  45. ASN1_OBJECT *obj;
  46. BIO_printf(bio, "Extensions:\n");
  47. n = X509v3_get_ext_count(extensions);
  48. for (i = 0; i < n; i++) {
  49. ex = X509v3_get_ext(extensions, i);
  50. obj = X509_EXTENSION_get_object(ex);
  51. if (i2a_ASN1_OBJECT(bio, obj) < 0)
  52. return 0;
  53. critical = X509_EXTENSION_get_critical(ex);
  54. BIO_printf(bio, ":%s\n", critical ? " critical" : "");
  55. if (!X509V3_EXT_print(bio, ex, 0, 4)) {
  56. BIO_printf(bio, "%4s", "");
  57. ASN1_STRING_print(bio, X509_EXTENSION_get_data(ex));
  58. }
  59. BIO_write(bio, "\n", 1);
  60. }
  61. return 1;
  62. }
  63. int TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg)
  64. {
  65. int i = OBJ_obj2nid(alg->algorithm);
  66. return BIO_printf(bio, "Hash Algorithm: %s\n",
  67. (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
  68. }
  69. int TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *a)
  70. {
  71. ASN1_OCTET_STRING *msg;
  72. TS_X509_ALGOR_print_bio(bio, a->hash_algo);
  73. BIO_printf(bio, "Message data:\n");
  74. msg = a->hashed_msg;
  75. BIO_dump_indent(bio, (const char *)ASN1_STRING_get0_data(msg),
  76. ASN1_STRING_length(msg), 4);
  77. return 1;
  78. }