|
| 1 | +/* envelopedData-ktri-stream.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/wolfcrypt/pkcs7.h> |
| 25 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 26 | +#include <wolfssl/wolfcrypt/logging.h> |
| 27 | + |
| 28 | +#define certFile "../certs/client-cert.der" |
| 29 | +#define keyFile "../certs/client-key.der" |
| 30 | + |
| 31 | +#define encodedFileKTRI "envelopedDataKTRI-stream.der" |
| 32 | + |
| 33 | +FILE *fileOut, *fileIn; |
| 34 | +#define TEST_SIZE 256 |
| 35 | +static byte* contentRead = NULL; |
| 36 | + |
| 37 | +static int GetContentCB(PKCS7* pkcs7, byte** content) |
| 38 | +{ |
| 39 | + int ret; |
| 40 | + |
| 41 | + ret = fread(contentRead, 1, TEST_SIZE, fileIn); |
| 42 | + *content = contentRead; |
| 43 | + |
| 44 | + return ret; |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz) |
| 49 | +{ |
| 50 | + if (outputSz > 0) { |
| 51 | + if (fwrite(output, 1, outputSz, fileOut) != outputSz) { |
| 52 | + return -1; |
| 53 | + } |
| 54 | + } |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +static int load_certs(byte* cert, word32* certSz, byte* key, word32* keySz) |
| 60 | +{ |
| 61 | + FILE* file; |
| 62 | + |
| 63 | + /* certificate file */ |
| 64 | + file = fopen(certFile, "rb"); |
| 65 | + if (!file) |
| 66 | + return -1; |
| 67 | + |
| 68 | + *certSz = (word32)fread(cert, 1, *certSz, file); |
| 69 | + fclose(file); |
| 70 | + |
| 71 | + /* key file */ |
| 72 | + file = fopen(keyFile, "rb"); |
| 73 | + if (!file) |
| 74 | + return -1; |
| 75 | + |
| 76 | + *keySz = (word32)fread(key, 1, *keySz, file); |
| 77 | + fclose(file); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key, |
| 84 | + word32 keySz, byte* out, word32 outSz, |
| 85 | + word32 contentSz, byte useStreamMode) |
| 86 | +{ |
| 87 | + int ret; |
| 88 | + PKCS7* pkcs7; |
| 89 | + |
| 90 | + pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID); |
| 91 | + if (pkcs7 == NULL) |
| 92 | + return -1; |
| 93 | + |
| 94 | + pkcs7->content = NULL; |
| 95 | + pkcs7->contentSz = contentSz; |
| 96 | + pkcs7->contentOID = DATA; |
| 97 | + pkcs7->encryptOID = AES256CBCb; |
| 98 | + |
| 99 | + if (useStreamMode) { |
| 100 | + wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB, StreamOutputCB); |
| 101 | + } |
| 102 | + |
| 103 | + /* add recipient using RSA certificate (KTRI type) */ |
| 104 | + ret = wc_PKCS7_AddRecipient_KTRI(pkcs7, cert, certSz, 0); |
| 105 | + if (ret < 0) { |
| 106 | + printf("wc_PKCS7_AddRecipient_KTRI() failed, ret = %d\n", ret); |
| 107 | + wc_PKCS7_Free(pkcs7); |
| 108 | + return -1; |
| 109 | + } |
| 110 | + |
| 111 | + /* encode envelopedData, returns size */ |
| 112 | + ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz); |
| 113 | + if (ret <= 0) { |
| 114 | + printf("ERROR: wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret); |
| 115 | + wc_PKCS7_Free(pkcs7); |
| 116 | + return -1; |
| 117 | + |
| 118 | + } else { |
| 119 | + printf("Successfully encoded EnvelopedData bundle (%s), stream mode" |
| 120 | + " %d\n", encodedFileKTRI, useStreamMode); |
| 121 | + } |
| 122 | + |
| 123 | + wc_PKCS7_Free(pkcs7); |
| 124 | + |
| 125 | + return ret; |
| 126 | +} |
| 127 | + |
| 128 | +static int envelopedData_decrypt(byte* in, word32 inSz, byte* cert, |
| 129 | + word32 certSz, byte* key, word32 keySz, |
| 130 | + byte* out, word32 outSz) |
| 131 | +{ |
| 132 | + int ret; |
| 133 | + PKCS7* pkcs7; |
| 134 | + |
| 135 | + pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID); |
| 136 | + if (pkcs7 == NULL) |
| 137 | + return -1; |
| 138 | + |
| 139 | + /* init with recipient cert */ |
| 140 | + ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz); |
| 141 | + if (ret != 0) { |
| 142 | + wc_PKCS7_Free(pkcs7); |
| 143 | + return -1; |
| 144 | + } |
| 145 | + |
| 146 | + /* set recipient private key */ |
| 147 | + ret = wc_PKCS7_SetKey(pkcs7, key, keySz); |
| 148 | + if (ret != 0) { |
| 149 | + wc_PKCS7_Free(pkcs7); |
| 150 | + return -1; |
| 151 | + } |
| 152 | + |
| 153 | + /* decode envelopedData, returns size */ |
| 154 | + ret = wc_PKCS7_DecodeEnvelopedData(pkcs7, in, inSz, out, outSz); |
| 155 | + if (ret <= 0) { |
| 156 | + printf("Failed to decode EnvelopedData bundle (%s), error %d\n", |
| 157 | + encodedFileKTRI, ret); |
| 158 | + wc_PKCS7_Free(pkcs7); |
| 159 | + return -1; |
| 160 | + } else { |
| 161 | + printf("Successfully decoded EnvelopedData bundle (%s)\n", |
| 162 | + encodedFileKTRI); |
| 163 | + } |
| 164 | + |
| 165 | + wc_PKCS7_Free(pkcs7); |
| 166 | + |
| 167 | + return ret; |
| 168 | +} |
| 169 | + |
| 170 | +#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER) |
| 171 | + |
| 172 | +#define EXTRA_ASN1_SIZE 1024 |
| 173 | + |
| 174 | +int main(int argc, char** argv) |
| 175 | +{ |
| 176 | + int ret = 0; |
| 177 | + int encryptedSz = 0, decryptedSz; |
| 178 | + word32 certSz, keySz, contentSz; |
| 179 | + |
| 180 | + byte cert[2048]; |
| 181 | + byte key[2048]; |
| 182 | + byte* encrypted = NULL; |
| 183 | + byte* decrypted = NULL; |
| 184 | + |
| 185 | +#ifdef DEBUG_WOLFSSL |
| 186 | + wolfSSL_Debugging_ON(); |
| 187 | +#endif |
| 188 | + |
| 189 | + if (argc != 2) { |
| 190 | + printf("USAGE: ./%s <content file name>\n", argv[0]); |
| 191 | + return -1; |
| 192 | + } |
| 193 | + |
| 194 | + if (wolfCrypt_Init() != 0) { |
| 195 | + printf("Issue with wolfcrypt init\n"); |
| 196 | + return -1; |
| 197 | + } |
| 198 | + |
| 199 | + fileIn = fopen(argv[1], "rb"); |
| 200 | + if (fileIn == NULL) { |
| 201 | + printf("Issue opening file %s\n", argv[1]); |
| 202 | + return -1; |
| 203 | + } |
| 204 | + |
| 205 | + fileOut = fopen(encodedFileKTRI, "wb"); |
| 206 | + if (fileOut == NULL) { |
| 207 | + printf("Issue opening file %s\n", encodedFileKTRI); |
| 208 | + fclose(fileIn); |
| 209 | + return -1; |
| 210 | + } |
| 211 | + |
| 212 | + contentRead = (byte*)XMALLOC(TEST_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
| 213 | + if (contentRead == NULL) { |
| 214 | + printf("Unable to malloc content read buffer\n"); |
| 215 | + ret = MEMORY_E; |
| 216 | + } |
| 217 | + |
| 218 | + if (ret == 0) { |
| 219 | + fseek(fileIn, 0, SEEK_END); |
| 220 | + contentSz = ftell(fileIn); |
| 221 | + fseek(fileIn, 0, SEEK_SET); |
| 222 | + printf("contentSz = %d\n", contentSz); |
| 223 | + |
| 224 | + certSz = sizeof(cert); |
| 225 | + keySz = sizeof(key); |
| 226 | + ret = load_certs(cert, &certSz, key, &keySz); |
| 227 | + } |
| 228 | + |
| 229 | + if (ret == 0) { |
| 230 | + encryptedSz = envelopedData_encrypt(cert, certSz, key, keySz, |
| 231 | + encrypted, encryptedSz, contentSz, 1); |
| 232 | + if (encryptedSz < 0) { |
| 233 | + ret = encryptedSz; |
| 234 | + printf("Issue %d with encrypt\n", ret); |
| 235 | + } |
| 236 | + } |
| 237 | + fclose(fileIn); |
| 238 | + fclose(fileOut); |
| 239 | + |
| 240 | + |
| 241 | +#if 1 |
| 242 | + decryptedSz = encryptedSz = contentSz + EXTRA_ASN1_SIZE; |
| 243 | + if (ret == 0) { |
| 244 | + encrypted = (byte*)XMALLOC(encryptedSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
| 245 | + decrypted = (byte*)XMALLOC(decryptedSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
| 246 | + if (encrypted == NULL || decrypted == NULL) { |
| 247 | + ret = MEMORY_E; |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + if (ret == 0) { |
| 252 | + FILE* f = fopen(encodedFileKTRI, "rb"); |
| 253 | + encryptedSz = fread(encrypted, 1, encryptedSz, f); |
| 254 | + fclose(f); |
| 255 | + if (encryptedSz <= 0) { |
| 256 | + printf("error reading file %s\n", encodedFileKTRI); |
| 257 | + ret = -1; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + if (ret == 0) { |
| 262 | + decryptedSz = envelopedData_decrypt(encrypted, encryptedSz, |
| 263 | + cert, certSz, key, keySz, |
| 264 | + decrypted, decryptedSz); |
| 265 | + if (decryptedSz < 0) { |
| 266 | + ret = decryptedSz; |
| 267 | + printf("Issue %d with decrypt\n", ret); |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | +#endif |
| 272 | + |
| 273 | + if (contentRead != NULL) |
| 274 | + XFREE(contentRead, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
| 275 | + if (encrypted != NULL) |
| 276 | + XFREE(encrypted, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
| 277 | + if (decrypted != NULL) |
| 278 | + XFREE(decrypted, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
| 279 | + |
| 280 | + return ret; |
| 281 | +} |
| 282 | + |
| 283 | +#else |
| 284 | + |
| 285 | +int main(int argc, char** argv) |
| 286 | +{ |
| 287 | + printf("Must build wolfSSL using ./configure --enable-pkcs7 --enable-indef\n"); |
| 288 | + return 0; |
| 289 | +} |
| 290 | + |
| 291 | +#endif |
| 292 | + |
0 commit comments