]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/crypto/tls/auth.go
Use GoGOST's PublicKey wrappers
[gostls13.git] / src / crypto / tls / auth.go
index a807e0528e4e55608e42e8b0f1758e619f49f9c2..58fd92cb3d9e96e3962fb296b75c810e4c0fc50e 100644 (file)
@@ -15,6 +15,8 @@ import (
        "fmt"
        "hash"
        "io"
+
+       "crypto/go.cypherpunks.ru/gogost/v5/gost3410"
 )
 
 // verifyHandshakeSignature verifies a signature against pre-hashed
@@ -54,6 +56,18 @@ func verifyHandshakeSignature(sigType uint8, pubkey crypto.PublicKey, hashFunc c
                if err := rsa.VerifyPSS(pubKey, hashFunc, signed, sig, signOpts); err != nil {
                        return err
                }
+       case signatureGOST:
+               pubKey, ok := pubkey.(*gost3410.PublicKey)
+               if !ok {
+                       return fmt.Errorf("expected GOST public key, got %T", pubkey)
+               }
+               ok, err := gost3410.PublicKeyReverseDigestAndSignature{Pub: pubKey}.VerifyDigest(signed, sig)
+               if err != nil {
+                       return err
+               }
+               if !ok {
+                       return errors.New("tls: GOST verification failure")
+               }
        default:
                return errors.New("internal error: unknown signature type")
        }
@@ -105,6 +119,8 @@ func typeAndHashFromSignatureScheme(signatureAlgorithm SignatureScheme) (sigType
                sigType = signatureECDSA
        case Ed25519:
                sigType = signatureEd25519
+       case GOSTR34102012256A, GOSTR34102012256B, GOSTR34102012256C, GOSTR34102012256D, GOSTR34102012512A, GOSTR34102012512B, GOSTR34102012512C:
+               sigType = signatureGOST
        default:
                return 0, 0, fmt.Errorf("unsupported signature algorithm: %v", signatureAlgorithm)
        }
@@ -119,6 +135,10 @@ func typeAndHashFromSignatureScheme(signatureAlgorithm SignatureScheme) (sigType
                hash = crypto.SHA512
        case Ed25519:
                hash = directSigning
+       case GOSTR34102012256A, GOSTR34102012256B, GOSTR34102012256C, GOSTR34102012256D:
+               hash = crypto.GOSTR34112012256
+       case GOSTR34102012512A, GOSTR34102012512B, GOSTR34102012512C:
+               hash = crypto.GOSTR34112012512
        default:
                return 0, 0, fmt.Errorf("unsupported signature algorithm: %v", signatureAlgorithm)
        }
@@ -155,9 +175,9 @@ var rsaSignatureSchemes = []struct {
        {PSSWithSHA256, crypto.SHA256.Size()*2 + 2, VersionTLS13},
        {PSSWithSHA384, crypto.SHA384.Size()*2 + 2, VersionTLS13},
        {PSSWithSHA512, crypto.SHA512.Size()*2 + 2, VersionTLS13},
-       // PKCS#1 v1.5 uses prefixes from hashPrefixes in crypto/rsa, and requires
+       // PKCS #1 v1.5 uses prefixes from hashPrefixes in crypto/rsa, and requires
        //    emLen >= len(prefix) + hLen + 11
-       // TLS 1.3 dropped support for PKCS#1 v1.5 in favor of RSA-PSS.
+       // TLS 1.3 dropped support for PKCS #1 v1.5 in favor of RSA-PSS.
        {PKCS1WithSHA256, 19 + crypto.SHA256.Size() + 11, VersionTLS12},
        {PKCS1WithSHA384, 19 + crypto.SHA384.Size() + 11, VersionTLS12},
        {PKCS1WithSHA512, 19 + crypto.SHA512.Size() + 11, VersionTLS12},
@@ -169,6 +189,7 @@ var rsaSignatureSchemes = []struct {
 // and optionally filtered by its explicit SupportedSignatureAlgorithms.
 //
 // This function must be kept in sync with supportedSignatureAlgorithms.
+// FIPS filtering is applied in the caller, selectSignatureScheme.
 func signatureSchemesForCertificate(version uint16, cert *Certificate) []SignatureScheme {
        priv, ok := cert.PrivateKey.(crypto.Signer)
        if !ok {
@@ -209,6 +230,25 @@ func signatureSchemesForCertificate(version uint16, cert *Certificate) []Signatu
                }
        case ed25519.PublicKey:
                sigAlgs = []SignatureScheme{Ed25519}
+       case *gost3410.PublicKey:
+               switch pub.C.Name {
+               case "id-tc26-gost-3410-12-256-paramSetA":
+                       return []SignatureScheme{GOSTR34102012256A}
+               case "id-tc26-gost-3410-12-256-paramSetB":
+                       return []SignatureScheme{GOSTR34102012256B}
+               case "id-tc26-gost-3410-12-256-paramSetC":
+                       return []SignatureScheme{GOSTR34102012256C}
+               case "id-tc26-gost-3410-12-256-paramSetD":
+                       return []SignatureScheme{GOSTR34102012256D}
+               case "id-tc26-gost-3410-12-512-paramSetA":
+                       return []SignatureScheme{GOSTR34102012512A}
+               case "id-tc26-gost-3410-12-512-paramSetB":
+                       return []SignatureScheme{GOSTR34102012512B}
+               case "id-tc26-gost-3410-12-512-paramSetC":
+                       return []SignatureScheme{GOSTR34102012512C}
+               default:
+                       return nil
+               }
        default:
                return nil
        }
@@ -280,6 +320,7 @@ func unsupportedCertificateError(cert *Certificate) error {
        case *rsa.PublicKey:
                return fmt.Errorf("tls: certificate RSA key size too small for supported signature algorithms")
        case ed25519.PublicKey:
+       case *gost3410.PublicKey:
        default:
                return fmt.Errorf("tls: unsupported certificate key (%T)", pub)
        }