|
| 1 | +/* certvfy.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2024 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | + |
| 24 | +#ifdef HAVE_CONFIG_H |
| 25 | + #include <config.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +#ifndef WOLFSSL_USER_SETTINGS |
| 29 | + #include <wolfssl/options.h> |
| 30 | +#endif |
| 31 | +#include <wolfssl/wolfcrypt/settings.h> |
| 32 | + |
| 33 | +#include <wolfssl/wolfcrypt/asn_public.h> |
| 34 | +#include <wolfssl/wolfcrypt/asn.h> |
| 35 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 36 | + |
| 37 | +#define MAX_DER_SZ 4096 |
| 38 | + |
| 39 | +int load_file(const char* name, byte* buf, int bufSz) |
| 40 | +{ |
| 41 | + FILE* file; |
| 42 | + |
| 43 | + file = fopen(name, "rb"); |
| 44 | + if (file == NULL) { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + bufSz = fread(buf, 1, bufSz, file); |
| 48 | + fclose(file); |
| 49 | + |
| 50 | + return bufSz; |
| 51 | +} |
| 52 | + |
| 53 | +int main(void) |
| 54 | +{ |
| 55 | + int res = 0; |
| 56 | + int ret; |
| 57 | + |
| 58 | + const char* caCert = "../certs/ca-cert.der"; |
| 59 | + const char* verifyCert = "../certs/server-cert.der"; |
| 60 | + |
| 61 | + byte caDer[MAX_DER_SZ]; |
| 62 | + int caDerSz; |
| 63 | + byte certDer[MAX_DER_SZ]; |
| 64 | + int certDerSz; |
| 65 | + |
| 66 | + DecodedCert ca; |
| 67 | + Signer caSigner; |
| 68 | + DecodedCert cert; |
| 69 | + |
| 70 | + XMEMSET(&ca, 0, sizeof(ca)); |
| 71 | + XMEMSET(&caSigner, 0, sizeof(caSigner)); |
| 72 | + XMEMSET(&cert, 0, sizeof(cert)); |
| 73 | + |
| 74 | + wolfCrypt_Init(); |
| 75 | + |
| 76 | + /* Load the DER encoded CA certificate. */ |
| 77 | + caDerSz = load_file(caCert, caDer, (int)sizeof(caDer)); |
| 78 | + if (caDerSz == 0) { |
| 79 | + printf("Failed to load CA file\n"); |
| 80 | + res = 1; |
| 81 | + goto exit; |
| 82 | + } |
| 83 | + |
| 84 | + /* Put the CA certificate data into the object. */ |
| 85 | + wc_InitDecodedCert(&ca, caDer, caDerSz, NULL); |
| 86 | + /* Parse fields of the certificate. */ |
| 87 | + ret = wc_ParseCert(&ca, CERT_TYPE, 0, NULL); |
| 88 | + if (ret != 0) { |
| 89 | + printf("Parsing CA failed: %s (%d)\n", wc_GetErrorString(ret), ret); |
| 90 | + res = 1; |
| 91 | + goto exit; |
| 92 | + } |
| 93 | + /* Put fields into CA signer object. */ |
| 94 | + caSigner.publicKey = ca.publicKey; |
| 95 | + caSigner.pubKeySize = ca.pubKeySize; |
| 96 | + caSigner.keyOID = ca.keyOID; |
| 97 | + XMEMCPY(caSigner.subjectNameHash, ca.subjectHash, KEYID_SIZE); |
| 98 | + |
| 99 | + /* Load the DER encoded certificate to verify. */ |
| 100 | + certDerSz = load_file(verifyCert, certDer, (int)sizeof(certDer)); |
| 101 | + if (certDerSz == 0) { |
| 102 | + printf("Failed to load certificate file\n"); |
| 103 | + res = 1; |
| 104 | + goto exit; |
| 105 | + } |
| 106 | + |
| 107 | + /* Put the certificate data into the object. */ |
| 108 | + wc_InitDecodedCert(&cert, certDer, certDerSz, NULL); |
| 109 | + /* Parse and verify the certificate. */ |
| 110 | + ret = wc_ParseCert(&cert, CERT_TYPE, 1, &caSigner); |
| 111 | + if (ret != 0) { |
| 112 | + printf("Verification failed: %s (%d)\n", wc_GetErrorString(ret), ret); |
| 113 | + res = 1; |
| 114 | + goto exit; |
| 115 | + } |
| 116 | + printf("Verification Successful!\n"); |
| 117 | + |
| 118 | +exit: |
| 119 | + wc_FreeDecodedCert(&cert); |
| 120 | + wc_FreeDecodedCert(&ca); |
| 121 | + wolfCrypt_Cleanup(); |
| 122 | + return res; |
| 123 | +} |
| 124 | + |
0 commit comments