cmp_testlib.c 3.4 KB

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