Skip to content

Commit d4d9460

Browse files
author
John Bland
committed
implement pkcs7 functions, encode signed broken
1 parent f314c4a commit d4d9460

File tree

9 files changed

+559
-1
lines changed

9 files changed

+559
-1
lines changed

addon/wolfcrypt/h/pkcs7.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* hmac.h
2+
*
3+
* Copyright (C) 2006-2022 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
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-1335, USA
20+
*/
21+
#include <napi.h>
22+
#include "wolfssl/options.h"
23+
#include <wolfssl/wolfcrypt/settings.h>
24+
#include <wolfssl/wolfcrypt/pkcs7.h>
25+
26+
Napi::Number sizeof_PKCS7(const Napi::CallbackInfo& info);
27+
Napi::Number typeof_Key_Sum(const Napi::CallbackInfo& info);
28+
Napi::Number typeof_Hash_Sum(const Napi::CallbackInfo& info);
29+
Napi::Number bind_wc_PKCS7_Init(const Napi::CallbackInfo& info);
30+
Napi::Number bind_wc_PKCS7_InitWithCert(const Napi::CallbackInfo& info);
31+
Napi::Number bind_wc_PKCS7_AddCertificate(const Napi::CallbackInfo& info);
32+
Napi::Number bind_wc_PKCS7_EncodeData(const Napi::CallbackInfo& info);
33+
Napi::Number bind_wc_PKCS7_EncodeSignedData(const Napi::CallbackInfo& info);
34+
Napi::Number bind_wc_PKCS7_VerifySignedData(const Napi::CallbackInfo& info);
35+
void bind_wc_PKCS7_Free(const Napi::CallbackInfo& info);

addon/wolfcrypt/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "./h/rsa.h"
2727
#include "./h/sha.h"
2828
#include "./h/ecc.h"
29+
#include "./h/pkcs7.h"
2930

3031
using namespace Napi;
3132

@@ -113,6 +114,17 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
113114
exports.Set(Napi::String::New(env, "wc_ecc_verify_hash"), Napi::Function::New(env, bind_wc_ecc_verify_hash));
114115
exports.Set(Napi::String::New(env, "wc_ecc_free"), Napi::Function::New(env, bind_wc_ecc_free));
115116

117+
exports.Set(Napi::String::New(env, "sizeof_PKCS7"), Napi::Function::New(env, sizeof_PKCS7));
118+
exports.Set(Napi::String::New(env, "typeof_Key_Sum"), Napi::Function::New(env, typeof_Key_Sum));
119+
exports.Set(Napi::String::New(env, "typeof_Hash_Sum"), Napi::Function::New(env, typeof_Hash_Sum));
120+
exports.Set(Napi::String::New(env, "wc_PKCS7_Init"), Napi::Function::New(env, bind_wc_PKCS7_Init));
121+
exports.Set(Napi::String::New(env, "wc_PKCS7_InitWithCert"), Napi::Function::New(env, bind_wc_PKCS7_InitWithCert));
122+
exports.Set(Napi::String::New(env, "wc_PKCS7_AddCertificate"), Napi::Function::New(env, bind_wc_PKCS7_AddCertificate));
123+
exports.Set(Napi::String::New(env, "wc_PKCS7_EncodeData"), Napi::Function::New(env, bind_wc_PKCS7_EncodeData));
124+
exports.Set(Napi::String::New(env, "wc_PKCS7_EncodeSignedData"), Napi::Function::New(env, bind_wc_PKCS7_EncodeSignedData));
125+
exports.Set(Napi::String::New(env, "wc_PKCS7_VerifySignedData"), Napi::Function::New(env, bind_wc_PKCS7_VerifySignedData));
126+
exports.Set(Napi::String::New(env, "wc_PKCS7_Free"), Napi::Function::New(env, bind_wc_PKCS7_Free));
127+
116128
return exports;
117129
}
118130

