|
| 1 | +/* envelopedDataDecode.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 | + |
| 23 | +#include <wolfssl/options.h> |
| 24 | +#include <wolfssl/wolfcrypt/settings.h> |
| 25 | +#include <wolfssl/wolfcrypt/pkcs7.h> |
| 26 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 27 | +#include <wolfssl/wolfcrypt/logging.h> |
| 28 | + |
| 29 | +static int load_certs(const char* certFile, byte* cert, word32* certSz, |
| 30 | + const char* keyFile, byte* key, word32* keySz) |
| 31 | +{ |
| 32 | + FILE* file; |
| 33 | + |
| 34 | + /* certificate file */ |
| 35 | + file = fopen(certFile, "rb"); |
| 36 | + if (!file) |
| 37 | + return -1; |
| 38 | + |
| 39 | + *certSz = (word32)fread(cert, 1, *certSz, file); |
| 40 | + fclose(file); |
| 41 | + |
| 42 | + /* key file */ |
| 43 | + file = fopen(keyFile, "rb"); |
| 44 | + if (!file) |
| 45 | + return -1; |
| 46 | + |
| 47 | + *keySz = (word32)fread(key, 1, *keySz, file); |
| 48 | + fclose(file); |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +static int envelopedData_decrypt(byte* in, word32 inSz, byte* cert, |
| 55 | + word32 certSz, byte* key, word32 keySz, |
| 56 | + byte* out, word32 outSz) |
| 57 | +{ |
| 58 | + int ret; |
| 59 | + PKCS7* pkcs7; |
| 60 | + |
| 61 | + pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID); |
| 62 | + if (pkcs7 == NULL) |
| 63 | + return -1; |
| 64 | + |
| 65 | + /* init with recipient cert */ |
| 66 | + ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz); |
| 67 | + if (ret != 0) { |
| 68 | + wc_PKCS7_Free(pkcs7); |
| 69 | + return -1; |
| 70 | + } |
| 71 | + |
| 72 | + /* set recipient private key */ |
| 73 | + ret = wc_PKCS7_SetKey(pkcs7, key, keySz); |
| 74 | + if (ret != 0) { |
| 75 | + wc_PKCS7_Free(pkcs7); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + |
| 79 | + /* decode envelopedData, returns size */ |
| 80 | + ret = wc_PKCS7_DecodeEnvelopedData(pkcs7, in, inSz, out, outSz); |
| 81 | + wc_PKCS7_Free(pkcs7); |
| 82 | + |
| 83 | + if (ret <= 0) { |
| 84 | + printf("Failed to decode EnvelopedData bundle error of %d\n", ret); |
| 85 | + } |
| 86 | + else { |
| 87 | + printf("Successfully decoded EnvelopedData bundle\n"); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +#ifdef HAVE_PKCS7 |
| 95 | + |
| 96 | +int main(int argc, char** argv) |
| 97 | +{ |
| 98 | + int ret; |
| 99 | + int encryptedSz, decryptedSz; |
| 100 | + word32 certSz, keySz; |
| 101 | + |
| 102 | + byte cert[2048]; |
| 103 | + byte key[2048]; |
| 104 | + byte* encrypted; |
| 105 | + byte* decrypted; |
| 106 | + |
| 107 | +#ifdef DEBUG_WOLFSSL |
| 108 | + wolfSSL_Debugging_ON(); |
| 109 | +#endif |
| 110 | + |
| 111 | + if (argc != 4) { |
| 112 | + printf("expecting DER cert, key, and encrypted bundle as args\n"); |
| 113 | + printf("%s <DER cert> <DER key> <Encrypted bundle>\n", argv[0]); |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + certSz = sizeof(cert); |
| 118 | + keySz = sizeof(key); |
| 119 | + ret = load_certs(argv[1], cert, &certSz, argv[2], key, &keySz); |
| 120 | + if (ret != 0) |
| 121 | + return -1; |
| 122 | + |
| 123 | + /* read encrypted bundle */ |
| 124 | + { |
| 125 | + FILE* file; |
| 126 | + |
| 127 | + file = fopen(argv[3], "rb"); |
| 128 | + if (!file) { |
| 129 | + printf("unable to open file %s\n", argv[3]); |
| 130 | + return -1; |
| 131 | + } |
| 132 | + fseek(file, 0, SEEK_END); |
| 133 | + encryptedSz = (int)ftell(file); |
| 134 | + rewind(file); |
| 135 | + |
| 136 | + encrypted = (byte*)malloc(encryptedSz); |
| 137 | + if (encrypted == NULL) { |
| 138 | + printf("malloc failed\n"); |
| 139 | + return -1; |
| 140 | + } |
| 141 | + |
| 142 | + decryptedSz = encryptedSz; |
| 143 | + decrypted = (byte*)malloc(decryptedSz); |
| 144 | + if (decrypted == NULL) { |
| 145 | + printf("malloc failed\n"); |
| 146 | + free(encrypted); |
| 147 | + return -1; |
| 148 | + } |
| 149 | + |
| 150 | + encryptedSz = (word32)fread(encrypted, 1, encryptedSz, file); |
| 151 | + printf("encrypted bundle size read = %d\n", encryptedSz); |
| 152 | + fclose(file); |
| 153 | + } |
| 154 | + |
| 155 | + decryptedSz = envelopedData_decrypt(encrypted, encryptedSz, |
| 156 | + cert, certSz, key, keySz, |
| 157 | + decrypted, decryptedSz); |
| 158 | + free(encrypted); |
| 159 | + if (decryptedSz < 0) { |
| 160 | + free(decrypted); |
| 161 | + return -1; |
| 162 | + } |
| 163 | + |
| 164 | +#ifdef DEBUG_WOLFSSL |
| 165 | + printf("Decrypted content (%d byte):\n", decryptedSz); |
| 166 | + WOLFSSL_BUFFER(decrypted, decryptedSz); |
| 167 | +#endif |
| 168 | + free(decrypted); |
| 169 | + |
| 170 | + return 0; |
| 171 | +} |
| 172 | + |
| 173 | +#else |
| 174 | + |
| 175 | +int main(int argc, char** argv) |
| 176 | +{ |
| 177 | + printf("Must build wolfSSL using ./configure --enable-pkcs7\n"); |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | +#endif |
| 182 | + |
0 commit comments