Xray-core/common/ocsp/ocsp.go

137 lines
3.1 KiB
Go
Raw Permalink Normal View History

2020-12-25 08:01:20 +00:00
package ocsp
import (
"bytes"
"crypto/x509"
"encoding/pem"
"io"
2020-12-25 08:01:20 +00:00
"net/http"
"os"
2020-12-25 15:10:12 +00:00
"github.com/xtls/xray-core/common/errors"
2020-12-25 15:10:12 +00:00
"github.com/xtls/xray-core/common/platform/filesystem"
2022-05-18 07:29:01 +00:00
"golang.org/x/crypto/ocsp"
2020-12-25 08:01:20 +00:00
)
func GetOCSPForFile(path string) ([]byte, error) {
return filesystem.ReadFile(path)
}
func CheckOCSPFileIsNotExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
return os.IsNotExist(err)
}
return false
}
func GetOCSPStapling(cert [][]byte, path string) ([]byte, error) {
ocspData, err := GetOCSPForFile(path)
if err != nil {
ocspData, err = GetOCSPForCert(cert)
2023-12-08 17:07:51 +00:00
if err != nil {
return nil, err
}
2020-12-25 08:01:20 +00:00
if !CheckOCSPFileIsNotExist(path) {
err = os.Remove(path)
if err != nil {
return nil, err
}
}
newFile, err := os.Create(path)
if err != nil {
return nil, err
}
newFile.Write(ocspData)
defer newFile.Close()
}
return ocspData, nil
}
func GetOCSPForCert(cert [][]byte) ([]byte, error) {
bundle := new(bytes.Buffer)
for _, derBytes := range cert {
err := pem.Encode(bundle, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return nil, err
}
}
pemBundle := bundle.Bytes()
certificates, err := parsePEMBundle(pemBundle)
if err != nil {
return nil, err
}
issuedCert := certificates[0]
if len(issuedCert.OCSPServer) == 0 {
return nil, errors.New("no OCSP server specified in cert")
2020-12-25 08:01:20 +00:00
}
if len(certificates) == 1 {
if len(issuedCert.IssuingCertificateURL) == 0 {
return nil, errors.New("no issuing certificate URL")
2020-12-25 08:01:20 +00:00
}
resp, errC := http.Get(issuedCert.IssuingCertificateURL[0])
if errC != nil {
return nil, errors.New("no issuing certificate URL")
2020-12-25 08:01:20 +00:00
}
defer resp.Body.Close()
issuerBytes, errC := io.ReadAll(resp.Body)
2020-12-25 08:01:20 +00:00
if errC != nil {
return nil, errors.New(errC)
2020-12-25 08:01:20 +00:00
}
issuerCert, errC := x509.ParseCertificate(issuerBytes)
if errC != nil {
return nil, errors.New(errC)
2020-12-25 08:01:20 +00:00
}
certificates = append(certificates, issuerCert)
}
issuerCert := certificates[1]
ocspReq, err := ocsp.CreateRequest(issuedCert, issuerCert, nil)
if err != nil {
return nil, err
}
reader := bytes.NewReader(ocspReq)
req, err := http.Post(issuedCert.OCSPServer[0], "application/ocsp-request", reader)
if err != nil {
return nil, errors.New(err)
2020-12-25 08:01:20 +00:00
}
defer req.Body.Close()
ocspResBytes, err := io.ReadAll(req.Body)
2020-12-25 08:01:20 +00:00
if err != nil {
return nil, errors.New(err)
2020-12-25 08:01:20 +00:00
}
return ocspResBytes, nil
}
// parsePEMBundle parses a certificate bundle from top to bottom and returns
// a slice of x509 certificates. This function will error if no certificates are found.
func parsePEMBundle(bundle []byte) ([]*x509.Certificate, error) {
var certificates []*x509.Certificate
var certDERBlock *pem.Block
for {
certDERBlock, bundle = pem.Decode(bundle)
if certDERBlock == nil {
break
}
if certDERBlock.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(certDERBlock.Bytes)
if err != nil {
return nil, err
}
certificates = append(certificates, cert)
}
}
if len(certificates) == 0 {
return nil, errors.New("no certificates were found while parsing the bundle")
2020-12-25 08:01:20 +00:00
}
return certificates, nil
}