Skip to content

Commit

Permalink
add simple CMS examples for different content types
Browse files Browse the repository at this point in the history
  • Loading branch information
cconlon committed Nov 15, 2018
1 parent 1783dce commit c1cf06e
Show file tree
Hide file tree
Showing 13 changed files with 2,692 additions and 3 deletions.
Binary file added certs/ecc-client-key.der
Binary file not shown.
11 changes: 9 additions & 2 deletions pkcs7/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CC = gcc
LIB_PATH = /usr/local
CFLAGS = -Wall -I$(LIB_PATH)/include
LIBS = -L$(LIB_PATH)/lib -lm
LIBS = -L$(LIB_PATH)/lib -lwolfssl -lm -lz

# option variables
DYN_LIB = -lwolfssl
Expand Down Expand Up @@ -33,4 +33,11 @@ debug: all
$(CC) -o $@ $< $(CFLAGS) $(LIBS)

clean:
rm -f $(TARGETS)
rm -f $(TARGETS) signedData_attrs.der signedData_noattrs.der \
signedFirmwarePkgData.der signedFirmwarePkgData_attrs.der \
signedFirmwarePkgData_noattrs.der \
signedEncryptedFPD_attrs.der signedEncryptedFPD_noattrs.der \
signedCompressedFPD_attrs.der signedCompressedFPD_noattrs.der \
signedEncryptedCompressedFPD_attrs.der signedEncryptedCompressedFPD_noattrs.der \
envelopedDataKTRI.der envelopedDataKARI.der \
envelopedDataPWRI.der envelopedDataORI.der
8 changes: 7 additions & 1 deletion pkcs7/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
### Build and install wolfSSL

```
./configure --enable-pkcs7 && make && sudo make install
$ ./configure --enable-pkcs7
$ make
$ sudo make install
```

### Build Example

Running `make` will try to compile all C source files into their own
respective example application. Each source file can be compiled separately
if desired, for example:

```
make
gcc -o pkcs7 pkcs7.c -Wall -I/usr/local/include -Os -L/usr/local/lib -lm -lwolfssl
Expand Down
133 changes: 133 additions & 0 deletions pkcs7/encryptedData.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* encryptedData.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>

static const byte data[] = { /* Hello World */
0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
0x72,0x6c,0x64
};

static const byte aes256Key[] = {
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
};

static int encryptedData_encrypt(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;
pkcs7->encryptOID = AES256CBCb;
pkcs7->encryptionKey = (byte*)aes256Key;
pkcs7->encryptionKeySz = sizeof(aes256Key);

/* encode encryptedData, returns size */
ret = wc_PKCS7_EncodeEncryptedData(pkcs7, out, outSz);
if (ret <= 0) {
wc_PKCS7_Free(pkcs7);
return -1;
}

wc_PKCS7_Free(pkcs7);

return ret;
}

static int encryptedData_decrypt(byte* in, word32 inSz, byte* out, word32 outSz)
{
int ret;
PKCS7* pkcs7;

pkcs7 = wc_PKCS7_New(NULL, 0);
if (pkcs7 == NULL)
return -1;

pkcs7->encryptionKey = (byte*)aes256Key;
pkcs7->encryptionKeySz = sizeof(aes256Key);

/* decrypt encryptedData, returns size */
ret = wc_PKCS7_DecodeEncryptedData(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 encryptedSz, decryptedSz;
byte encrypted[1024];
byte decrypted[1024];

#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
#endif

encryptedSz = encryptedData_encrypt(encrypted, sizeof(encrypted));
if (encryptedSz < 0)
return -1;

printf("EncryptedData DER (%d byte):\n", encryptedSz);
WOLFSSL_BUFFER(encrypted, encryptedSz);

decryptedSz = encryptedData_decrypt(encrypted, encryptedSz,
decrypted, sizeof(decrypted));
if (decryptedSz < 0)
return -1;

printf("DecryptedData DER (%d byte):\n", decryptedSz);
WOLFSSL_BUFFER(decrypted, decryptedSz);

(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

Loading

0 comments on commit c1cf06e

Please sign in to comment.