-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create initial version for signing certs with KMS key
- Loading branch information
Showing
5 changed files
with
192 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
.vscode | ||
/auth-wrapper | ||
vendor | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
|
||
"github.com/connectedcars/auth-wrapper/sshagent" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
// https://medium.com/tarkalabs/ssh-recipes-in-go-an-interlude-6fa88a03d458 | ||
// https://gitlab.openebs.ci/openebs/maya/blob/b5f23e9b2e0c3e9d9503a5c1ae9c15cf8e439db5/vendor/golang.org/x/crypto/ssh/agent/client_test.go | ||
// https://github.com/cloudtools/ssh-cert-authority | ||
// https://github.com/signmykeyio/signmykey | ||
|
||
func signCert(key string) (int, error) { | ||
// Parse public key string | ||
userPubkey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
cert := &ssh.Certificate{ | ||
Key: userPubkey, | ||
KeyId: "test", | ||
CertType: ssh.UserCert, | ||
ValidPrincipals: []string{"tlb"}, | ||
ValidAfter: 0, | ||
ValidBefore: ssh.CertTimeInfinity, // uint64(time.Now().Add(time.Minute * 60).Unix()), | ||
Permissions: ssh.Permissions{ | ||
CriticalOptions: map[string]string{}, | ||
Extensions: map[string]string{}, | ||
}, | ||
} | ||
|
||
/*sshKeyPath := "/Users/f736trbe/.ssh/id_rsa" | ||
privateKeyBytes, err := ioutil.ReadFile(sshKeyPath) | ||
if err != nil { | ||
return 1, fmt.Errorf("Failed to read SSHPrivateKey from %s: %v", sshKeyPath, err) | ||
} | ||
caPrivateKey, err := sshagent.ParsePrivateSSHKey(privateKeyBytes, "") | ||
if err != nil { | ||
return 1, fmt.Errorf("Failed to read SSHPrivateKey from %s: %v", sshKeyPath, err) | ||
} | ||
sshSigner, err := ssh.NewSignerFromKey(caPrivateKey) | ||
if err != nil { | ||
return 1, fmt.Errorf("Failed to read SSHPrivateKey from %s: %v", sshKeyPath, err) | ||
}*/ | ||
|
||
cryptoSigner, err := sshagent.NewKMSSigner("projects/connectedcars-staging/locations/global/keyRings/cloudbuilder/cryptoKeys/ssh-key/cryptoKeyVersions/3", true) | ||
if err != nil { | ||
return 1, fmt.Errorf("Failed to read NewKMSSigner from: %v", err) | ||
} | ||
sshSigner, err := sshagent.NewSSHSignerFromKMSSigner(cryptoSigner) | ||
if err != nil { | ||
return 1, fmt.Errorf("Failed NewSignerFromSigner from: %v", err) | ||
} | ||
|
||
err = cert.SignCert(rand.Reader, sshSigner) | ||
if err != nil { | ||
return 1, fmt.Errorf("Failed SignCert from %v", err) | ||
} | ||
ioutil.WriteFile("/Users/f736trbe/git/connectedcars/auth-wrapper/cert.pub", ssh.MarshalAuthorizedKey(cert), 0644) | ||
return 1, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package sshagent | ||
|
||
import ( | ||
"crypto" | ||
"encoding/asn1" | ||
"io" | ||
"math/big" | ||
|
||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
type wrappedSigner struct { | ||
signer KMSSigner | ||
pubKey ssh.PublicKey | ||
} | ||
|
||
// NewSSHSignerFromKMSSigner takes a KMSSigner implementation and | ||
// returns a corresponding ssh.Signer interface. | ||
func NewSSHSignerFromKMSSigner(signer KMSSigner) (ssh.Signer, error) { | ||
pubKey, err := ssh.NewPublicKey(signer.Public()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &wrappedSigner{signer, pubKey}, nil | ||
} | ||
|
||
func (s *wrappedSigner) PublicKey() ssh.PublicKey { | ||
return s.pubKey | ||
} | ||
|
||
func (s *wrappedSigner) Sign(rand io.Reader, data []byte) (*ssh.Signature, error) { | ||
return s.SignWithAlgorithm(rand, data) | ||
} | ||
|
||
func (s *wrappedSigner) SignWithAlgorithm(rand io.Reader, data []byte) (*ssh.Signature, error) { | ||
hashFunc := s.signer.Digest() | ||
|
||
var digest []byte | ||
if hashFunc != 0 { | ||
h := hashFunc.New() | ||
h.Write(data) | ||
digest = h.Sum(nil) | ||
} else { | ||
digest = data | ||
} | ||
|
||
signature, err := s.signer.Sign(rand, digest, hashFunc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var algorithm string | ||
if s.PublicKey().Type() == "ssh-rsa" { | ||
switch hashFunc { | ||
case crypto.SHA1: | ||
algorithm = ssh.SigAlgoRSA | ||
case crypto.SHA256: | ||
algorithm = ssh.SigAlgoRSASHA2256 | ||
case crypto.SHA512: | ||
algorithm = ssh.SigAlgoRSASHA2512 | ||
} | ||
} else { | ||
algorithm = s.pubKey.Type() | ||
} | ||
|
||
// crypto.Signer.Sign is expected to return an ASN.1-encoded signature | ||
// for ECDSA and DSA, but that's not the encoding expected by SSH, so | ||
// re-encode. | ||
switch s.pubKey.Type() { | ||
case "ecdsa-sha2-nistp256", "ecdsa-sha2-nistp384", "ecdsa-sha2-nistp521", "ssh-dss": | ||
type asn1Signature struct { | ||
R, S *big.Int | ||
} | ||
asn1Sig := new(asn1Signature) | ||
_, err := asn1.Unmarshal(signature, asn1Sig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch s.pubKey.Type() { | ||
case "ecdsa-sha2-nistp256", "ecdsa-sha2-nistp384", "ecdsa-sha2-nistp521": | ||
signature = ssh.Marshal(asn1Sig) | ||
case "ssh-dss": | ||
signature = make([]byte, 40) | ||
r := asn1Sig.R.Bytes() | ||
s := asn1Sig.S.Bytes() | ||
copy(signature[20-len(r):20], r) | ||
copy(signature[40-len(s):40], s) | ||
} | ||
} | ||
|
||
return &ssh.Signature{ | ||
Format: algorithm, | ||
Blob: signature, | ||
}, nil | ||
} |