@@ -2,6 +2,7 @@ package storage
2
2
3
3
import (
4
4
"context"
5
+ "crypto"
5
6
"encoding/json"
6
7
"fmt"
7
8
"io"
@@ -22,19 +23,23 @@ const (
22
23
)
23
24
24
25
type Signature struct {
25
- // SignDesignExtensions is a flag that determines if the signature should
26
- // include the extension payloads .
27
- signExtensions bool
26
+ // Signer if provided, will be used to sign extensions. If not provided,
27
+ // no extensions will be signed .
28
+ Signer crypto. Signer
28
29
Storage
29
30
}
30
31
31
- func NewSignatureStorage (signExtensions bool , s Storage ) * Signature {
32
+ func NewSignatureStorage (signer crypto. Signer , s Storage ) * Signature {
32
33
return & Signature {
33
- signExtensions : signExtensions ,
34
- Storage : s ,
34
+ Signer : signer ,
35
+ Storage : s ,
35
36
}
36
37
}
37
38
39
+ func (s * Signature ) SigningEnabled () bool {
40
+ return s .Signer != nil
41
+ }
42
+
38
43
// AddExtension includes the signature manifest of the vsix. Signing happens on
39
44
// demand, so leave the manifest unsigned. This is safe to do even if
40
45
// 'signExtensions' is disabled, as these files lay dormant until signed.
@@ -61,7 +66,7 @@ func (s *Signature) Manifest(ctx context.Context, publisher, name string, versio
61
66
return nil , err
62
67
}
63
68
64
- if s .signExtensions {
69
+ if s .SigningEnabled () {
65
70
manifest .Assets .Asset = append (manifest .Assets .Asset , VSIXAsset {
66
71
Type : VSIXSignatureType ,
67
72
Path : sigzipFilename ,
@@ -72,11 +77,11 @@ func (s *Signature) Manifest(ctx context.Context, publisher, name string, versio
72
77
}
73
78
74
79
func (s * Signature ) Open (ctx context.Context , fp string ) (fs.File , error ) {
75
- if s .signExtensions && filepath .Base (fp ) == "p7s.sig" {
80
+ if s .SigningEnabled () && filepath .Base (fp ) == "p7s.sig" {
76
81
// This file must exist, and it is always empty
77
82
return mem .NewFileHandle (mem .CreateFile ("p7s.sig" )), nil
78
83
}
79
- if s .signExtensions && filepath .Base (fp ) == sigzipFilename {
84
+ if s .SigningEnabled () && filepath .Base (fp ) == sigzipFilename {
80
85
// hijack this request, sign the sig manifest
81
86
manifest , err := s .Storage .Open (ctx , filepath .Join (filepath .Dir (fp ), sigManifestName ))
82
87
if err != nil {
@@ -85,13 +90,12 @@ func (s *Signature) Open(ctx context.Context, fp string) (fs.File, error) {
85
90
}
86
91
defer manifest .Close ()
87
92
88
- key , _ := extensionsign .GenerateKey ()
89
93
manifestData , err := io .ReadAll (manifest )
90
94
if err != nil {
91
95
return nil , xerrors .Errorf ("read signature manifest: %w" , err )
92
96
}
93
97
94
- signed , err := extensionsign .SignAndZipManifest (key , manifestData )
98
+ signed , err := extensionsign .SignAndZipManifest (s . Signer , manifestData )
95
99
if err != nil {
96
100
return nil , xerrors .Errorf ("sign and zip manifest: %w" , err )
97
101
}
0 commit comments