xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/x509_dup_cert_test.c (revision 132cc1c4aeae88a30a5e34dc141e119eb83f8d7f)
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /* ====================================================================
11  * Copyright (c) 2017, 2018 Oracle and/or its affiliates.  All rights reserved.
12  */
13 
14 #include <stdio.h>
15 #include <openssl/err.h>
16 #include <openssl/x509_vfy.h>
17 
18 static int test_509_dup_cert(const char *cert_f)
19 {
20     int ret = 0;
21     X509_STORE_CTX *sctx = NULL;
22     X509_STORE *store = NULL;
23     X509_LOOKUP *lookup = NULL;
24 
25     store = X509_STORE_new();
26     if (store == NULL)
27         goto err;
28 
29     lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
30     if (lookup == NULL)
31         goto err;
32 
33     if (!X509_load_cert_file(lookup, cert_f, X509_FILETYPE_PEM))
34         goto err;
35     if (!X509_load_cert_file(lookup, cert_f, X509_FILETYPE_PEM))
36         goto err;
37 
38     ret = 1;
39 
40  err:
41     X509_STORE_CTX_free(sctx);
42     X509_STORE_free(store);
43     if (ret != 1)
44         ERR_print_errors_fp(stderr);
45     return ret;
46 }
47 
48 int main(int argc, char **argv)
49 {
50     CRYPTO_set_mem_debug(1);
51     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
52 
53     if (argc != 2) {
54         fprintf(stderr, "usage: x509_dup_cert_test cert.pem\n");
55         return 1;
56     }
57 
58     if (!test_509_dup_cert(argv[1])) {
59         fprintf(stderr, "Test X509 duplicate cert failed\n");
60         return 1;
61     }
62 
63 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
64     if (CRYPTO_mem_leaks_fp(stderr) <= 0)
65         return 1;
66 #endif
67 
68     printf("PASS\n");
69     return 0;
70 }
71