Bläddra i källkod

go.crypto/ssh: Miscellaneous changes up for discussion.
Export key and certificate algorithm names.
Switch from string literals over to using the constants for any key/cert algorithm references.
Make URL references visible in the godoc web display.
Standardize url reference names with surrounding [].

R=dave, agl, jonathan.mark.pittman
CC=golang-dev
https://golang.org/cl/6944047

Jonathan Pittman 13 år sedan
förälder
incheckning
54c65aebf4
6 ändrade filer med 66 tillägg och 71 borttagningar
  1. 10 13
      ssh/agent.go
  2. 9 15
      ssh/certs.go
  3. 15 15
      ssh/common.go
  4. 5 0
      ssh/doc.go
  5. 26 26
      ssh/keys.go
  6. 1 2
      ssh/messages.go

+ 10 - 13
ssh/agent.go

@@ -4,9 +4,6 @@
 
 
 package ssh
 package ssh
 
 
-// References
-//   PROTOCOL.agent: http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.agent
-
 import (
 import (
 	"encoding/base64"
 	"encoding/base64"
 	"errors"
 	"errors"
@@ -14,7 +11,7 @@ import (
 	"sync"
 	"sync"
 )
 )
 
 
-// See PROTOCOL.agent, section 3.
+// See [PROTOCOL.agent], section 3.
 const (
 const (
 	// 3.2 Requests from client to agent for protocol 2 key operations
 	// 3.2 Requests from client to agent for protocol 2 key operations
 	agentRequestIdentities   = 11
 	agentRequestIdentities   = 11
@@ -50,34 +47,34 @@ const maxAgentResponseBytes = 16 << 20
 
 
 // Agent messages:
 // Agent messages:
 // These structures mirror the wire format of the corresponding ssh agent
 // These structures mirror the wire format of the corresponding ssh agent
-// messages found in PROTOCOL.agent.
+// messages found in [PROTOCOL.agent].
 
 
 type failureAgentMsg struct{}
 type failureAgentMsg struct{}
 
 
 type successAgentMsg struct{}
 type successAgentMsg struct{}
 
 
-// See PROTOCOL.agent, section 2.5.2.
+// See [PROTOCOL.agent], section 2.5.2.
 type requestIdentitiesAgentMsg struct{}
 type requestIdentitiesAgentMsg struct{}
 
 
-// See PROTOCOL.agent, section 2.5.2.
+// See [PROTOCOL.agent], section 2.5.2.
 type identitiesAnswerAgentMsg struct {
 type identitiesAnswerAgentMsg struct {
 	NumKeys uint32
 	NumKeys uint32
 	Keys    []byte `ssh:"rest"`
 	Keys    []byte `ssh:"rest"`
 }
 }
 
 
-// See PROTOCOL.agent, section 2.6.2.
+// See [PROTOCOL.agent], section 2.6.2.
 type signRequestAgentMsg struct {
 type signRequestAgentMsg struct {
 	KeyBlob []byte
 	KeyBlob []byte
 	Data    []byte
 	Data    []byte
 	Flags   uint32
 	Flags   uint32
 }
 }
 
 
-// See PROTOCOL.agent, section 2.6.2.
+// See [PROTOCOL.agent], section 2.6.2.
 type signResponseAgentMsg struct {
 type signResponseAgentMsg struct {
 	SigBlob []byte
 	SigBlob []byte
 }
 }
 
 
-// AgentKey represents a protocol 2 key as defined in PROTOCOL.agent,
+// AgentKey represents a protocol 2 key as defined in [PROTOCOL.agent],
 // section 2.5.2.
 // section 2.5.2.
 type AgentKey struct {
 type AgentKey struct {
 	blob    []byte
 	blob    []byte
@@ -127,7 +124,7 @@ func parseAgentKey(in []byte) (out *AgentKey, rest []byte, ok bool) {
 }
 }
 
 
 // AgentClient provides a means to communicate with an ssh agent process based
 // AgentClient provides a means to communicate with an ssh agent process based
-// on the protocol described in PROTOCOL.agent?rev=1.6.
+// on the protocol described in [PROTOCOL.agent]?rev=1.6.
 type AgentClient struct {
 type AgentClient struct {
 	// conn is typically represented by using a *net.UnixConn
 	// conn is typically represented by using a *net.UnixConn
 	conn io.ReadWriter
 	conn io.ReadWriter
@@ -175,7 +172,7 @@ func (ac *AgentClient) sendAndReceive(req []byte) (reply interface{}, replyType
 }
 }
 
 
 // RequestIdentities queries the agent for protocol 2 keys as defined in
 // RequestIdentities queries the agent for protocol 2 keys as defined in
-// PROTOCOL.agent section 2.5.2.
+// [PROTOCOL.agent] section 2.5.2.
 func (ac *AgentClient) RequestIdentities() ([]*AgentKey, error) {
 func (ac *AgentClient) RequestIdentities() ([]*AgentKey, error) {
 	req := marshal(agentRequestIdentities, requestIdentitiesAgentMsg{})
 	req := marshal(agentRequestIdentities, requestIdentitiesAgentMsg{})
 
 
@@ -207,7 +204,7 @@ func (ac *AgentClient) RequestIdentities() ([]*AgentKey, error) {
 }
 }
 
 
 // SignRequest requests the signing of data by the agent using a protocol 2 key
 // SignRequest requests the signing of data by the agent using a protocol 2 key
-// as defined in PROTOCOL.agent section 2.6.2.  Supported key types include
+// as defined in [PROTOCOL.agent] section 2.6.2.  Supported key types include
 // *rsa.PublicKey, *dsa.PublicKey, *OpenSSHCertV01.
 // *rsa.PublicKey, *dsa.PublicKey, *OpenSSHCertV01.
 func (ac *AgentClient) SignRequest(key interface{}, data []byte) ([]byte, error) {
 func (ac *AgentClient) SignRequest(key interface{}, data []byte) ([]byte, error) {
 	req := marshal(agentSignRequest, signRequestAgentMsg{
 	req := marshal(agentSignRequest, signRequestAgentMsg{

+ 9 - 15
ssh/certs.go

@@ -4,9 +4,6 @@
 
 
 package ssh
 package ssh
 
 
-// References
-//   [PROTOCOL.certkeys]: http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.certkeys
-
 import (
 import (
 	"crypto/dsa"
 	"crypto/dsa"
 	"crypto/ecdsa"
 	"crypto/ecdsa"
@@ -16,11 +13,11 @@ import (
 
 
 // String constants in [PROTOCOL.certkeys] for certificate algorithm names.
 // String constants in [PROTOCOL.certkeys] for certificate algorithm names.
 const (
 const (
-	certAlgoRSAv01      = "ssh-rsa-cert-v01@openssh.com"
-	certAlgoDSAv01      = "ssh-dss-cert-v01@openssh.com"
-	certAlgoECDSA256v01 = "ecdsa-sha2-nistp256-cert-v01@openssh.com"
-	certAlgoECDSA384v01 = "ecdsa-sha2-nistp384-cert-v01@openssh.com"
-	certAlgoECDSA521v01 = "ecdsa-sha2-nistp521-cert-v01@openssh.com"
+	CertAlgoRSAv01      = "ssh-rsa-cert-v01@openssh.com"
+	CertAlgoDSAv01      = "ssh-dss-cert-v01@openssh.com"
+	CertAlgoECDSA256v01 = "ecdsa-sha2-nistp256-cert-v01@openssh.com"
+	CertAlgoECDSA384v01 = "ecdsa-sha2-nistp384-cert-v01@openssh.com"
+	CertAlgoECDSA521v01 = "ecdsa-sha2-nistp521-cert-v01@openssh.com"
 )
 )
 
 
 // Certificate types are used to specify whether a certificate is for identification
 // Certificate types are used to specify whether a certificate is for identification
@@ -41,10 +38,7 @@ type tuple struct {
 }
 }
 
 
 // An OpenSSHCertV01 represents an OpenSSH certificate as defined in
 // An OpenSSHCertV01 represents an OpenSSH certificate as defined in
-// [PROTOCOL.certkeys] rev 1.8. Supported formats include
-// ssh-rsa-cert-v01@openssh.com, ssh-dss-cert-v01@openssh.com,
-// ecdsa-sha2-nistp256-cert-v01@openssh.com, ecdsa-sha2-nistp384-cert-v01@openssh.com,
-// and ecdsa-sha2-nistp521-cert-v01@openssh.com.
+// [PROTOCOL.certkeys]?rev=1.8.
 type OpenSSHCertV01 struct {
 type OpenSSHCertV01 struct {
 	Nonce                   []byte
 	Nonce                   []byte
 	Key                     interface{} // rsa, dsa, or ecdsa *PublicKey
 	Key                     interface{} // rsa, dsa, or ecdsa *PublicKey
@@ -68,19 +62,19 @@ func parseOpenSSHCertV01(in []byte, algo string) (out *OpenSSHCertV01, rest []by
 	}
 	}
 
 
 	switch algo {
 	switch algo {
-	case certAlgoRSAv01:
+	case CertAlgoRSAv01:
 		var rsaPubKey *rsa.PublicKey
 		var rsaPubKey *rsa.PublicKey
 		if rsaPubKey, in, ok = parseRSA(in); !ok {
 		if rsaPubKey, in, ok = parseRSA(in); !ok {
 			return
 			return
 		}
 		}
 		cert.Key = rsaPubKey
 		cert.Key = rsaPubKey
-	case certAlgoDSAv01:
+	case CertAlgoDSAv01:
 		var dsaPubKey *dsa.PublicKey
 		var dsaPubKey *dsa.PublicKey
 		if dsaPubKey, in, ok = parseDSA(in); !ok {
 		if dsaPubKey, in, ok = parseDSA(in); !ok {
 			return
 			return
 		}
 		}
 		cert.Key = dsaPubKey
 		cert.Key = dsaPubKey
-	case certAlgoECDSA256v01, certAlgoECDSA384v01, certAlgoECDSA521v01:
+	case CertAlgoECDSA256v01, CertAlgoECDSA384v01, CertAlgoECDSA521v01:
 		var ecdsaPubKey *ecdsa.PublicKey
 		var ecdsaPubKey *ecdsa.PublicKey
 		if ecdsaPubKey, in, ok = parseECDSA(in); !ok {
 		if ecdsaPubKey, in, ok = parseECDSA(in); !ok {
 			return
 			return

+ 15 - 15
ssh/common.go

@@ -193,16 +193,16 @@ func serializeSignature(algoname string, sig []byte) []byte {
 	// The corresponding private key to a public certificate is always a normal
 	// The corresponding private key to a public certificate is always a normal
 	// private key.  For signature serialization purposes, ensure we use the
 	// private key.  For signature serialization purposes, ensure we use the
 	// proper key algorithm name in case the public cert algorithm name is passed.
 	// proper key algorithm name in case the public cert algorithm name is passed.
-	case certAlgoRSAv01:
-		algoname = "ssh-rsa"
-	case certAlgoDSAv01:
-		algoname = "ssh-dss"
-	case certAlgoECDSA256v01:
-		algoname = "ecdsa-sha2-nistp256"
-	case certAlgoECDSA384v01:
-		algoname = "ecdsa-sha2-nistp384"
-	case certAlgoECDSA521v01:
-		algoname = "ecdsa-sha2-nistp521"
+	case CertAlgoRSAv01:
+		algoname = KeyAlgoRSA
+	case CertAlgoDSAv01:
+		algoname = KeyAlgoDSA
+	case CertAlgoECDSA256v01:
+		algoname = KeyAlgoECDSA256
+	case CertAlgoECDSA384v01:
+		algoname = KeyAlgoECDSA384
+	case CertAlgoECDSA521v01:
+		algoname = KeyAlgoECDSA521
 	}
 	}
 	length := stringLength(len(algoname))
 	length := stringLength(len(algoname))
 	length += stringLength(len(sig))
 	length += stringLength(len(sig))
@@ -242,17 +242,17 @@ func serializePublickey(key interface{}) []byte {
 func algoName(key interface{}) string {
 func algoName(key interface{}) string {
 	switch key.(type) {
 	switch key.(type) {
 	case *rsa.PublicKey:
 	case *rsa.PublicKey:
-		return "ssh-rsa"
+		return KeyAlgoRSA
 	case *dsa.PublicKey:
 	case *dsa.PublicKey:
-		return "ssh-dss"
+		return KeyAlgoDSA
 	case *ecdsa.PublicKey:
 	case *ecdsa.PublicKey:
 		switch key.(*ecdsa.PublicKey).Params().BitSize {
 		switch key.(*ecdsa.PublicKey).Params().BitSize {
 		case 256:
 		case 256:
-			return "ecdsa-sha2-nistp256"
+			return KeyAlgoECDSA256
 		case 384:
 		case 384:
-			return "ecdsa-sha2-nistp384"
+			return KeyAlgoECDSA384
 		case 521:
 		case 521:
-			return "ecdsa-sha2-nistp521"
+			return KeyAlgoECDSA521
 		}
 		}
 	case *OpenSSHCertV01:
 	case *OpenSSHCertV01:
 		return algoName(key.(*OpenSSHCertV01).Key) + "-cert-v01@openssh.com"
 		return algoName(key.(*OpenSSHCertV01).Key) + "-cert-v01@openssh.com"

+ 5 - 0
ssh/doc.go

@@ -10,5 +10,10 @@ family of application protocols. The most typical application level
 protocol is a remote shell and this is specifically implemented.  However,
 protocol is a remote shell and this is specifically implemented.  However,
 the multiplexed nature of SSH is exposed to users that wish to support
 the multiplexed nature of SSH is exposed to users that wish to support
 others.
 others.
+
+References:
+  [PROTOCOL.certkeys]: http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.certkeys
+  [PROTOCOL.agent]:    http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.agent
+  [SSH-PARAMETERS]:    http://www.iana.org/assignments/ssh-parameters/ssh-parameters.xml#ssh-parameters-1
 */
 */
 package ssh
 package ssh

+ 26 - 26
ssh/keys.go

@@ -16,11 +16,11 @@ import (
 
 
 // Key types supported by OpenSSH 5.9
 // Key types supported by OpenSSH 5.9
 const (
 const (
-	keyAlgoRSA      = "ssh-rsa"
-	keyAlgoDSA      = "ssh-dss"
-	keyAlgoECDSA256 = "ecdsa-sha2-nistp256"
-	keyAlgoECDSA384 = "ecdsa-sha2-nistp384"
-	keyAlgoECDSA521 = "ecdsa-sha2-nistp521"
+	KeyAlgoRSA      = "ssh-rsa"
+	KeyAlgoDSA      = "ssh-dss"
+	KeyAlgoECDSA256 = "ecdsa-sha2-nistp256"
+	KeyAlgoECDSA384 = "ecdsa-sha2-nistp384"
+	KeyAlgoECDSA521 = "ecdsa-sha2-nistp521"
 )
 )
 
 
 // parsePubKey parses a public key according to RFC 4253, section 6.6.
 // parsePubKey parses a public key according to RFC 4253, section 6.6.
@@ -31,13 +31,13 @@ func parsePubKey(in []byte) (out interface{}, rest []byte, ok bool) {
 	}
 	}
 
 
 	switch string(algo) {
 	switch string(algo) {
-	case keyAlgoRSA:
+	case KeyAlgoRSA:
 		return parseRSA(in)
 		return parseRSA(in)
-	case keyAlgoDSA:
+	case KeyAlgoDSA:
 		return parseDSA(in)
 		return parseDSA(in)
-	case keyAlgoECDSA256, keyAlgoECDSA384, keyAlgoECDSA521:
+	case KeyAlgoECDSA256, KeyAlgoECDSA384, KeyAlgoECDSA521:
 		return parseECDSA(in)
 		return parseECDSA(in)
-	case certAlgoRSAv01, certAlgoDSAv01, certAlgoECDSA256v01, certAlgoECDSA384v01, certAlgoECDSA521v01:
+	case CertAlgoRSAv01, CertAlgoDSAv01, CertAlgoECDSA256v01, CertAlgoECDSA384v01, CertAlgoECDSA521v01:
 		return parseOpenSSHCertV01(in, string(algo))
 		return parseOpenSSHCertV01(in, string(algo))
 	}
 	}
 	panic("ssh: unknown public key type")
 	panic("ssh: unknown public key type")
@@ -127,12 +127,12 @@ func parseECDSA(in []byte) (out *ecdsa.PublicKey, rest []byte, ok bool) {
 // marshalPrivRSA serializes an RSA private key according to RFC 4253, section 6.6.
 // marshalPrivRSA serializes an RSA private key according to RFC 4253, section 6.6.
 func marshalPrivRSA(priv *rsa.PrivateKey) []byte {
 func marshalPrivRSA(priv *rsa.PrivateKey) []byte {
 	e := new(big.Int).SetInt64(int64(priv.E))
 	e := new(big.Int).SetInt64(int64(priv.E))
-	length := stringLength(len(keyAlgoRSA))
+	length := stringLength(len(KeyAlgoRSA))
 	length += intLength(e)
 	length += intLength(e)
 	length += intLength(priv.N)
 	length += intLength(priv.N)
 
 
 	ret := make([]byte, length)
 	ret := make([]byte, length)
-	r := marshalString(ret, []byte(keyAlgoRSA))
+	r := marshalString(ret, []byte(KeyAlgoRSA))
 	r = marshalInt(r, e)
 	r = marshalInt(r, e)
 	r = marshalInt(r, priv.N)
 	r = marshalInt(r, priv.N)
 
 
@@ -249,17 +249,17 @@ func ParseAuthorizedKey(in []byte) (out interface{}, comment string, options []s
 
 
 		field := string(in[:i])
 		field := string(in[:i])
 		switch field {
 		switch field {
-		case keyAlgoRSA, keyAlgoDSA:
+		case KeyAlgoRSA, KeyAlgoDSA:
 			out, comment, ok = parseAuthorizedKey(in[i:])
 			out, comment, ok = parseAuthorizedKey(in[i:])
 			if ok {
 			if ok {
 				return
 				return
 			}
 			}
-		case keyAlgoECDSA256, keyAlgoECDSA384, keyAlgoECDSA521:
+		case KeyAlgoECDSA256, KeyAlgoECDSA384, KeyAlgoECDSA521:
 			// We don't support these keys.
 			// We don't support these keys.
 			in = rest
 			in = rest
 			continue
 			continue
-		case certAlgoRSAv01, certAlgoDSAv01,
-			certAlgoECDSA256v01, certAlgoECDSA384v01, certAlgoECDSA521v01:
+		case CertAlgoRSAv01, CertAlgoDSAv01,
+			CertAlgoECDSA256v01, CertAlgoECDSA384v01, CertAlgoECDSA521v01:
 			// We don't support these certificates.
 			// We don't support these certificates.
 			in = rest
 			in = rest
 			continue
 			continue
@@ -304,7 +304,7 @@ func ParseAuthorizedKey(in []byte) (out interface{}, comment string, options []s
 
 
 		field = string(in[:i])
 		field = string(in[:i])
 		switch field {
 		switch field {
-		case keyAlgoRSA, keyAlgoDSA:
+		case KeyAlgoRSA, KeyAlgoDSA:
 			out, comment, ok = parseAuthorizedKey(in[i:])
 			out, comment, ok = parseAuthorizedKey(in[i:])
 			if ok {
 			if ok {
 				options = candidateOptions
 				options = candidateOptions
@@ -332,34 +332,34 @@ func MarshalAuthorizedKey(key interface{}) []byte {
 	b := &bytes.Buffer{}
 	b := &bytes.Buffer{}
 	switch keyType := key.(type) {
 	switch keyType := key.(type) {
 	case *rsa.PublicKey:
 	case *rsa.PublicKey:
-		b.WriteString(keyAlgoRSA)
+		b.WriteString(KeyAlgoRSA)
 	case *dsa.PublicKey:
 	case *dsa.PublicKey:
-		b.WriteString(keyAlgoDSA)
+		b.WriteString(KeyAlgoDSA)
 	case *ecdsa.PublicKey:
 	case *ecdsa.PublicKey:
 		switch keyType.Params().BitSize {
 		switch keyType.Params().BitSize {
 		case 256:
 		case 256:
-			b.WriteString(keyAlgoECDSA256)
+			b.WriteString(KeyAlgoECDSA256)
 		case 384:
 		case 384:
-			b.WriteString(keyAlgoECDSA384)
+			b.WriteString(KeyAlgoECDSA384)
 		case 521:
 		case 521:
-			b.WriteString(keyAlgoECDSA521)
+			b.WriteString(KeyAlgoECDSA521)
 		default:
 		default:
 			panic("unexpected key type")
 			panic("unexpected key type")
 		}
 		}
 	case *OpenSSHCertV01:
 	case *OpenSSHCertV01:
 		switch keyType.Key.(type) {
 		switch keyType.Key.(type) {
 		case *rsa.PublicKey:
 		case *rsa.PublicKey:
-			b.WriteString(certAlgoRSAv01)
+			b.WriteString(CertAlgoRSAv01)
 		case *dsa.PublicKey:
 		case *dsa.PublicKey:
-			b.WriteString(certAlgoDSAv01)
+			b.WriteString(CertAlgoDSAv01)
 		case *ecdsa.PublicKey:
 		case *ecdsa.PublicKey:
 			switch keyType.Key.(*ecdsa.PublicKey).Params().BitSize {
 			switch keyType.Key.(*ecdsa.PublicKey).Params().BitSize {
 			case 256:
 			case 256:
-				b.WriteString(certAlgoECDSA256v01)
+				b.WriteString(CertAlgoECDSA256v01)
 			case 384:
 			case 384:
-				b.WriteString(certAlgoECDSA384v01)
+				b.WriteString(CertAlgoECDSA384v01)
 			case 521:
 			case 521:
-				b.WriteString(certAlgoECDSA521v01)
+				b.WriteString(CertAlgoECDSA521v01)
 			default:
 			default:
 				panic("unexpected key type")
 				panic("unexpected key type")
 			}
 			}

+ 1 - 2
ssh/messages.go

@@ -13,8 +13,7 @@ import (
 )
 )
 
 
 // These are SSH message type numbers. They are scattered around several
 // These are SSH message type numbers. They are scattered around several
-// documents but many were taken from
-// http://www.iana.org/assignments/ssh-parameters/ssh-parameters.xml#ssh-parameters-1
+// documents but many were taken from [SSH-PARAMETERS].
 const (
 const (
 	msgDisconnect     = 1
 	msgDisconnect     = 1
 	msgIgnore         = 2
 	msgIgnore         = 2