add example of no certs bundle and stream mode
parent
5a7c32742a
commit
81827df470
|
@ -79,7 +79,8 @@ static int write_file_buffer(const char* fileName, byte* in, word32 inSz)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
|
static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
|
||||||
word32 keySz, byte* out, word32 outSz)
|
word32 keySz, byte* out, word32 outSz,
|
||||||
|
byte useStreamMode)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
PKCS7* pkcs7;
|
PKCS7* pkcs7;
|
||||||
|
@ -93,6 +94,10 @@ static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
|
||||||
pkcs7->contentOID = DATA;
|
pkcs7->contentOID = DATA;
|
||||||
pkcs7->encryptOID = AES256CBCb;
|
pkcs7->encryptOID = AES256CBCb;
|
||||||
|
|
||||||
|
if (useStreamMode) {
|
||||||
|
wc_PKCS7_SetStreamMode(pkcs7, 1);
|
||||||
|
}
|
||||||
|
|
||||||
/* add recipient using RSA certificate (KTRI type) */
|
/* add recipient using RSA certificate (KTRI type) */
|
||||||
ret = wc_PKCS7_AddRecipient_KTRI(pkcs7, cert, certSz, 0);
|
ret = wc_PKCS7_AddRecipient_KTRI(pkcs7, cert, certSz, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
@ -109,8 +114,8 @@ static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
printf("Successfully encoded EnvelopedData bundle (%s)\n",
|
printf("Successfully encoded EnvelopedData bundle (%s), stream mode"
|
||||||
encodedFileKTRI);
|
" %d\n", encodedFileKTRI, useStreamMode);
|
||||||
|
|
||||||
if (write_file_buffer(encodedFileKTRI, out, ret) != 0) {
|
if (write_file_buffer(encodedFileKTRI, out, ret) != 0) {
|
||||||
printf("ERROR: error writing encoded to output file\n");
|
printf("ERROR: error writing encoded to output file\n");
|
||||||
|
@ -177,7 +182,7 @@ int main(int argc, char** argv)
|
||||||
byte key[2048];
|
byte key[2048];
|
||||||
byte encrypted[1024];
|
byte encrypted[1024];
|
||||||
byte decrypted[1024];
|
byte decrypted[1024];
|
||||||
|
|
||||||
#ifdef DEBUG_WOLFSSL
|
#ifdef DEBUG_WOLFSSL
|
||||||
wolfSSL_Debugging_ON();
|
wolfSSL_Debugging_ON();
|
||||||
#endif
|
#endif
|
||||||
|
@ -189,10 +194,18 @@ int main(int argc, char** argv)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
encryptedSz = envelopedData_encrypt(cert, certSz, key, keySz,
|
encryptedSz = envelopedData_encrypt(cert, certSz, key, keySz,
|
||||||
encrypted, sizeof(encrypted));
|
encrypted, sizeof(encrypted), 0);
|
||||||
if (encryptedSz < 0)
|
if (encryptedSz < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
#ifdef ASN_BER_TO_DER
|
||||||
|
/* recreate the bundle with BER encoding */
|
||||||
|
encryptedSz = envelopedData_encrypt(cert, certSz, key, keySz,
|
||||||
|
encrypted, sizeof(encrypted), 1);
|
||||||
|
if (encryptedSz < 0)
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef DEBUG_WOLFSSL
|
#ifdef DEBUG_WOLFSSL
|
||||||
printf("EnvelopedData DER (%d byte):\n", encryptedSz);
|
printf("EnvelopedData DER (%d byte):\n", encryptedSz);
|
||||||
WOLFSSL_BUFFER(encrypted, encryptedSz);
|
WOLFSSL_BUFFER(encrypted, encryptedSz);
|
||||||
|
|
|
@ -79,7 +79,8 @@ static int write_file_buffer(const char* fileName, byte* in, word32 inSz)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
|
static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
|
||||||
word32 keySz, byte* out, word32 outSz)
|
word32 keySz, byte* out, word32 outSz,
|
||||||
|
byte streamMode, byte noCerts)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
PKCS7* pkcs7;
|
PKCS7* pkcs7;
|
||||||
|
@ -118,6 +119,14 @@ static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
|
||||||
pkcs7->signedAttribs = NULL;
|
pkcs7->signedAttribs = NULL;
|
||||||
pkcs7->signedAttribsSz = 0;
|
pkcs7->signedAttribsSz = 0;
|
||||||
|
|
||||||
|
if (streamMode) {
|
||||||
|
wc_PKCS7_SetStreamMode(pkcs7, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (noCerts) {
|
||||||
|
wc_PKCS7_SetNoCerts(pkcs7, 1);
|
||||||
|
}
|
||||||
|
|
||||||
/* encode signedData, returns size */
|
/* encode signedData, returns size */
|
||||||
ret = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
|
ret = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
|
@ -127,8 +136,8 @@ static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
printf("Successfully encoded SignedData bundle (%s)\n",
|
printf("Successfully encoded SignedData bundle (%s) %s\n",
|
||||||
encodedFileNoAttrs);
|
encodedFileNoAttrs, (noCerts)? "No Certs Added":"");
|
||||||
|
|
||||||
#ifdef DEBUG_WOLFSSL
|
#ifdef DEBUG_WOLFSSL
|
||||||
printf("Encoded DER (%d bytes):\n", ret);
|
printf("Encoded DER (%d bytes):\n", ret);
|
||||||
|
@ -244,10 +253,14 @@ static int signedData_verify(byte* in, word32 inSz, byte* cert,
|
||||||
|
|
||||||
if (ret < 0 || (pkcs7->contentSz != sizeof(data)) ||
|
if (ret < 0 || (pkcs7->contentSz != sizeof(data)) ||
|
||||||
(XMEMCMP(pkcs7->content, data, pkcs7->contentSz) != 0)) {
|
(XMEMCMP(pkcs7->content, data, pkcs7->contentSz) != 0)) {
|
||||||
printf("ERROR: Failed to verify SignedData bundle, ret = %d\n", ret);
|
if (ret == PKCS7_SIGNEEDS_CHECK) {
|
||||||
wc_PKCS7_Free(pkcs7);
|
printf("WARNING: Parsed through bundle but no certificates found to"
|
||||||
return -1;
|
" verify signature with\n");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("ERROR: Failed to verify SignedData bundle, ret = %d\n",
|
||||||
|
ret);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Successfully verified SignedData bundle.\n");
|
printf("Successfully verified SignedData bundle.\n");
|
||||||
|
|
||||||
|
@ -287,7 +300,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
/* no attributes */
|
/* no attributes */
|
||||||
encryptedSz = signedData_sign_noattrs(cert, certSz, key, keySz,
|
encryptedSz = signedData_sign_noattrs(cert, certSz, key, keySz,
|
||||||
encrypted, sizeof(encrypted));
|
encrypted, sizeof(encrypted), 0, 0);
|
||||||
if (encryptedSz < 0)
|
if (encryptedSz < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
@ -297,6 +310,19 @@ int main(int argc, char** argv)
|
||||||
if (decryptedSz < 0)
|
if (decryptedSz < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
/* no attributes, stream mode, and no certs */
|
||||||
|
encryptedSz = signedData_sign_noattrs(cert, certSz, key, keySz,
|
||||||
|
encrypted, sizeof(encrypted), 1, 1);
|
||||||
|
if (encryptedSz < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
decryptedSz = signedData_verify(encrypted, encryptedSz,
|
||||||
|
cert, certSz, key, keySz,
|
||||||
|
decrypted, sizeof(decrypted));
|
||||||
|
/* should be error to warn that the signature needs checked */
|
||||||
|
if (decryptedSz != PKCS7_SIGNEEDS_CHECK)
|
||||||
|
return -1;
|
||||||
|
|
||||||
/* default attributes + messageType attribute */
|
/* default attributes + messageType attribute */
|
||||||
encryptedSz = signedData_sign_attrs(cert, certSz, key, keySz,
|
encryptedSz = signedData_sign_attrs(cert, certSz, key, keySz,
|
||||||
encrypted, sizeof(encrypted));
|
encrypted, sizeof(encrypted));
|
||||||
|
|
Loading…
Reference in New Issue