Skip to content

Commit 6082cbd

Browse files
authored
Merge branch 'main' into dependabot/github_actions/codeql-action-f442528237
2 parents 74a2bf9 + 44e92f1 commit 6082cbd

29 files changed

Lines changed: 689 additions & 443 deletions

cmd/mksyscall/main.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os/exec"
1313
"path/filepath"
1414
"runtime"
15+
"strings"
1516
)
1617

1718
const mkwinsyscallVersion = "v0.37.0"
@@ -36,7 +37,10 @@ func main() {
3637
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", description)
3738
}
3839
flag.Parse()
39-
goTool := filepath.Join(runtime.GOROOT(), "bin", "go")
40+
goTool, err := exec.LookPath("go")
41+
if err != nil {
42+
log.Fatal(err)
43+
}
4044

4145
listCmd := exec.Command(goTool, "list", "-m")
4246
listCmd.Env = append(os.Environ(), "GO111MODULE=on")
@@ -47,8 +51,8 @@ func main() {
4751
log.Fatal("WARNING: Please switch from using:\n go run ./cmd/mksyscall\nto using:\n go run golang.org/x/sys/windows/mkwinsyscall\n")
4852
}
4953

50-
install(goTool)
51-
zsys := generateSyscalls()
54+
mkwinsyscall := install(goTool)
55+
zsys := generateSyscalls(mkwinsyscall)
5256

