go.crypto/ssh: add client support for OpenSSH certificates

Refactor key parsing, marshaling, and serialization to be a bit more flexible

R=agl, dave, djm
CC=golang-dev
https://golang.org/cl/5650067
This commit is contained in:
Jonathan Pittman
2012-02-24 12:52:06 -05:00
committed by Adam Langley
parent d33bbf2cb3
commit 9b05c27191
5 changed files with 475 additions and 63 deletions
+6 -41
View File
@@ -67,49 +67,10 @@ func (s *ServerConfig) SetRSAPrivateKey(pemBytes []byte) error {
return err
}
s.rsaSerialized = marshalRSA(s.rsa)
s.rsaSerialized = marshalPrivRSA(s.rsa)
return nil
}
// marshalRSA serializes an RSA private key according to RFC 4256, section 6.6.
func marshalRSA(priv *rsa.PrivateKey) []byte {
e := new(big.Int).SetInt64(int64(priv.E))
length := stringLength([]byte(hostAlgoRSA))
length += intLength(e)
length += intLength(priv.N)
ret := make([]byte, length)
r := marshalString(ret, []byte(hostAlgoRSA))
r = marshalInt(r, e)
r = marshalInt(r, priv.N)
return ret
}
// parseRSA parses an RSA key according to RFC 4256, section 6.6.
func parseRSA(in []byte) (pubKey *rsa.PublicKey, ok bool) {
algo, in, ok := parseString(in)
if !ok || string(algo) != hostAlgoRSA {
return nil, false
}
bigE, in, ok := parseInt(in)
if !ok || bigE.BitLen() > 24 {
return nil, false
}
e := bigE.Int64()
if e < 3 || e&1 == 0 {
return nil, false
}
N, in, ok := parseInt(in)
if !ok || len(in) > 0 {
return nil, false
}
return &rsa.PublicKey{
N: N,
E: int(e),
}, true
}
func parseRSASig(in []byte) (sig []byte, ok bool) {
algo, in, ok := parseString(in)
if !ok || string(algo) != hostAlgoRSA {
@@ -485,7 +446,11 @@ userAuthLoop:
h := hashFunc.New()
h.Write(signedData)
digest := h.Sum(nil)
rsaKey, ok := parseRSA(pubKey)
key, _, ok := parsePubKey(pubKey)
if !ok {
return ParseError{msgUserAuthRequest}
}
rsaKey, ok := key.(*rsa.PublicKey)
if !ok {
return ParseError{msgUserAuthRequest}
}