Skip to content

Commit 5e611ed

Browse files
author
John Bland
committed
fix bind_wc_PKCS7_EncodeSignedData and implement wc_PKCS7_GetAttributeValue and wc_PKCS7_GetSignerSID
bind_wc_PKCS7_EncodeSignedData required that the wc_PKCS7_SetSignerIdentifierType was called and that pkcs7->publicKeyOID was set to the same value as encryptOID, now it runs without error implemented wc_PKCS7_GetAttributeValue and wc_PKCS7_GetSignerSID bindings
1 parent 4ba2a03 commit 5e611ed

File tree

5 files changed

+211
-6
lines changed

5 files changed

+211
-6
lines changed

addon/wolfcrypt/h/pkcs7.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121
#include <napi.h>
22-
#include "wolfssl/options.h"
22+
#include <wolfssl/options.h>
2323
#include <wolfssl/wolfcrypt/settings.h>
24+
#include <wolfssl/wolfcrypt/error-crypt.h>
2425
#include <wolfssl/wolfcrypt/pkcs7.h>
2526

2627
Napi::Number sizeof_PKCS7(const Napi::CallbackInfo& info);
@@ -32,4 +33,8 @@ Napi::Number bind_wc_PKCS7_AddCertificate(const Napi::CallbackInfo& info);
3233
Napi::Number bind_wc_PKCS7_EncodeData(const Napi::CallbackInfo& info);
3334
Napi::Number bind_wc_PKCS7_EncodeSignedData(const Napi::CallbackInfo& info);
3435
Napi::Number bind_wc_PKCS7_VerifySignedData(const Napi::CallbackInfo& info);
36+
Napi::Number sizeof_wc_PKCS7_GetAttributeValue(const Napi::CallbackInfo& info);
37+
Napi::Number bind_wc_PKCS7_GetAttributeValue(const Napi::CallbackInfo& info);
38+
Napi::Number sizeof_wc_PKCS7_GetSignerSID(const Napi::CallbackInfo& info);
39+
Napi::Number bind_wc_PKCS7_GetSignerSID(const Napi::CallbackInfo& info);
3540
void bind_wc_PKCS7_Free(const Napi::CallbackInfo& info);

addon/wolfcrypt/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
123123
exports.Set(Napi::String::New(env, "wc_PKCS7_EncodeData"), Napi::Function::New(env, bind_wc_PKCS7_EncodeData));
124124
exports.Set(Napi::String::New(env, "wc_PKCS7_EncodeSignedData"), Napi::Function::New(env, bind_wc_PKCS7_EncodeSignedData));
125125
exports.Set(Napi::String::New(env, "wc_PKCS7_VerifySignedData"), Napi::Function::New(env, bind_wc_PKCS7_VerifySignedData));
126+
exports.Set(Napi::String::New(env, "sizeof_wc_PKCS7_GetAttributeValue"), Napi::Function::New(env, sizeof_wc_PKCS7_GetAttributeValue));
127+
exports.Set(Napi::String::New(env, "wc_PKCS7_GetAttributeValue"), Napi::Function::New(env, bind_wc_PKCS7_GetAttributeValue));
128+
exports.Set(Napi::String::New(env, "sizeof_wc_PKCS7_GetSignerSID"), Napi::Function::New(env, sizeof_wc_PKCS7_GetSignerSID));
129+
exports.Set(Napi::String::New(env, "wc_PKCS7_GetSignerSID"), Napi::Function::New(env, bind_wc_PKCS7_GetSignerSID));
126130
exports.Set(Napi::String::New(env, "wc_PKCS7_Free"), Napi::Function::New(env, bind_wc_PKCS7_Free));
127131

128132
return exports;

addon/wolfcrypt/pkcs7.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,19 @@ Napi::Number bind_wc_PKCS7_EncodeSignedData(const Napi::CallbackInfo& info)
223223

224224
wc_InitRng( &rng );
225225

226+
ret = wc_PKCS7_SetSignerIdentifierType( pkcs7, CMS_SKID );
227+
228+
if ( ret != 0 )
229+
{
230+
return Napi::Number::New( env, ret );
231+
}
232+
226233
pkcs7->content = data;
227234
pkcs7->contentSz = data_size;
228235
pkcs7->privateKey = key;
229236
pkcs7->privateKeySz = key_size;
230237
pkcs7->encryptOID = key_sum;
238+
pkcs7->publicKeyOID = key_sum;
231239
pkcs7->hashOID = hash_sum;
232240
pkcs7->rng = &rng;
233241

