cmp_testlib.c 3.0 KB

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