mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 01:19:20 +08:00 
			
		
		
		
	caddyhttp: Add client.public_key(_sha256) placeholders
This commit is contained in:
		@ -16,9 +16,14 @@ package caddyhttp
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"crypto/ecdsa"
 | 
				
			||||||
 | 
						"crypto/ed25519"
 | 
				
			||||||
 | 
						"crypto/elliptic"
 | 
				
			||||||
 | 
						"crypto/rsa"
 | 
				
			||||||
	"crypto/sha256"
 | 
						"crypto/sha256"
 | 
				
			||||||
	"crypto/tls"
 | 
						"crypto/tls"
 | 
				
			||||||
	"crypto/x509"
 | 
						"crypto/x509"
 | 
				
			||||||
 | 
						"encoding/asn1"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
@ -243,6 +248,18 @@ func getReqTLSReplacement(req *http.Request, key string) (interface{}, bool) {
 | 
				
			|||||||
		switch field {
 | 
							switch field {
 | 
				
			||||||
		case "client.fingerprint":
 | 
							case "client.fingerprint":
 | 
				
			||||||
			return fmt.Sprintf("%x", sha256.Sum256(cert.Raw)), true
 | 
								return fmt.Sprintf("%x", sha256.Sum256(cert.Raw)), true
 | 
				
			||||||
 | 
							case "client.public_key", "client.public_key_sha256":
 | 
				
			||||||
 | 
								if cert.PublicKey == nil {
 | 
				
			||||||
 | 
									return nil, true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								pubKeyBytes, err := marshalPublicKey(cert.PublicKey)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return nil, true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if strings.HasSuffix(field, "_sha256") {
 | 
				
			||||||
 | 
									return fmt.Sprintf("%x", sha256.Sum256(pubKeyBytes)), true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								return fmt.Sprintf("%x", pubKeyBytes), true
 | 
				
			||||||
		case "client.issuer":
 | 
							case "client.issuer":
 | 
				
			||||||
			return cert.Issuer, true
 | 
								return cert.Issuer, true
 | 
				
			||||||
		case "client.serial":
 | 
							case "client.serial":
 | 
				
			||||||
@ -271,6 +288,19 @@ func getReqTLSReplacement(req *http.Request, key string) (interface{}, bool) {
 | 
				
			|||||||
	return nil, false
 | 
						return nil, false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// marshalPublicKey returns the byte encoding of pubKey.
 | 
				
			||||||
 | 
					func marshalPublicKey(pubKey interface{}) ([]byte, error) {
 | 
				
			||||||
 | 
						switch key := pubKey.(type) {
 | 
				
			||||||
 | 
						case *rsa.PublicKey:
 | 
				
			||||||
 | 
							return asn1.Marshal(key)
 | 
				
			||||||
 | 
						case *ecdsa.PublicKey:
 | 
				
			||||||
 | 
							return elliptic.Marshal(key.Curve, key.X, key.Y), nil
 | 
				
			||||||
 | 
						case ed25519.PublicKey:
 | 
				
			||||||
 | 
							return key, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil, fmt.Errorf("unrecognized public key type: %T", pubKey)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// getTLSPeerCert retrieves the first peer certificate from a TLS session.
 | 
					// getTLSPeerCert retrieves the first peer certificate from a TLS session.
 | 
				
			||||||
// Returns nil if no peer cert is in use.
 | 
					// Returns nil if no peer cert is in use.
 | 
				
			||||||
func getTLSPeerCert(cs *tls.ConnectionState) *x509.Certificate {
 | 
					func getTLSPeerCert(cs *tls.ConnectionState) *x509.Certificate {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user