-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicitly use signature algorithm for SSH certs (#125)
* add support for configuring sign algo for ssh cert * increase code cov * address comments * pkcs11/algosigner_test.go * verify signature algo for cert
- Loading branch information
Showing
5 changed files
with
194 additions
and
17 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
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,85 @@ | ||
// Copyright 2021 Yahoo. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package pkcs11 | ||
|
||
import ( | ||
"crypto" | ||
"errors" | ||
"io" | ||
|
||
"golang.org/x/crypto/ssh" | ||
|
||
"github.com/theparanoids/crypki" | ||
) | ||
|
||
type sshAlgorithmSigner struct { | ||
algorithm string | ||
signer ssh.AlgorithmSigner | ||
} | ||
|
||
func (s *sshAlgorithmSigner) PublicKey() ssh.PublicKey { | ||
return s.signer.PublicKey() | ||
} | ||
|
||
func (s *sshAlgorithmSigner) Sign(rand io.Reader, data []byte) (*ssh.Signature, error) { | ||
return s.signer.SignWithAlgorithm(rand, data, s.algorithm) | ||
} | ||
|
||
func getSignatureAlgorithm(publicAlgo crypki.PublicKeyAlgorithm, signAlgo crypki.SignatureAlgorithm) (algorithm string, err error) { | ||
switch publicAlgo { | ||
case crypki.RSA: | ||
{ | ||
switch signAlgo { | ||
case crypki.ECDSAWithSHA256, crypki.ECDSAWithSHA384: | ||
err = errors.New("public key algo & signature algo mismatch, unable to get AlgorithmSigner") | ||
case crypki.SHAWithRSA: | ||
algorithm = ssh.SigAlgoRSA | ||
case crypki.SHA512WithRSA: | ||
algorithm = ssh.SigAlgoRSASHA2512 | ||
case crypki.SHA256WithRSA: | ||
algorithm = ssh.SigAlgoRSASHA2256 | ||
default: | ||
algorithm = ssh.SigAlgoRSASHA2256 | ||
} | ||
} | ||
case crypki.ECDSA: | ||
// For ECDSA public algorithm, signature algo does not exist. We pass in | ||
// empty algorithm & the crypto library will ensure the right algorithm is chosen | ||
// for signing the cert. | ||
return | ||
default: | ||
err = errors.New("public key algorithm not supported") | ||
} | ||
return | ||
} | ||
|
||
func newAlgorithmSignerFromSigner(signer crypto.Signer, publicAlgo crypki.PublicKeyAlgorithm, signAlgo crypki.SignatureAlgorithm) (ssh.Signer, error) { | ||
sshSigner, err := ssh.NewSignerFromSigner(signer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
algorithmSigner, ok := sshSigner.(ssh.AlgorithmSigner) | ||
if !ok { | ||
return nil, errors.New("unable to cast to ssh.AlgorithmSigner") | ||
} | ||
algorithm, err := getSignatureAlgorithm(publicAlgo, signAlgo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s := sshAlgorithmSigner{ | ||
signer: algorithmSigner, | ||
algorithm: algorithm, | ||
} | ||
return &s, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2021 Yahoo. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package pkcs11 | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/crypto/ssh" | ||
|
||
"github.com/theparanoids/crypki" | ||
) | ||
|
||
func TestGetSignatureAlgorithm(t *testing.T) { | ||
t.Parallel() | ||
tests := map[string]struct { | ||
pubAlgo crypki.PublicKeyAlgorithm | ||
signAlgo crypki.SignatureAlgorithm | ||
want string | ||
wantError bool | ||
}{ | ||
"rsa pub rsa 256 signing": { | ||
pubAlgo: crypki.RSA, | ||
signAlgo: crypki.SHA256WithRSA, | ||
want: ssh.SigAlgoRSASHA2256, | ||
wantError: false, | ||
}, | ||
"rsa pub rsa 512 signing": { | ||
pubAlgo: crypki.RSA, | ||
signAlgo: crypki.SHA512WithRSA, | ||
want: ssh.SigAlgoRSASHA2512, | ||
wantError: false, | ||
}, | ||
"rsa pub sha1 signing": { | ||
pubAlgo: crypki.RSA, | ||
signAlgo: crypki.SHAWithRSA, | ||
want: ssh.SigAlgoRSA, | ||
wantError: false, | ||
}, | ||
"rsa pub ec signing": { | ||
pubAlgo: crypki.RSA, | ||
signAlgo: crypki.ECDSAWithSHA384, | ||
want: "", | ||
wantError: true, | ||
}, | ||
"rsa pub no signing algo": { | ||
pubAlgo: crypki.RSA, | ||
signAlgo: crypki.UnknownSignatureAlgorithm, | ||
want: ssh.SigAlgoRSASHA2256, | ||
wantError: false, | ||
}, | ||
"ec pub ec sign": { | ||
pubAlgo: crypki.ECDSA, | ||
signAlgo: crypki.ECDSAWithSHA384, | ||
want: "", | ||
wantError: false, | ||
}, | ||
"default pub key algo": { | ||
pubAlgo: crypki.UnknownPublicKeyAlgorithm, | ||
signAlgo: crypki.UnknownSignatureAlgorithm, | ||
want: "", | ||
wantError: true, | ||
}, | ||
} | ||
for name, tt := range tests { | ||
tt := tt | ||
t.Run(name, func(t *testing.T) { | ||
got, err := getSignatureAlgorithm(tt.pubAlgo, tt.signAlgo) | ||
if (err != nil) != tt.wantError { | ||
t.Errorf("%s: got %s want %s", name, got, tt.want) | ||
} | ||
if got != tt.want { | ||
t.Errorf("%s: got %s want %s", name, got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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