add PKCS#7/CMS example for CompressedData content type

pull/113/head
Chris Conlon 2018-10-23 14:08:23 -06:00
parent d5bef93e3b
commit 35675c7242
3 changed files with 176 additions and 1 deletions

View File

@ -42,4 +42,5 @@ clean:
envelopedDataKTRI.der envelopedDataKARI.der \
envelopedDataPWRI.der envelopedDataORI.der \
authEnvelopedDataKARI.der authEnvelopedDataKTRI.der \
authEnvelopedDataORI.der authEnvelopedDataPWRI.der encryptedData.der
authEnvelopedDataORI.der authEnvelopedDataPWRI.der encryptedData.der \
compressedData.der

View File

@ -126,6 +126,25 @@ of the bundle will be printed out to the terminal window.
Successfully encoded EncryptedData bundle (encryptedData.der)
```
### CompressedData
Example file: `compressedData.c`
Generated bundle file: `compressedData.der`
This example creates a PKCS#7/CMS CompressedData bundle. After creating the
bundle, it decodes, uncompresses, and verifies the bundle.
The generated CompressedData bundle is written out to a file for analysis and
additional debugging.
If wolfSSL has been configured and compiled with debug support, the bytes
of the bundle will be printed out to the terminal window.
```
./compressedData
Successfully encoded CompressedData bundle (compressedData.der)
```
### EnvelopedData using KTRI RecipientInfo
Example file: `envelopedData-ktri.c`

View File

@ -0,0 +1,155 @@
/* compressedData.c
*
* Copyright (C) 2006-2018 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/pkcs7.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/logging.h>
#define compressedFile "compressedData.der"
static const byte data[] = { /* Hello World */
0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
0x72,0x6c,0x64
};
static int write_file_buffer(const char* fileName, byte* in, word32 inSz)
{
int ret;
FILE* file;
file = fopen(fileName, "wb");
if (file == NULL) {
printf("ERROR: opening file for writing: %s\n", fileName);
return -1;
}
ret = (int)fwrite(in, 1, inSz, file);
if (ret == 0) {
printf("ERROR: writing buffer to output file\n");
return -1;
}
fclose(file);
return 0;
}
static int compressedData_encode(byte* out, word32 outSz)
{
int ret;
PKCS7* pkcs7;
pkcs7 = wc_PKCS7_New(NULL, 0);
if (pkcs7 == NULL)
return -1;
pkcs7->content = (byte*)data;
pkcs7->contentSz = sizeof(data);
pkcs7->contentOID = DATA;
/* encode compressedData, returns size */
ret = wc_PKCS7_EncodeCompressedData(pkcs7, out, outSz);
if (ret <= 0) {
wc_PKCS7_Free(pkcs7);
return -1;
} else {
printf("Successfully encoded CompressedData bundle (%s)\n",
compressedFile);
if (write_file_buffer(compressedFile, out, ret) != 0) {
printf("ERROR: error writing encoded to output file\n");
return -1;
}
}
wc_PKCS7_Free(pkcs7);
return ret;
}
static int compressedData_decode(byte* in, word32 inSz, byte* out, word32 outSz)
{
int ret;
PKCS7* pkcs7;
pkcs7 = wc_PKCS7_New(NULL, 0);
if (pkcs7 == NULL)
return -1;
/* decode compressedData, returns size */
ret = wc_PKCS7_DecodeCompressedData(pkcs7, in, inSz, out, outSz);
if (ret <= 0) {
wc_PKCS7_Free(pkcs7);
return -1;
}
wc_PKCS7_Free(pkcs7);
return ret;
}
#ifdef HAVE_PKCS7
int main(int argc, char** argv)
{
int compressedSz, decodedSz;
byte compressed[1024];
byte decoded[1024];
#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
#endif
compressedSz = compressedData_encode(compressed, sizeof(compressed));
if (compressedSz < 0)
return -1;
#ifdef DEBUG_WOLFSSL
printf("CompressedData DER (%d bytes):\n", compressedSz);
WOLFSSL_BUFFER(compressed, compressedSz);
#endif
decodedSz = compressedData_decode(compressed, compressedSz,
decoded, sizeof(decoded));
if (decodedSz < 0)
return -1;
#ifdef DEBUG_WOLFSSL
printf("Decoded Data DER (%d bytes):\n", decodedSz);
WOLFSSL_BUFFER(decoded, decodedSz);
#endif
(void)argc;
(void)argv;
return 0;
}
#else
int main(int argc, char** argv)
{
printf("Must build wolfSSL using ./configure --enable-pkcs7\n");
return 0;
}
#endif