addon/wolfcrypt/pkcs7.cpp

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
/* pkcs7.cpp
2+
*
3+
* Copyright (C) 2006-2022 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
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-1335, USA
20+
*/
21+
#include "./h/pkcs7.h"
22+
23+
Napi::Number sizeof_PKCS7(const Napi::CallbackInfo& info)
24+
{
25+
Napi::Env env = info.Env();
26+
27+
return Napi::Number::New( env, sizeof( PKCS7 ) );
28+
}
29+
30+
Napi::Number typeof_Key_Sum(const Napi::CallbackInfo& info)
31+
{
32+
int ret = -1;
33+
Napi::Env env = info.Env();
34+
std::string type = info[0].As<Napi::String>().Utf8Value();
35+
36+
if ( strcmp( type.c_str(), "DSA" ) == 0 )
37+
{
38+
ret = DSAk;
39+
}
40+
else if ( strcmp( type.c_str(), "RSA" ) == 0 )
41+
{
42+
ret = RSAk;
43+
}
44+
else if ( strcmp( type.c_str(), "ECDSA" ) == 0 )
45+
{
46+
ret = ECDSAk;
47+
}
48+
else if ( strcmp( type.c_str(), "ED25519" ) == 0 )
49+
{
50+
ret = ED25519k;
51+
}
52+
else if ( strcmp( type.c_str(), "X25519" ) == 0 )
53+
{
54+
ret = X25519k;
55+
}
56+
else if ( strcmp( type.c_str(), "ED448" ) == 0 )
57+
{
58+
ret = ED448k;
59+
}
60+
else if ( strcmp( type.c_str(), "X448" ) == 0 )
61+
{
62+
ret = X448k;
63+
}
64+
else if ( strcmp( type.c_str(), "DH" ) == 0 )
65+
{
66+
ret = DHk;
67+
}
68+
else if ( strcmp( type.c_str(), "FALCON_LEVEL1" ) == 0 )
69+
{
70+
ret = FALCON_LEVEL1k;
71+
}
72+
else if ( strcmp( type.c_str(), "FALCON_LEVEL5" ) == 0 )
73+
{
74+
ret = FALCON_LEVEL5k;
75+
}
76+
77+
return Napi::Number::New( env, ret );
78+
}
79+
80+
Napi::Number typeof_Hash_Sum(const Napi::CallbackInfo& info)
81+
{
82+
int ret = -1;
83+
Napi::Env env = info.Env();
84+
std::string type = info[0].As<Napi::String>().Utf8Value();
85+
86+
if ( strcmp( type.c_str(), "MD2" ) == 0 )
87+
{
88+
ret = MD2h;
89+
}
90+
else if ( strcmp( type.c_str(), "MD5" ) == 0 )
91+
{
92+
ret = MD5h;
93+
}
94+
else if ( strcmp( type.c_str(), "SHA" ) == 0 )
95+
{
96+
ret = SHAh;
97+
}
98+
else if ( strcmp( type.c_str(), "SHA224" ) == 0 )
99+
{
100+
ret = SHA224h;
101+
}
102+
else if ( strcmp( type.c_str(), "SHA256" ) == 0 )
103+
{
104+
ret = SHA256h;
105+
}
106+
else if ( strcmp( type.c_str(), "SHA384" ) == 0 )
107+
{
108+
ret = SHA384h;
109+
}
110+
else if ( strcmp( type.c_str(), "SHA512" ) == 0 )
111+
{
112+
ret = SHA512h;
113+
}
114+
else if ( strcmp( type.c_str(), "SHA512_224" ) == 0 )
115+
{
116+
ret = SHA512_224h;
117+
}
118+
else if ( strcmp( type.c_str(), "SHA512_256" ) == 0 )
119+
{
120+
ret = SHA512_256h;
121+
}
122+
else if ( strcmp( type.c_str(), "SHA3_224" ) == 0 )
123+
{
124+
ret = SHA3_224h;
125+
}
126+
else if ( strcmp( type.c_str(), "SHA3_256" ) == 0 )
127+
{
128+
ret = SHA3_256h;
129+
}
130+
else if ( strcmp( type.c_str(), "SHA3_384" ) == 0 )
131+
{
132+
ret = SHA3_384h;
133+
}
134+
else if ( strcmp( type.c_str(), "SHA3_512" ) == 0 )
135+
{
136+
ret = SHA3_512h;
137+
}
138+
else if ( strcmp( type.c_str(), "SHAKE128" ) == 0 )
139+
{
140+
ret = SHAKE128h;
141+
}
142+
else if ( strcmp( type.c_str(), "SHAKE256" ) == 0 )
143+
{
144+
ret = SHAKE256h;
145+
}
146+
147+
return Napi::Number::New( env, ret );
148+
}
149+
150+
Napi::Number bind_wc_PKCS7_Init(const Napi::CallbackInfo& info)
151+
{
152+
int ret;
153+
Napi::Env env = info.Env();
154+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
155+
156+
ret = wc_PKCS7_Init( pkcs7, NULL, INVALID_DEVID );
157+
158+
return Napi::Number::New( env, ret );
159+
}
160+
161+
Napi::Number bind_wc_PKCS7_InitWithCert(const Napi::CallbackInfo& info)
162+
{
163+
int ret;
164+
Napi::Env env = info.Env();
165+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
166+
uint8_t* cert = info[1].As<Napi::Uint8Array>().Data();
167+
int cert_size = info[2].As<Napi::Number>().Int32Value();
168+
169+
ret = wc_PKCS7_InitWithCert( pkcs7, cert, cert_size );
170+
171+
return Napi::Number::New( env, ret );
172+
}
173+
174+
Napi::Number bind_wc_PKCS7_AddCertificate(const Napi::CallbackInfo& info)
175+
{
176+
int ret;
177+
Napi::Env env = info.Env();
178+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
179+
uint8_t* cert = info[1].As<Napi::Uint8Array>().Data();
180+
int cert_size = info[2].As<Napi::Number>().Int32Value();
181+
182+
ret = wc_PKCS7_AddCertificate( pkcs7, cert, cert_size );
183+
184+
return Napi::Number::New( env, ret );
185+
}
186+
187+
Napi::Number bind_wc_PKCS7_EncodeData(const Napi::CallbackInfo& info)
188+
{
189+
int ret;
190+
Napi::Env env = info.Env();
191+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
192+
uint8_t* data = info[1].As<Napi::Uint8Array>().Data();
193+
int data_size = info[2].As<Napi::Number>().Int32Value();
194+
uint8_t* key = info[3].As<Napi::Uint8Array>().Data();
195+
int key_size = info[4].As<Napi::Number>().Int32Value();
196+
uint8_t* output = info[5].As<Napi::Uint8Array>().Data();
197+
int output_size = info[6].As<Napi::Number>().Int32Value();
198+
199+
pkcs7->content = data;
200+
pkcs7->contentSz = data_size;
201+
pkcs7->privateKey = key;
202+
pkcs7->privateKeySz = key_size;
203+
204+
ret = wc_PKCS7_EncodeData( pkcs7, output, output_size );
205+
206+
return Napi::Number::New( env, ret );
207+
}
208+
209+
Napi::Number bind_wc_PKCS7_EncodeSignedData(const Napi::CallbackInfo& info)
210+
{
211+
int ret;
212+
Napi::Env env = info.Env();
213+
WC_RNG rng;
214+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
215+
uint8_t* data = info[1].As<Napi::Uint8Array>().Data();
216+
int data_size = info[2].As<Napi::Number>().Int32Value();
217+
uint8_t* key = info[3].As<Napi::Uint8Array>().Data();
218+
int key_size = info[4].As<Napi::Number>().Int32Value();
219+
int key_sum = info[5].As<Napi::Number>().Int32Value();
220+
int hash_sum = info[6].As<Napi::Number>().Int32Value();
221+
uint8_t* output = info[7].As<Napi::Uint8Array>().Data();
222+
int output_size = info[8].As<Napi::Number>().Int32Value();
223+
224+
wc_InitRng( &rng );
225+
226+
pkcs7->content = data;
227+
pkcs7->contentSz = data_size;
228+
pkcs7->privateKey = key;
229+
pkcs7->privateKeySz = key_size;
230+
pkcs7->encryptOID = key_sum;
231+
pkcs7->hashOID = hash_sum;
232+
pkcs7->rng = &rng;
233+
234+
ret = wc_PKCS7_EncodeSignedData( pkcs7, output, output_size );
235+
236+
wc_FreeRng( &rng );
237+
238+
return Napi::Number::New( env, ret );
239+
}
240+
241+
Napi::Number bind_wc_PKCS7_VerifySignedData(const Napi::CallbackInfo& info)
242+
{
243+
int ret;
244+
Napi::Env env = info.Env();
245+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
246+
uint8_t* in = info[1].As<Napi::Uint8Array>().Data();
247+
int in_size = info[2].As<Napi::Number>().Int32Value();
248+
249+
ret = wc_PKCS7_VerifySignedData( pkcs7, in, in_size );
250+
251+
return Napi::Number::New( env, ret );
252+
}
253+
254+
void bind_wc_PKCS7_Free(const Napi::CallbackInfo& info)
255+
{
256+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
257+
258+
wc_PKCS7_Free( pkcs7 );
259+
}

app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const hmac_tests = require( './tests/hmac' );
2323
const rsa_tests = require( './tests/rsa' );
2424
const sha_tests = require( './tests/sha' );
2525
const ecc_tests = require( './tests/ecc' );
26+
const pkcs7_tests = require( './tests/pkcs7' );
2627

2728
(async function() {
2829
for ( const key of Object.keys( evp_tests ) )
@@ -49,4 +50,9 @@ const ecc_tests = require( './tests/ecc' );
4950
{
5051
await ecc_tests[key]()
5152
}
53+
54+
for ( const key of Object.keys( pkcs7_tests ) )
55+
{
56+
await pkcs7_tests[key]()
57+
}
5258
})()

binding.gyp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"addon/wolfcrypt/hmac.cpp",
1010
"addon/wolfcrypt/rsa.cpp",
1111
"addon/wolfcrypt/sha.cpp",
12-
"addon/wolfcrypt/ecc.cpp"
12+
"addon/wolfcrypt/ecc.cpp",
13+
"addon/wolfcrypt/pkcs7.cpp"
1314
],
1415
'include_dirs': [
1516
"<!@(node -p \"require('node-addon-api').include\")"

client-cert.der

1.03 KB
Binary file not shown.

client-key.der

608 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)