cmp_testlib.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2007-2020 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. EVP_PKEY *load_pem_key(const char *file, OSSL_LIB_CTX *libctx)
  14. {
  15. EVP_PKEY *key = NULL;
  16. BIO *bio = NULL;
  17. if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
  18. return NULL;
  19. if (TEST_int_gt(BIO_read_filename(bio, file), 0))
  20. (void)TEST_ptr(key = PEM_read_bio_PrivateKey_ex(bio, NULL, NULL, NULL,
  21. libctx, NULL));
  22. BIO_free(bio);
  23. return key;
  24. }
  25. X509 *load_pem_cert(const char *file, OSSL_LIB_CTX *libctx)
  26. {
  27. X509 *cert = NULL;
  28. BIO *bio = NULL;
  29. if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
  30. return NULL;
  31. if (TEST_int_gt(BIO_read_filename(bio, file), 0)
  32. && TEST_ptr(cert = X509_new_ex(libctx, NULL)))
  33. (void)TEST_ptr(cert = PEM_read_bio_X509(bio, &cert, NULL, NULL));
  34. BIO_free(bio);
  35. return cert;
  36. }
  37. OSSL_CMP_MSG *load_pkimsg(const char *file)
  38. {
  39. OSSL_CMP_MSG *msg;
  40. (void)TEST_ptr((msg = OSSL_CMP_MSG_read(file)));
  41. return msg;
  42. }
  43. X509_REQ *load_csr(const char *file)
  44. {
  45. X509_REQ *csr = NULL;
  46. BIO *bio = NULL;
  47. if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new_file(file, "rb")))
  48. return NULL;
  49. (void)TEST_ptr(csr = d2i_X509_REQ_bio(bio, NULL));
  50. BIO_free(bio);
  51. return csr;
  52. }
  53. /*
  54. * Checks whether the syntax of msg conforms to ASN.1
  55. */
  56. int valid_asn1_encoding(const OSSL_CMP_MSG *msg)
  57. {
  58. return msg != NULL ? i2d_OSSL_CMP_MSG(msg, NULL) > 0 : 0;
  59. }
  60. /*
  61. * Compares two stacks of certificates in the order of their elements.
  62. * Returns 0 if sk1 and sk2 are equal and another value otherwise
  63. */
  64. int STACK_OF_X509_cmp(const STACK_OF(X509) *sk1, const STACK_OF(X509) *sk2)
  65. {
  66. int i, res;
  67. X509 *a, *b;
  68. if (sk1 == sk2)
  69. return 0;
  70. if (sk1 == NULL)
  71. return -1;
  72. if (sk2 == NULL)
  73. return 1;
  74. if ((res = sk_X509_num(sk1) - sk_X509_num(sk2)))
  75. return res;
  76. for (i = 0; i < sk_X509_num(sk1); i++) {
  77. a = sk_X509_value(sk1, i);
  78. b = sk_X509_value(sk2, i);
  79. if (a != b)
  80. if ((res = X509_cmp(a, b)) != 0)
  81. return res;
  82. }
  83. return 0;
  84. }
  85. /*
  86. * Up refs and push a cert onto sk.
  87. * Returns the number of certificates on the stack on success
  88. * Returns -1 or 0 on error
  89. */
  90. int STACK_OF_X509_push1(STACK_OF(X509) *sk, X509 *cert)
  91. {
  92. int res;
  93. if (sk == NULL || cert == NULL)
  94. return -1;
  95. if (!X509_up_ref(cert))
  96. return -1;
  97. res = sk_X509_push(sk, cert);
  98. if (res <= 0)
  99. X509_free(cert); /* down-ref */
  100. return res;
  101. }
  102. int print_to_bio_out(const char *func, const char *file, int line,
  103. OSSL_CMP_severity level, const char *msg)
  104. {
  105. return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg);
  106. }