ts_lib.c 2.4 KB

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