5357
if *output == "" {
5458
os.Stdout.Write(zsys)
@@ -62,7 +66,7 @@ func main() {
6266

6367
// install makes sure mkwinsyscall can be called by
6468
// running go install golang.org/x/sys/windows/mkwinsyscall.
65-
func install(goTool string) {
69+
func install(goTool string) string {
6670
// mkwinsyscall is hardcoded here instead of adding it to go.mod so
6771
// it doesn't appear in go.sum, which will reduce the likelihood
6872
// of having patch conflicts when vendoring go-crypto-winnative.
@@ -75,6 +79,29 @@ func install(goTool string) {
7579
if err != nil {
7680
log.Fatal(err)
7781
}
82+
83+
binDir := goEnv(goTool, "GOBIN")
84+
if binDir == "" {
85+
goPaths := filepath.SplitList(goEnv(goTool, "GOPATH"))
86+
if len(goPaths) == 0 {
87+
log.Fatal("GOPATH is empty")
88+
}
89+
binDir = filepath.Join(goPaths[0], "bin")
90+
}
91+
binary := "mkwinsyscall"
92+
if runtime.GOOS == "windows" {
93+
binary += ".exe"
94+
}
95+
return filepath.Join(binDir, binary)
96+
}
97+
98+
func goEnv(goTool, name string) string {
99+
cmd := exec.Command(goTool, "env", name)
100+
output, err := cmd.Output()
101+
if err != nil {
102+
log.Fatal(err)
103+
}
104+
return strings.TrimSpace(string(output))
78105
}
79106

80107
// generateSyscalls runs mkwinsyscall with GOROOT set to the current working directory.
@@ -84,15 +111,15 @@ func install(goTool string) {
84111
// to avoid DLL preloading attacks. As sysdll is a std internal package, this function
85112
// replaces the generated code's sysdll import with our own version located at
86113
// "./internal/sysdll".
87-
func generateSyscalls() []byte {
114+
func generateSyscalls(mkwinsyscall string) []byte {
88115
wd, err := os.Getwd()
89116
if err != nil {
90117
log.Fatal(err)
91118
}
92119
args := flag.Args()
93120
// We have intercepted the output argument, so we can be sure
94121
// that mkwinsyscall will emit the generated file to the standard output.
95-
cmd := exec.Command("mkwinsyscall", args...)
122+
cmd := exec.Command(mkwinsyscall, args...)
96123
var bout bytes.Buffer
97124
cmd.Stdout = &bout
98125
cmd.Stderr = os.Stderr
@@ -104,6 +131,7 @@ func generateSyscalls() []byte {
104131
zsys := bout.Bytes()
105132
zsys = bytes.ReplaceAll(zsys, []byte("\"internal/syscall/windows/sysdll\""), []byte("\"github.com/microsoft/go-crypto-winnative/internal/sysdll\""))
106133
zsys = bytes.ReplaceAll(zsys, []byte("windows.NTStatus"), []byte("NTStatus"))
134+
zsys = bytes.ReplaceAll(zsys, []byte(".dll.dll\""), []byte(".dll\""))
107135

108136
return zsys
109137
}

cmd/mkwinmd/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
// Command mkwinmd runs the pinned go-winmd generator with Win32 metadata from NuGet.
5+
package main
6+
7+
import (
8+
"log"
9+
"os"
10+
"os/exec"
11+
"path/filepath"
12+
)
13+
14+
const (
15+
goWinMDModule = "github.com/microsoft/go-winmd"
16+
goWinMDVersion = "v0.0.0-20260825140017-369639105e55"
17+
metadataPackage = "Microsoft.Windows.SDK.Win32Metadata"
18+
metadataVersion = "71.0.20-preview"
19+
metadataArchivePath = "Windows.Win32.winmd"
20+
)
21+
22+
func main() {
23+
goTool, err := exec.LookPath("go")
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
source := downloadMetadata(goTool)
28+
29+
args := []string{"run", goWinMDModule + "/cmd/gowinmd@" + goWinMDVersion, "-source", source}
30+
args = append(args, os.Args[1:]...)
31+
runGo(goTool, args...)
32+
}
33+
34+
func downloadMetadata(goTool string) string {
35+
cacheDir, err := os.UserCacheDir()
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
path := filepath.Join(cacheDir, "go-crypto-winnative", metadataPackage, metadataVersion, metadataArchivePath)
40+
if info, err := os.Stat(path); err == nil && info.Size() > 0 {
41+
return path
42+
}
43+
44+
runGo(goTool,
45+
"run", goWinMDModule+"/cmd/getwinmd@"+goWinMDVersion,
46+
"-version", metadataVersion,
47+
"-output", path,
48+
)
49+
return path
50+
}
51+
52+
func runGo(goTool string, args ...string) {
53+
cmd := exec.Command(goTool, args...)
54+
cmd.Env = append(os.Environ(), "GO111MODULE=on")
55+
cmd.Stdin = os.Stdin
56+
cmd.Stdout = os.Stdout
57+
cmd.Stderr = os.Stderr
58+
if err := cmd.Run(); err != nil {
59+
log.Fatal(err)
60+
}
61+
}

cng/chacha20poly1305.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func NewChaCha20Poly1305(key []byte) (cipher.AEAD, error) {
4545
}
4646

4747
func (c *chacha20poly1305) finalize() {
48-
if c.kh != 0 {
48+
if c.kh != nil {
4949
bcrypt.DestroyKey(c.kh)
5050
}
5151
}

cng/chacha20poly1305_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build windows
6+
// +build windows
7+
58
package cng_test
69

710
import (

cng/chacha20poly1305_vectors_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build windows
6+
// +build windows
7+
58
package cng_test
69

710
var chacha20Poly1305Tests = []struct {

cng/cipher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ func loadCipher(id, mode string) (cipherAlgorithm, error) {
3838
func newCipherHandle(id, mode string, key []byte) (bcrypt.KEY_HANDLE, error) {
3939
h, err := loadCipher(id, mode)
4040
if err != nil {
41-
return 0, err
41+
return nil, err
4242
}
4343
if !keyIsAllowed(h.allowedKeyLengths, uint32(len(key)*8)) {
44-
return 0, errors.New("crypto/cipher: invalid key size")
44+
return nil, errors.New("crypto/cipher: invalid key size")
4545
}
4646
var kh bcrypt.KEY_HANDLE
4747
err = bcrypt.GenerateSymmetricKey(h.handle, &kh, nil, key, 0)
4848
if err != nil {
49-
return 0, err
49+
return nil, err
5050
}
5151
return kh, nil
5252
}

cng/cng.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717
)
1818

1919
func FIPS() (bool, error) {
20-
var enabled bool
20+
var enabled uint8
2121
err := bcrypt.GetFipsAlgorithmMode(&enabled)
2222
if err != nil {
2323
return false, err
2424
}
25-
return enabled, nil
25+
return enabled != 0, nil
2626
}
2727

2828
// len32 clamps s length so it can fit into a Win32 LONG,
@@ -122,19 +122,19 @@ func getKeyLengths(h bcrypt.HANDLE) (lengths bcrypt.KEY_LENGTHS_STRUCT, err erro
122122
if err != nil {
123123
return
124124
}
125-
if lengths.MinLength > lengths.MaxLength || (lengths.Increment == 0 && lengths.MinLength != lengths.MaxLength) {
125+
if lengths.DwMinLength > lengths.DwMaxLength || (lengths.DwIncrement == 0 && lengths.DwMinLength != lengths.DwMaxLength) {
126126
err = errors.New("invalid BCRYPT_KEY_LENGTHS_STRUCT")
127127
return
128128
}
129129
return lengths, nil
130130
}
131131

132132
func keyIsAllowed(lengths bcrypt.KEY_LENGTHS_STRUCT, bits uint32) bool {
133-
if bits < lengths.MinLength || bits > lengths.MaxLength {
133+
if bits < lengths.DwMinLength || bits > lengths.DwMaxLength {
134134
return false
135135
}
136-
if lengths.Increment == 0 {
137-
return bits == lengths.MinLength
136+
if lengths.DwIncrement == 0 {
137+
return bits == lengths.DwMinLength
138138
}
139-
return (bits-lengths.MinLength)%lengths.Increment == 0
139+
return (bits-lengths.DwMinLength)%lengths.DwIncrement == 0
140140
}

0 commit comments

Comments
 (0)