|
| 1 | +/* smime-verify.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2020 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. (formerly known as CyaSSL) |
| 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-1301, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include <wolfssl/options.h> |
| 23 | +#include <wolfssl/wolfcrypt/settings.h> |
| 24 | +#include <wolfssl/ssl.h> |
| 25 | +#include <wolfssl/wolfcrypt/pkcs7.h> |
| 26 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 27 | +#include <wolfssl/wolfcrypt/logging.h> |
| 28 | + |
| 29 | + |
| 30 | +#ifdef HAVE_SMIME |
| 31 | + |
| 32 | +static int Verify(byte* smime, int smimeSz, byte* ca, int caSz, byte* contentIn, int contentInSz, int detached) |
| 33 | +{ |
| 34 | + WOLFSSL_PKCS7* pkcs7Compat = NULL; |
| 35 | + WOLFSSL_BIO *in, *content = NULL; |
| 36 | + WOLFSSL_BIO *multi = NULL; |
| 37 | + WOLFSSL_X509* x509 = NULL; |
| 38 | + WOLFSSL_X509_STORE* store = NULL; |
| 39 | + int ret = 0; |
| 40 | + |
| 41 | + in = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()); |
| 42 | + if (in == NULL) { |
| 43 | + ret = MEMORY_E; |
| 44 | + } |
| 45 | + |
| 46 | + if (ret == 0) { |
| 47 | + |
| 48 | + wolfSSL_BIO_write(in, smime, smimeSz); |
| 49 | + pkcs7Compat = (WOLFSSL_PKCS7*)wolfSSL_SMIME_read_PKCS7(in, &multi); |
| 50 | + if (pkcs7Compat == NULL) { |
| 51 | + printf("Error parsing SMIME\n"); |
| 52 | + ret = -1; |
| 53 | + } |
| 54 | + |
| 55 | + if (multi != NULL) { |
| 56 | + byte* pt; |
| 57 | + int ptSz, i; |
| 58 | + |
| 59 | + printf("Multi part message, signed data is : "); |
| 60 | + ptSz = wolfSSL_BIO_get_mem_data(multi, &pt); |
| 61 | + for (i = 0; i < ptSz; i ++) |
| 62 | + printf("%02X", pt[i]); |
| 63 | + printf("\n"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (ret == 0) { |
| 68 | + const unsigned char* pt; |
| 69 | + |
| 70 | + /* set devID */ |
| 71 | + pkcs7Compat->pkcs7.devId = INVALID_DEVID; |
| 72 | + |
| 73 | + pt = ca; |
| 74 | + x509 = wolfSSL_d2i_X509(NULL, &pt, caSz); |
| 75 | + if (x509 == NULL) { |
| 76 | + printf("Error decoding signer\n"); |
| 77 | + ret = -1; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (ret == 0) { |
| 82 | + store = wolfSSL_X509_STORE_new(); |
| 83 | + if (store == NULL) { |
| 84 | + printf("Error creating cert store\n"); |
| 85 | + ret = MEMORY_E; |
| 86 | + } |
| 87 | + else { |
| 88 | + wolfSSL_X509_STORE_add_cert(store, x509); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (ret == 0 && contentIn != NULL) { |
| 93 | + pkcs7Compat->pkcs7.content = contentIn; |
| 94 | + pkcs7Compat->pkcs7.contentSz = contentInSz; |
| 95 | + wc_PKCS7_SetDetached(&pkcs7Compat->pkcs7, 1); |
| 96 | + } |
| 97 | + |
| 98 | + if (ret == 0) { |
| 99 | + content = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()); |
| 100 | + ret = wolfSSL_PKCS7_verify((PKCS7*)pkcs7Compat, NULL, store, multi, |
| 101 | + content, detached); |
| 102 | + if (ret == WOLFSSL_SUCCESS) { |
| 103 | + ret = 0; |
| 104 | + } |
| 105 | + else { |
| 106 | + /* print out certificate that could not be verified */ |
| 107 | + int i; |
| 108 | + byte* pt = pkcs7Compat->pkcs7.verifyCert; |
| 109 | + |
| 110 | + printf("Could not verify certificate :"); |
| 111 | + for (i = 0; i < pkcs7Compat->pkcs7.verifyCertSz; i++) { |
| 112 | + printf("%02X", pt[i]); |
| 113 | + } |
| 114 | + printf("\n"); |
| 115 | + ret = -1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + if (ret == 0 && content != NULL) { |
| 121 | + byte* pt; |
| 122 | + int ptSz, i; |
| 123 | + |
| 124 | + printf("Content found on verify : "); |
| 125 | + ptSz = wolfSSL_BIO_get_mem_data(content, &pt); |
| 126 | + for (i = 0; i < ptSz; i ++) |
| 127 | + printf("%c", pt[i]); |
| 128 | + printf("\n"); |
| 129 | + } |
| 130 | + |
| 131 | + wolfSSL_BIO_free(in); |
| 132 | + wolfSSL_BIO_free(content); |
| 133 | + wolfSSL_BIO_free(multi); |
| 134 | + wolfSSL_PKCS7_free((PKCS7*)pkcs7Compat); |
| 135 | + wolfSSL_X509_free(x509); |
| 136 | + wolfSSL_X509_STORE_free(store); |
| 137 | + return ret; |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +/* read private smime and signer certificate in DER format */ |
| 142 | +static int ReadSmimeAndCert(char* smimeFile, char* certFile, char* contentFile, |
| 143 | + byte* smime, |
| 144 | + int* smimeSz, byte* cert, int* certSz, byte* content, int* contentSz) |
| 145 | +{ |
| 146 | + int ret; |
| 147 | + XFILE f; |
| 148 | + |
| 149 | + f = XFOPEN(smimeFile, "rb"); |
| 150 | + if (f == NULL) { |
| 151 | + printf("Error opening file %s\n", smimeFile); |
| 152 | + return -1; |
| 153 | + } |
| 154 | + else { |
| 155 | + ret = XFREAD(smime, 1, *smimeSz, f); |
| 156 | + if (ret >= 0) { |
| 157 | + if (ret == *smimeSz) { |
| 158 | + printf("smime read in was larger than buffer\n"); |
| 159 | + XFCLOSE(f); |
| 160 | + return -1; |
| 161 | + } |
| 162 | + else { |
| 163 | + *smimeSz = ret; |
| 164 | + ret = 0; |
| 165 | + XFCLOSE(f); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + f = XFOPEN(certFile, "rb"); |
| 171 | + if (f == NULL) { |
| 172 | + printf("Error opening file %s\n", certFile); |
| 173 | + return -1; |
| 174 | + } |
| 175 | + else { |
| 176 | + ret = XFREAD(cert, 1, *certSz, f); |
| 177 | + if (ret >= 0) { |
| 178 | + if (ret == *certSz) { |
| 179 | + printf("Cert read in was larger than buffer\n"); |
| 180 | + XFCLOSE(f); |
| 181 | + return -1; |
| 182 | + } |
| 183 | + else { |
| 184 | + *certSz = ret; |
| 185 | + ret = 0; |
| 186 | + XFCLOSE(f); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + f = XFOPEN(contentFile, "rb"); |
| 192 | + if (f == NULL) { |
| 193 | + printf("Error opening file %s\n", contentFile); |
| 194 | + return -1; |
| 195 | + } |
| 196 | + else { |
| 197 | + ret = XFREAD(content, 1, *contentSz, f); |
| 198 | + if (ret >= 0) { |
| 199 | + if (ret == *contentSz) { |
| 200 | + printf("Cert read in was larger than buffer\n"); |
| 201 | + XFCLOSE(f); |
| 202 | + return -1; |
| 203 | + } |
| 204 | + else { |
| 205 | + *contentSz = ret; |
| 206 | + ret = 0; |
| 207 | + XFCLOSE(f); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + return ret; |
| 213 | +} |
| 214 | + |
| 215 | +int main(int argc, char** argv) |
| 216 | +{ |
| 217 | + byte cert[4096]; |
| 218 | + int certSz = 4096; |
| 219 | + |
| 220 | + byte smime[10000]; |
| 221 | + int smimeSz = 10000; |
| 222 | + |
| 223 | + byte content[10000]; |
| 224 | + int contentSz = 10000; |
| 225 | + |
| 226 | + int ret; |
| 227 | + |
| 228 | + if (argc != 4) { |
| 229 | + printf("Use ./smime-verify <smime file> <der cert file> <content file>\n"); |
| 230 | + return -1; |
| 231 | + } |
| 232 | + |
| 233 | +#ifdef DEBUG_WOLFSSL |
| 234 | + wolfSSL_Debugging_ON(); |
| 235 | +#endif |
| 236 | + |
| 237 | + if (wolfSSL_Init() != WOLFSSL_SUCCESS) { |
| 238 | + printf("Failure to initialize wolfSSL library\n"); |
| 239 | + return -1; |
| 240 | + } |
| 241 | + |
| 242 | + ret = ReadSmimeAndCert(argv[1], argv[2], argv[3], smime, &smimeSz, cert, |
| 243 | + &certSz, content, &contentSz); |
| 244 | + if (ret == 0) { |
| 245 | + ret = Verify(smime, smimeSz, cert, certSz, content, contentSz, 0); |
| 246 | + if (ret == 0) { |
| 247 | + printf("Verify Success\n"); |
| 248 | + } |
| 249 | + else { |
| 250 | + printf("Verify Failed\n"); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + wolfSSL_Cleanup(); |
| 255 | + return ret; |
| 256 | +} |
| 257 | +#else |
| 258 | +int main() |
| 259 | +{ |
| 260 | + printf("wolfSSL was compiled with out HAVE_SMIME support\n"); |
| 261 | + return 0; |
| 262 | +} |
| 263 | +#endif |
0 commit comments