@@ -7,35 +7,66 @@ const { JWK: { importKey }, JWKS: { KeyStore } } = require('../..')
77test ( 'public EC' , t => {
88 const jwk = recipes . get ( '3.1' )
99 const key = importKey ( jwk )
10+ t . true ( key . toPEM ( ) . includes ( 'BEGIN PUBLIC KEY' ) )
1011 t . deepEqual ( key . toJWK ( ) , jwk )
1112 t . deepEqual ( key . toJWK ( false ) , jwk )
1213 t . throws ( ( ) => {
1314 key . toJWK ( true )
1415 } , { instanceOf : TypeError , message : 'public key cannot be exported as private' } )
16+ t . throws ( ( ) => {
17+ key . toPEM ( false , { cipher : 'aes-256-cbc' } )
18+ } , { instanceOf : TypeError , message : 'cipher and passphrase can only be applied when exporting private keys' } )
19+ t . throws ( ( ) => {
20+ key . toPEM ( false , { passphrase : 'top secret' } )
21+ } , { instanceOf : TypeError , message : 'cipher and passphrase can only be applied when exporting private keys' } )
22+ t . throws ( ( ) => {
23+ key . toPEM ( true )
24+ } , { instanceOf : TypeError , message : 'public key cannot be exported as private' } )
1525} )
1626
1727test ( 'private EC' , t => {
1828 const jwk = recipes . get ( '3.2' )
1929 const key = importKey ( jwk )
30+ t . true ( key . toPEM ( true , { cipher : 'aes-256-cbc' , passphrase : 'top secret' } ) . includes ( 'BEGIN ENCRYPTED PRIVATE KEY' ) )
31+ t . true ( key . toPEM ( true , { type : 'sec1' } ) . includes ( 'BEGIN EC PRIVATE KEY' ) )
32+ t . true ( key . toPEM ( true , { type : 'sec1' , cipher : 'aes-256-cbc' , passphrase : 'top secret' } ) . includes ( 'ENCRYPTED' ) )
33+ t . true ( key . toPEM ( true ) . includes ( 'BEGIN PRIVATE KEY' ) )
34+ t . true ( key . toPEM ( ) . includes ( 'BEGIN PUBLIC KEY' ) )
2035 t . deepEqual ( key . toJWK ( true ) , jwk )
2136 const { d, ...pub } = jwk
2237 t . deepEqual ( key . toJWK ( ) , pub )
2338 t . deepEqual ( key . toJWK ( false ) , pub )
39+ t . throws ( ( ) => {
40+ key . toPEM ( false , { cipher : 'aes-256-cbc' } )
41+ } , { instanceOf : TypeError , message : 'cipher and passphrase can only be applied when exporting private keys' } )
42+ t . throws ( ( ) => {
43+ key . toPEM ( false , { passphrase : 'top secret' } )
44+ } , { instanceOf : TypeError , message : 'cipher and passphrase can only be applied when exporting private keys' } )
2445} )
2546
2647test ( 'public RSA' , t => {
2748 const jwk = recipes . get ( '3.3' )
2849 const key = importKey ( jwk )
50+ t . true ( key . toPEM ( ) . includes ( 'BEGIN PUBLIC KEY' ) )
2951 t . deepEqual ( key . toJWK ( ) , jwk )
3052 t . deepEqual ( key . toJWK ( false ) , jwk )
3153 t . throws ( ( ) => {
3254 key . toJWK ( true )
3355 } , { instanceOf : TypeError , message : 'public key cannot be exported as private' } )
56+ t . throws ( ( ) => {
57+ key . toPEM ( true )
58+ } , { instanceOf : TypeError , message : 'public key cannot be exported as private' } )
3459} )
3560
3661test ( 'private RSA' , t => {
3762 const jwk = recipes . get ( '3.4' )
3863 const key = importKey ( jwk )
64+ t . true ( key . toPEM ( true , { type : 'pkcs1' } ) . includes ( 'BEGIN RSA PRIVATE KEY' ) )
65+ t . true ( key . toPEM ( true , { cipher : 'aes-256-cbc' , passphrase : 'top secret' , type : 'pkcs1' } ) . includes ( 'ENCRYPTED' ) )
66+ t . true ( key . toPEM ( true , { type : 'pkcs1' , cipher : 'aes-256-cbc' , passphrase : 'top secret' } ) . includes ( 'BEGIN RSA PRIVATE KEY' ) )
67+ t . true ( key . toPEM ( true , { cipher : 'aes-256-cbc' , passphrase : 'top secret' } ) . includes ( 'BEGIN ENCRYPTED PRIVATE KEY' ) )
68+ t . true ( key . toPEM ( true ) . includes ( 'BEGIN PRIVATE KEY' ) )
69+ t . true ( key . toPEM ( ) . includes ( 'BEGIN PUBLIC KEY' ) )
3970 t . deepEqual ( key . toJWK ( true ) , jwk )
4071 const { d, dp, dq, p, q, qi, ...pub } = jwk
4172 t . deepEqual ( key . toJWK ( ) , pub )
@@ -45,6 +76,9 @@ test('private RSA', t => {
4576test ( 'oct (1/2)' , t => {
4677 const jwk = recipes . get ( '3.5' )
4778 const key = importKey ( jwk )
79+ t . throws ( ( ) => {
80+ key . toPEM ( )
81+ } , { instanceOf : TypeError , message : 'symmetric keys cannot be exported as PEM' } )
4882 t . deepEqual ( key . toJWK ( true ) , jwk )
4983 const { k, ...pub } = jwk
5084 t . deepEqual ( key . toJWK ( ) , pub )
0 commit comments