@@ -183,7 +183,7 @@ func TestImportRSAPublicKeyBlob(t *testing.T) {
183183 AiKeyAlg : 0x0000A400 ,
184184 })
185185 _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
186- Magic : 0x12345678 ,
186+ Magic : magicRSA2 ,
187187 BitLen : 2048 ,
188188 PubExp : 65537 ,
189189 })
@@ -240,6 +240,216 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
240240 require .NoError (t , err )
241241 require .NotNil (t , privateKey )
242242 })
243+
244+ t .Run ("invalid blob header" , func (t * testing.T ) {
245+ privateKey , err := ImportRSAPrivateKeyBlob (nil )
246+ require .EqualError (t , err , "failed to read blob header: EOF" )
247+ require .Nil (t , privateKey )
248+ })
249+
250+ t .Run ("invalid blob type" , func (t * testing.T ) {
251+ buf := new (bytes.Buffer )
252+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
253+ Type : publicKeyBlob ,
254+ Version : curBlobVersion ,
255+ AiKeyAlg : 0x0000A400 ,
256+ })
257+
258+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
259+ require .EqualError (t , err , "invalid blob type" )
260+ require .Nil (t , privateKey )
261+ })
262+
263+ t .Run ("invalid blob version" , func (t * testing.T ) {
264+ buf := new (bytes.Buffer )
265+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
266+ Type : privateKeyBlob ,
267+ Version : 1 ,
268+ AiKeyAlg : 0x0000A400 ,
269+ })
270+
271+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
272+ require .EqualError (t , err , "invalid blob version" )
273+ require .Nil (t , privateKey )
274+ })
275+
276+ t .Run ("failed to read blob public key" , func (t * testing.T ) {
277+ buf := new (bytes.Buffer )
278+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
279+ Type : privateKeyBlob ,
280+ Version : curBlobVersion ,
281+ AiKeyAlg : 0x0000A400 ,
282+ })
283+ _ = binary .Write (buf , binary .LittleEndian , uint32 (magicRSA1 ))
284+
285+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
286+ require .EqualError (t , err , "failed to read blob private key: unexpected EOF" )
287+ require .Nil (t , privateKey )
288+ })
289+
290+ t .Run ("invalid blob magic" , func (t * testing.T ) {
291+ buf := new (bytes.Buffer )
292+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
293+ Type : privateKeyBlob ,
294+ Version : curBlobVersion ,
295+ AiKeyAlg : 0x0000A400 ,
296+ })
297+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
298+ Magic : magicRSA1 ,
299+ BitLen : 2048 ,
300+ PubExp : 65537 ,
301+ })
302+
303+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
304+ require .EqualError (t , err , "invalid blob magic" )
305+ require .Nil (t , privateKey )
306+ })
307+
308+ t .Run ("invalid blob bit length" , func (t * testing.T ) {
309+ buf := new (bytes.Buffer )
310+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
311+ Type : privateKeyBlob ,
312+ Version : curBlobVersion ,
313+ AiKeyAlg : 0x0000A400 ,
314+ })
315+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
316+ Magic : magicRSA2 ,
317+ BitLen : 2047 ,
318+ PubExp : 65537 ,
319+ })
320+
321+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
322+ require .EqualError (t , err , "invalid blob bit length" )
323+ require .Nil (t , privateKey )
324+ })
325+
326+ t .Run ("failed to read modulus" , func (t * testing.T ) {
327+ buf := new (bytes.Buffer )
328+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
329+ Type : privateKeyBlob ,
330+ Version : curBlobVersion ,
331+ AiKeyAlg : 0x0000A400 ,
332+ })
333+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
334+ Magic : magicRSA2 ,
335+ BitLen : 2048 ,
336+ PubExp : 65537 ,
337+ })
338+ _ = binary .Write (buf , binary .LittleEndian , []byte {0x01 })
339+
340+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
341+ require .EqualError (t , err , "failed to read modulus: unexpected EOF" )
342+ require .Nil (t , privateKey )
343+ })
344+
345+ t .Run ("failed to read prime1" , func (t * testing.T ) {
346+ buf := new (bytes.Buffer )
347+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
348+ Type : privateKeyBlob ,
349+ Version : curBlobVersion ,
350+ AiKeyAlg : 0x0000A400 ,
351+ })
352+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
353+ Magic : magicRSA2 ,
354+ BitLen : 2048 ,
355+ PubExp : 65537 ,
356+ })
357+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x01 }, 256 ))
358+ _ = binary .Write (buf , binary .LittleEndian , []byte {0x02 })
359+
360+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
361+ require .EqualError (t , err , "failed to read prime1: unexpected EOF" )
362+ require .Nil (t , privateKey )
363+ })
364+
365+ t .Run ("failed to read prime2" , func (t * testing.T ) {
366+ buf := new (bytes.Buffer )
367+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
368+ Type : privateKeyBlob ,
369+ Version : curBlobVersion ,
370+ AiKeyAlg : 0x0000A400 ,
371+ })
372+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
373+ Magic : magicRSA2 ,
374+ BitLen : 2048 ,
375+ PubExp : 65537 ,
376+ })
377+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x01 }, 256 ))
378+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x02 }, 128 ))
379+ _ = binary .Write (buf , binary .LittleEndian , []byte {0x03 })
380+
381+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
382+ require .EqualError (t , err , "failed to read prime2: unexpected EOF" )
383+ require .Nil (t , privateKey )
384+ })
385+
386+ t .Run ("failed to read skipped fields" , func (t * testing.T ) {
387+ buf := new (bytes.Buffer )
388+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
389+ Type : privateKeyBlob ,
390+ Version : curBlobVersion ,
391+ AiKeyAlg : 0x0000A400 ,
392+ })
393+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
394+ Magic : magicRSA2 ,
395+ BitLen : 2048 ,
396+ PubExp : 65537 ,
397+ })
398+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x01 }, 256 ))
399+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x02 }, 128 ))
400+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x03 }, 128 ))
401+ _ = binary .Write (buf , binary .LittleEndian , []byte {0x00 })
402+
403+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
404+ require .EqualError (t , err , "failed to read skipped fields: unexpected EOF" )
405+ require .Nil (t , privateKey )
406+ })
407+
408+ t .Run ("failed to read private exponent" , func (t * testing.T ) {
409+ buf := new (bytes.Buffer )
410+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
411+ Type : privateKeyBlob ,
412+ Version : curBlobVersion ,
413+ AiKeyAlg : 0x0000A400 ,
414+ })
415+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
416+ Magic : magicRSA2 ,
417+ BitLen : 2048 ,
418+ PubExp : 65537 ,
419+ })
420+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x01 }, 256 ))
421+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x02 }, 128 ))
422+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x03 }, 128 ))
423+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x00 }, 128 * 3 ))
424+ _ = binary .Write (buf , binary .LittleEndian , []byte {0x04 })
425+
426+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
427+ require .EqualError (t , err , "failed to read private exponent: unexpected EOF" )
428+ require .Nil (t , privateKey )
429+ })
430+
431+ t .Run ("invalid private key validation" , func (t * testing.T ) {
432+ buf := new (bytes.Buffer )
433+ _ = binary .Write (buf , binary .LittleEndian , blobHeader {
434+ Type : privateKeyBlob ,
435+ Version : curBlobVersion ,
436+ AiKeyAlg : 0x0000A400 ,
437+ })
438+ _ = binary .Write (buf , binary .LittleEndian , rsaPubKey {
439+ Magic : magicRSA2 ,
440+ BitLen : 2048 ,
441+ PubExp : 65537 ,
442+ })
443+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x01 }, 256 ))
444+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x02 }, 128 ))
445+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x02 }, 128 )) // same as prime1
446+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x00 }, 128 * 3 ))
447+ _ = binary .Write (buf , binary .LittleEndian , bytes .Repeat ([]byte {0x04 }, 256 ))
448+
449+ privateKey , err := ImportRSAPrivateKeyBlob (buf .Bytes ())
450+ require .ErrorContains (t , err , "failed to validate private key" )
451+ require .Nil (t , privateKey )
452+ })
243453}
244454
245455func TestExportRSAPublicKeyBlob (t * testing.T ) {
0 commit comments