|
| 1 | +/* |
| 2 | + * Copyright © 2025 Mobi - Com Polska Sp. z o.o. |
| 3 | + * Author: Małgorzata Olszówka <[email protected]> |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * PKCS#11 provider test |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in the |
| 15 | + * documentation and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | + * SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +#include "helpers_prov.h" |
| 31 | + |
| 32 | +#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 33 | + |
| 34 | +int main(int argc, char *argv[]) |
| 35 | +{ |
| 36 | + OBJ_SET *obj_set; |
| 37 | + int ret = EXIT_FAILURE; |
| 38 | + |
| 39 | + if (argc < 1) { |
| 40 | + fprintf(stderr, "usage: %s [object URL]\n", argv[0]); |
| 41 | + return ret; |
| 42 | + } |
| 43 | + |
| 44 | + obj_set = OPENSSL_zalloc(sizeof(OBJ_SET)); |
| 45 | + if (!obj_set) |
| 46 | + return ret; |
| 47 | + |
| 48 | + /* Load pkcs11prov and default providers */ |
| 49 | + if (!providers_load()) { |
| 50 | + display_openssl_errors(); |
| 51 | + return ret; |
| 52 | + } |
| 53 | + |
| 54 | + /* Load private key, public key and certificate */ |
| 55 | + load_objects(argv[1], NULL, obj_set); |
| 56 | + |
| 57 | + if (!obj_set->private_key) { |
| 58 | + printf("Cannot load private key: %s\n", argv[1]); |
| 59 | + goto cleanup; |
| 60 | + } |
| 61 | + if (!obj_set->public_key) { |
| 62 | + printf("Cannot load public key: %s\n", argv[1]); |
| 63 | + goto cleanup; |
| 64 | + } |
| 65 | + if (!obj_set->cert) { |
| 66 | + printf("Cannot load certificate: %s\n", argv[1]); |
| 67 | + goto cleanup; |
| 68 | + } |
| 69 | + ret = X509_check_private_key(obj_set->cert, obj_set->private_key); |
| 70 | + if (!ret) { |
| 71 | + printf("Could not check private key.\n"); |
| 72 | + display_openssl_errors(); |
| 73 | + goto cleanup; |
| 74 | + } |
| 75 | + printf("Key and certificate matched.\n"); |
| 76 | + ret = EXIT_SUCCESS; |
| 77 | + |
| 78 | +cleanup: |
| 79 | + EVP_PKEY_free(obj_set->private_key); |
| 80 | + EVP_PKEY_free(obj_set->public_key); |
| 81 | + X509_free(obj_set->cert); |
| 82 | + OPENSSL_free(obj_set); |
| 83 | + providers_cleanup(); |
| 84 | + printf("\n"); |
| 85 | + return ret; |
| 86 | +} |
| 87 | + |
| 88 | +#else |
| 89 | + |
| 90 | +int main() { |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ |
| 95 | + |
| 96 | +/* vim: set noexpandtab: */ |
0 commit comments