@@ -251,6 +259,80 @@ Napi::Number bind_wc_PKCS7_VerifySignedData(const Napi::CallbackInfo& info)
251259
return Napi::Number::New( env, ret );
252260
}
253261

262+
Napi::Number sizeof_wc_PKCS7_GetAttributeValue(const Napi::CallbackInfo& info)
263+
{
264+
int ret;
265+
Napi::Env env = info.Env();
266+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
267+
uint8_t* oid = info[1].As<Napi::Uint8Array>().Data();
268+
unsigned int oid_size = info[2].As<Napi::Number>().Int32Value();
269+
unsigned int out_size;
270+
271+
ret = wc_PKCS7_GetAttributeValue( pkcs7, oid, oid_size, NULL, &out_size );
272+
273+
if ( ret != LENGTH_ONLY_E )
274+
{
275+
return Napi::Number::New( env, ret );
276+
}
277+
278+
return Napi::Number::New( env, out_size );
279+
}
280+
281+
Napi::Number bind_wc_PKCS7_GetAttributeValue(const Napi::CallbackInfo& info)
282+
{
283+
int ret;
284+
Napi::Env env = info.Env();
285+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
286+
uint8_t* oid = info[1].As<Napi::Uint8Array>().Data();
287+
unsigned int oid_size = info[2].As<Napi::Number>().Int32Value();
288+
uint8_t* out = info[3].As<Napi::Uint8Array>().Data();
289+
unsigned int out_size = info[4].As<Napi::Number>().Int32Value();
290+
291+
ret = wc_PKCS7_GetAttributeValue( pkcs7, oid, oid_size, out, &out_size );
292+
293+
if ( ret < 0 )
294+
{
295+
return Napi::Number::New( env, ret );
296+
}
297+
298+
return Napi::Number::New( env, out_size );
299+
}
300+
301+
Napi::Number sizeof_wc_PKCS7_GetSignerSID(const Napi::CallbackInfo& info)
302+
{
303+
int ret;
304+
Napi::Env env = info.Env();
305+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
306+
unsigned int out_size;
307+
308+
ret = wc_PKCS7_GetSignerSID( pkcs7, NULL, &out_size );
309+
310+
if ( ret != LENGTH_ONLY_E )
311+
{
312+
return Napi::Number::New( env, ret );
313+
}
314+
315+
return Napi::Number::New( env, out_size );
316+
}
317+
318+
Napi::Number bind_wc_PKCS7_GetSignerSID(const Napi::CallbackInfo& info)
319+
{
320+
int ret;
321+
Napi::Env env = info.Env();
322+
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );
323+
uint8_t* out = info[1].As<Napi::Uint8Array>().Data();
324+
unsigned int out_size = info[2].As<Napi::Number>().Int32Value();
325+
326+
ret = wc_PKCS7_GetSignerSID( pkcs7, out, &out_size );
327+
328+
if ( ret < 0 )
329+
{
330+
return Napi::Number::New( env, ret );
331+
}
332+
333+
return Napi::Number::New( env, out_size );
334+
}
335+
254336
void bind_wc_PKCS7_Free(const Napi::CallbackInfo& info)
255337
{
256338
PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() );

interfaces/pkcs7.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class WolfSSL_PKCS7
130130

131131
let ret = wolfcrypt.wc_PKCS7_EncodeSignedData( this.pkcs7, data, data.length, key, key.length, keySumType, hashSumType, outBuf, outBuf.length )
132132

133-
if ( ret != 0 )
133+
if ( ret <= 0 )
134134
{
135135
throw `Failed to wc_PKCS7_EncodeSignedData ${ ret }`
136136
}
@@ -160,6 +160,54 @@ class WolfSSL_PKCS7
160160
}
161161
}
162162

