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