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