| 1 |
// Copyright 2022-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package certstore |
| 15 |
|
| 16 |
import ( |
| 17 |
"crypto" |
| 18 |
"crypto/x509" |
| 19 |
"io" |
| 20 |
"runtime" |
| 21 |
"strings" |
| 22 |
) |
| 23 |
|
| 24 |
type StoreType int |
| 25 |
|
| 26 |
const MATCHBYEMPTY = 0 |
| 27 |
const STOREEMPTY = 0 |
| 28 |
|
| 29 |
const ( |
| 30 |
windowsCurrentUser StoreType = iota + 1 |
| 31 |
windowsLocalMachine |
| 32 |
) |
| 33 |
|
| 34 |
var StoreMap = map[string]StoreType{ |
| 35 |
"windowscurrentuser": windowsCurrentUser, |
| 36 |
"windowslocalmachine": windowsLocalMachine, |
| 37 |
} |
| 38 |
|
| 39 |
var StoreOSMap = map[StoreType]string{ |
| 40 |
windowsCurrentUser: "windows", |
| 41 |
windowsLocalMachine: "windows", |
| 42 |
} |
| 43 |
|
| 44 |
type MatchByType int |
| 45 |
|
| 46 |
const ( |
| 47 |
matchByIssuer MatchByType = iota + 1 |
| 48 |
matchBySubject |
| 49 |
matchByThumbprint |
| 50 |
) |
| 51 |
|
| 52 |
var MatchByMap = map[string]MatchByType{ |
| 53 |
"issuer": matchByIssuer, |
| 54 |
"subject": matchBySubject, |
| 55 |
"thumbprint": matchByThumbprint, |
| 56 |
} |
| 57 |
|
| 58 |
var Usage = ` |
| 59 |
In place of cert_file and key_file you may use the windows certificate store: |
| 60 |
|
| 61 |
tls { |
| 62 |
cert_store: "WindowsCurrentUser" |
| 63 |
cert_match_by: "Subject" |
| 64 |
cert_match: "MyServer123" |
| 65 |
} |
| 66 |
` |
| 67 |
|
| 68 |
func ParseCertStore(certStore string) (StoreType, error) { |
| 69 |
certStoreType, exists := StoreMap[strings.ToLower(certStore)] |
| 70 |
if !exists { |
| 71 |
return 0, ErrBadCertStore |
| 72 |
} |
| 73 |
validOS, exists := StoreOSMap[certStoreType] |
| 74 |
if !exists || validOS != runtime.GOOS { |
| 75 |
return 0, ErrOSNotCompatCertStore |
| 76 |
} |
| 77 |
return certStoreType, nil |
| 78 |
} |
| 79 |
|
| 80 |
func ParseCertMatchBy(certMatchBy string) (MatchByType, error) { |
| 81 |
certMatchByType, exists := MatchByMap[strings.ToLower(certMatchBy)] |
| 82 |
if !exists { |
| 83 |
return 0, ErrBadMatchByType |
| 84 |
} |
| 85 |
return certMatchByType, nil |
| 86 |
} |
| 87 |
|
| 88 |
func GetLeafIssuer(leaf *x509.Certificate, vOpts x509.VerifyOptions) (issuer *x509.Certificate) { |
| 89 |
chains, err := leaf.Verify(vOpts) |
| 90 |
if err != nil || len(chains) == 0 { |
| 91 |
issuer = nil |
| 92 |
} else { |
| 93 |
issuer = chains[0][1] |
| 94 |
} |
| 95 |
return |
| 96 |
} |
| 97 |
|
| 98 |
// credential provides access to a public key and is a crypto.Signer. |
| 99 |
type credential interface { |
| 100 |
// Public returns the public key corresponding to the leaf certificate. |
| 101 |
Public() crypto.PublicKey |
| 102 |
// Sign signs digest with the private key. |
| 103 |
Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) |
| 104 |
} |