-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'google:master' into sot-migration
- Loading branch information
Showing
8 changed files
with
223 additions
and
49 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
51 changes: 33 additions & 18 deletions
51
fleetspeak/src/server/components/proto/fleetspeak_components/config.pb.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,52 @@ | ||
package https | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// GetClientCert returns the client certificate from either the request header or TLS connection state. | ||
func GetClientCert(req *http.Request, hn string) (*x509.Certificate, error) { | ||
if hn != "" { | ||
return getCertFromHeader(hn, req.Header) | ||
} else { | ||
return getCertFromTLS(req) | ||
} | ||
} | ||
|
||
func getCertFromHeader(hn string, rh http.Header) (*x509.Certificate, error) { | ||
headerCert := rh.Get(hn) | ||
if headerCert == "" { | ||
return nil, errors.New("no certificate found in header") | ||
} | ||
// Most certificates are URL PEM encoded | ||
if decodedCert, err := url.PathUnescape(headerCert); err != nil { | ||
return nil, err | ||
} else { | ||
headerCert = decodedCert | ||
} | ||
block, rest := pem.Decode([]byte(headerCert)) | ||
if block == nil || block.Type != "CERTIFICATE" { | ||
return nil, errors.New("failed to decode PEM block containing certificate") | ||
} | ||
if len(rest) != 0 { | ||
return nil, errors.New("received more than 1 client cert") | ||
} | ||
cert, err := x509.ParseCertificate(block.Bytes) | ||
return cert, err | ||
} | ||
|
||
func getCertFromTLS(req *http.Request) (*x509.Certificate, error) { | ||
if req.TLS == nil { | ||
return nil, errors.New("TLS information not found") | ||
} | ||
if len(req.TLS.PeerCertificates) != 1 { | ||
return nil, fmt.Errorf("expected 1 client cert, received %v", len(req.TLS.PeerCertificates)) | ||
} | ||
cert := req.TLS.PeerCertificates[0] | ||
return cert, 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
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
Oops, something went wrong.