1
1
import { expect } from 'chai' ;
2
2
import { extractIssuerData , extractPrivateKeyAndCertificateFromPkcs12 , getHash , sign } from '../../src/utils/cryptography' ;
3
- import { verifySignature } from '../test-utils/cryptography' ;
3
+ import { createBancoCentralCertificateKeyAndP12 , verifySignature } from '../test-utils/cryptography' ;
4
4
import fs from 'fs' ;
5
5
import path from 'path' ;
6
6
import * as forge from 'node-forge' ;
7
- const signatureP12 = fs . readFileSync ( path . resolve ( 'test/test-data/pkcs12/signature.p12' ) ) ;
8
7
9
8
describe ( 'Given the sign function' , ( ) => {
10
9
it ( 'should return the signature for the input data' , ( ) => {
11
10
const data = 'something' ;
12
- const { privateKey, certificate } = extractPrivateKeyAndCertificateFromPkcs12 ( signatureP12 ) ;
11
+ const p12 = fs . readFileSync ( path . resolve ( 'test/test-data/pkcs12/signature.p12' ) ) ;
12
+ const { privateKey, certificate } = extractPrivateKeyAndCertificateFromPkcs12 ( p12 ) ;
13
13
14
14
const resultSignature = sign ( data , privateKey ) ;
15
15
const verifiedSuccessfully = verifySignature ( data , certificate . publicKey as forge . pki . rsa . PublicKey , resultSignature ) ;
@@ -29,15 +29,31 @@ describe('Given the extractPrivateKeyAndCertificateFromPkcs12 function', () => {
29
29
it ( 'should return an object with the private key and certificate contained in the pkcs12 file' , ( ) => {
30
30
const privateKeyPem = fs . readFileSync ( path . resolve ( 'test/test-data/pkcs12/privateKey.pem' ) ) . toString ( 'utf-8' ) ;
31
31
const certificatePem = fs . readFileSync ( path . resolve ( 'test/test-data/pkcs12/certificate.pem' ) ) . toString ( 'utf-8' ) ;
32
+ const p12 = fs . readFileSync ( path . resolve ( 'test/test-data/pkcs12/signature.p12' ) ) ;
32
33
const password = '' ;
33
34
34
- const result = extractPrivateKeyAndCertificateFromPkcs12 ( signatureP12 , password ) ;
35
+ const result = extractPrivateKeyAndCertificateFromPkcs12 ( p12 , password ) ;
35
36
36
37
// Here we convert from fromPem and toPem to overcome format inconsistencies due to new line encoding and pkcs8 shrouding of private key.
37
38
// This way the comparison is delegated to node-forge functions only becoming abstracted and consistent.
38
39
expect ( forge . pki . privateKeyToPem ( result . privateKey ) ) . to . equal ( forge . pki . privateKeyToPem ( forge . pki . privateKeyFromPem ( privateKeyPem ) ) ) ;
39
40
expect ( forge . pki . certificateToPem ( result . certificate ) ) . to . equal ( forge . pki . certificateToPem ( forge . pki . certificateFromPem ( certificatePem ) ) ) ;
40
41
} ) ;
42
+
43
+ /**
44
+ * Unskip when createBancoCentralCertificateKeyAndP12 is fixed
45
+ */
46
+ it . skip ( "should return the correct private key for a 'Banco Central del Ecuador' .p12" , ( ) => {
47
+ const { keyPem, certPem, p12Buffer } = createBancoCentralCertificateKeyAndP12 ( ) ;
48
+ const password = '' ;
49
+
50
+ const result = extractPrivateKeyAndCertificateFromPkcs12 ( p12Buffer , password ) ;
51
+
52
+ // Here we convert from fromPem and toPem to overcome format inconsistencies due to new line encoding and pkcs8 shrouding of private key.
53
+ // This way the comparison is delegated to node-forge functions only becoming abstracted and consistent.
54
+ expect ( forge . pki . privateKeyToPem ( result . privateKey ) ) . to . equal ( forge . pki . privateKeyToPem ( forge . pki . privateKeyFromPem ( keyPem ) ) ) ;
55
+ expect ( forge . pki . certificateToPem ( result . certificate ) ) . to . equal ( forge . pki . certificateToPem ( forge . pki . certificateFromPem ( certPem ) ) ) ;
56
+ } ) ;
41
57
} ) ;
42
58
43
59
describe ( 'Give the extractIssuerData function' , ( ) => {
0 commit comments