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