Repository navigation
Expand file tree
/
Copy pathpqc_example_test.go
More file actions
67 lines (56 loc) · 1.84 KB
/
Copy pathpqc_example_test.go
File metadata and controls
67 lines (56 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package pdfsign_test
import (
"bytes"
"crypto"
"crypto/x509"
"fmt"
"log"
"github.com/digitorus/pdfsign"
"github.com/digitorus/pdfsign/internal/testpki"
)
// ExampleDocument_Sign_postQuantumOnly signs and validates a PAdES B-T PDF
// without introducing a classical public-key signature in the signer, CA,
// revocation, or timestamp paths.
func ExampleDocument_Sign_postQuantumOnly() {
doc, err := pdfsign.OpenFile("testfiles/testfile12.pdf")
if err != nil {
log.Fatal(err)
}
pki := testpki.NewTestPKIWithConfig(nil, testpki.TestPKIConfig{
Profile: testpki.MLDSA_65,
IntermediateCAs: 1,
})
pki.StartCRLServer()
defer pki.Close()
privateKey, certificate := pki.IssueLeaf("PQC PDF Signer")
tsaURL := pki.StartMockTSA()
doc.Sign(privateKey, certificate, pki.Chain()...).
Format(pdfsign.PAdES_B_T).
Digest(crypto.SHA512).
Timestamp(tsaURL).
Reason("Post-quantum approval")
var output bytes.Buffer
if _, err := doc.Write(&output); err != nil {
log.Fatal(err)
}
signed, err := pdfsign.Open(bytes.NewReader(output.Bytes()), int64(output.Len()))
if err != nil {
log.Fatal(err)
}
result := signed.Verify().
TrustedRoots(pki.RootPool()).
AllowedAlgorithms(x509.MLDSA).
ValidateFullChain(true).
ValidateTimestampCertificates(true).
ExternalChecks(true)
signature := result.Signatures()[0]
pqcSignerAndTSA := signature.Certificate.PublicKeyAlgorithm == x509.MLDSA &&
signature.Timestamp != nil && signature.Timestamp.Certificate.PublicKeyAlgorithm == x509.MLDSA
fmt.Printf("ML-DSA signer and TSA: %t\n", pqcSignerAndTSA)
fmt.Printf("OCSP and CRL checked: %t\n", pki.OCSPRequests > 0 && pki.Requests > 0)
fmt.Printf("Signature and timestamp valid: %t\n", result.Valid() && signature.TimestampValid)
// Output:
// ML-DSA signer and TSA: true
// OCSP and CRL checked: true
// Signature and timestamp valid: true
}