cmp_testlib.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include "cmp_testlib.h"
  12. #include <openssl/rsa.h> /* needed in case config no-deprecated */
  13. OSSL_CMP_MSG *load_pkimsg(const char *file, OSSL_LIB_CTX *libctx)
  14. {
  15. OSSL_CMP_MSG *msg;
  16. (void)TEST_ptr((msg = OSSL_CMP_MSG_read(file, libctx, NULL)));
  17. return msg;
  18. }
  19. /*
  20. * Checks whether the syntax of msg conforms to ASN.1
  21. */
  22. int valid_asn1_encoding(const OSSL_CMP_MSG *msg)
  23. {
  24. return msg != NULL ? i2d_OSSL_CMP_MSG(msg, NULL) > 0 : 0;
  25. }
  26. /*
  27. * Compares two stacks of certificates in the order of their elements.
  28. * Returns 0 if sk1 and sk2 are equal and another value otherwise
  29. */
  30. int STACK_OF_X509_cmp(const STACK_OF(X509) *sk1, const STACK_OF(X509) *sk2)
  31. {
  32. int i, res;
  33. X509 *a, *b;
  34. if (sk1 == sk2)
  35. return 0;
  36. if (sk1 == NULL)
  37. return -1;
  38. if (sk2 == NULL)
  39. return 1;
  40. if ((res = sk_X509_num(sk1) - sk_X509_num(sk2)))
  41. return res;
  42. for (i = 0; i < sk_X509_num(sk1); i++) {
  43. a = sk_X509_value(sk1, i);
  44. b = sk_X509_value(sk2, i);
  45. if (a != b)
  46. if ((res = X509_cmp(a, b)) != 0)
  47. return res;
  48. }
  49. return 0;
  50. }
  51. /*
  52. * Up refs and push a cert onto sk.
  53. * Returns the number of certificates on the stack on success
  54. * Returns -1 or 0 on error
  55. */
  56. int STACK_OF_X509_push1(STACK_OF(X509) *sk, X509 *cert)
  57. {
  58. int res;
  59. if (sk == NULL || cert == NULL)
  60. return -1;
  61. if (!X509_up_ref(cert))
  62. return -1;
  63. res = sk_X509_push(sk, cert);
  64. if (res <= 0)
  65. X509_free(cert); /* down-ref */
  66. return res;
  67. }
  68. int print_to_bio_out(const char *func, const char *file, int line,
  69. OSSL_CMP_severity level, const char *msg)
  70. {
  71. return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg);
  72. }