163+
GetAttributeValue( oid )
164+
{
165+
if ( this.pkcs7 == null )
166+
{
167+
throw 'Pkcs7 not allocated'
168+
}
169+
170+
if ( typeof oid == 'string' )
171+
{
172+
oid = Buffer.from( oid )
173+
}
174+
175+
if ( !Buffer.isBuffer( oid ) )
176+
{
177+
throw `oid must be a Buffer or string`
178+
}
179+
180+
let outBuf = Buffer.alloc( wolfcrypt.sizeof_wc_PKCS7_GetAttributeValue( this.pkcs7, oid, oid.length ) )
181+
182+
let ret = wolfcrypt.wc_PKCS7_GetAttributeValue( this.pkcs7, oid, oid.length, outBuf, outBuf.length )
183+
184+
if ( ret <= 0 )
185+
{
186+
throw `Failed to wc_PKCS7_GetAttributeValue ${ ret }`
187+
}
188+
189+
return outBuf
190+
}
191+
192+
GetSignerSID()
193+
{
194+
if ( this.pkcs7 == null )
195+
{
196+
throw 'Pkcs7 not allocated'
197+
}
198+
199+
let outBuf = Buffer.alloc( wolfcrypt.sizeof_wc_PKCS7_GetSignerSID( this.pkcs7 ) )
200+
201+
let ret = wolfcrypt.wc_PKCS7_GetSignerSID( this.pkcs7, outBuf, outBuf.length )
202+
203+
if ( ret <= 0 )
204+
{
205+
throw `Failed to wc_PKCS7_GetSignerSID ${ ret }`
206+
}
207+
208+
return outBuf
209+
}
210+
163211
free()
164212
{
165213
wolfcrypt.wc_PKCS7_Free( this.pkcs7 )

tests/pkcs7.js

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ const pkcs7_tests =
4747

4848
const encoded = pkcs7.EncodeData( message, key )
4949

50-
console.log( encoded.toString() )
51-
5250
pkcs7.free()
5351

5452
console.log( 'PASS pkcs7 encodeData' )
@@ -64,11 +62,79 @@ const pkcs7_tests =
6462

6563
const encoded = pkcs7.EncodeSignedData( message, key, 'RSA', 'SHA' )
6664

67-
console.log( encoded.toString() )
65+
pkcs7.free()
66+
67+
pkcs7 = new WolfSSL_PKCS7()
68+
69+
pkcs7.VerifySignedData( encoded )
70+
71+
console.log( 'PASS pkcs7 signVerify' )
72+
},
73+
74+
getAttribute: function()
75+
{
76+
const cert = fs.readFileSync( './client-cert.der' )
77+
const key = fs.readFileSync( './client-key.der' )
78+
79+
let pkcs7 = new WolfSSL_PKCS7()
80+
pkcs7.AddCertificate( cert )
81+
82+
const encoded = pkcs7.EncodeSignedData( message, key, 'RSA', 'SHA' )
6883

6984
pkcs7.free()
7085

71-
console.log( 'PASS pkcs7 encodeData' )
86+
pkcs7 = new WolfSSL_PKCS7()
87+
88+
pkcs7.VerifySignedData( encoded )
89+
90+
// oid identifier for data
91+
const data = pkcs7.GetAttributeValue( Buffer.from( '2a864886f70d010904', 'hex' ) )
92+
93+
console.log( 'PASS pkcs7 getAttribute' )
94+
},
95+
96+
getAttribute: function()
97+
{
98+
const cert = fs.readFileSync( './client-cert.der' )
99+
const key = fs.readFileSync( './client-key.der' )
100+
101+
let pkcs7 = new WolfSSL_PKCS7()
102+
pkcs7.AddCertificate( cert )
103+
104+
const encoded = pkcs7.EncodeSignedData( message, key, 'RSA', 'SHA' )
105+
106+
pkcs7.free()
107+
108+
pkcs7 = new WolfSSL_PKCS7()
109+
110+
pkcs7.VerifySignedData( encoded )
111+
112+
// oid identifier for data
113+
const data = pkcs7.GetAttributeValue( Buffer.from( '2a864886f70d010904', 'hex' ) )
114+
115+
console.log( 'PASS pkcs7 getAttribute' )
116+
},
117+
118+
getSid: function()
119+
{
120+
const cert = fs.readFileSync( './client-cert.der' )
121+
const key = fs.readFileSync( './client-key.der' )
122+
123+
let pkcs7 = new WolfSSL_PKCS7()
124+
pkcs7.AddCertificate( cert )
125+
126+
const encoded = pkcs7.EncodeSignedData( message, key, 'RSA', 'SHA' )
127+
128+
pkcs7.free()
129+
130+
pkcs7 = new WolfSSL_PKCS7()
131+
132+
pkcs7.VerifySignedData( encoded )
133+
134+
// oid identifier for data
135+
const data = pkcs7.GetSignerSID()
136+
137+
console.log( 'PASS pkcs7 getSid' )
72138
},
73139
}
74140

0 commit comments

Comments
 (0)