2020-11-11 18:48:37 +00:00
|
|
|
//
|
|
|
|
// Copyright 2020 FoxyUtils ehf. All rights reserved.
|
|
|
|
//
|
|
|
|
// This is a commercial product and requires a license to operate.
|
|
|
|
// A trial license can be obtained at https://unidoc.io
|
|
|
|
//
|
|
|
|
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by the UniDoc End User License Agreement
|
|
|
|
// terms that can be accessed at https://unidoc.io/eula/
|
|
|
|
|
2023-08-03 17:30:04 +00:00
|
|
|
package sigutil ;import (_g "bytes";_gg "crypto";_ac "crypto/x509";_ag "encoding/asn1";_de "encoding/pem";_d "errors";_gb "fmt";_cb "github.com/unidoc/timestamp";_bc "github.com/unidoc/unipdf/v3/common";_aca "golang.org/x/crypto/ocsp";_gd "io";_a "io/ioutil";
|
|
|
|
_b "net/http";_c "time";);
|
2023-05-29 17:26:33 +00:00
|
|
|
|
2023-08-03 17:30:04 +00:00
|
|
|
// Get retrieves the certificate at the specified URL.
|
|
|
|
func (_bf *CertClient )Get (url string )(*_ac .Certificate ,error ){if _bf .HTTPClient ==nil {_bf .HTTPClient =_fdg ();};_dc ,_fa :=_bf .HTTPClient .Get (url );if _fa !=nil {return nil ,_fa ;};defer _dc .Body .Close ();_fg ,_fa :=_a .ReadAll (_dc .Body );
|
|
|
|
if _fa !=nil {return nil ,_fa ;};if _ga ,_ :=_de .Decode (_fg );_ga !=nil {_fg =_ga .Bytes ;};_fb ,_fa :=_ac .ParseCertificate (_fg );if _fa !=nil {return nil ,_fa ;};return _fb ,nil ;};
|
|
|
|
|
|
|
|
// CRLClient represents a CRL (Certificate revocation list) client.
|
|
|
|
// It is used to request revocation data from CRL servers.
|
|
|
|
type CRLClient struct{
|
|
|
|
|
|
|
|
// HTTPClient is the HTTP client used to make CRL requests.
|
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
|
|
|
HTTPClient *_b .Client ;};
|
|
|
|
|
|
|
|
// GetIssuer retrieves the issuer of the provided certificate.
|
|
|
|
func (_cc *CertClient )GetIssuer (cert *_ac .Certificate )(*_ac .Certificate ,error ){for _ ,_cba :=range cert .IssuingCertificateURL {_e ,_eb :=_cc .Get (_cba );if _eb !=nil {_bc .Log .Debug ("\u0057\u0041\u0052\u004e\u003a\u0020\u0063\u006f\u0075\u006c\u0064\u0020\u006e\u006f\u0074 \u0064\u006f\u0077\u006e\u006c\u006f\u0061\u0064\u0020\u0069\u0073\u0073\u0075e\u0072\u0020\u0066\u006f\u0072\u0020\u0063\u0065\u0072\u0074\u0069\u0066ic\u0061\u0074\u0065\u0020\u0025\u0076\u003a\u0020\u0025\u0076",cert .Subject .CommonName ,_eb );
|
|
|
|
continue ;};return _e ,nil ;};return nil ,_gb .Errorf ("\u0069\u0073\u0073\u0075e\u0072\u0020\u0063\u0065\u0072\u0074\u0069\u0066\u0069\u0063a\u0074e\u0020\u006e\u006f\u0074\u0020\u0066\u006fu\u006e\u0064");};
|
|
|
|
|
|
|
|
// OCSPClient represents a OCSP (Online Certificate Status Protocol) client.
|
|
|
|
// It is used to request revocation data from OCSP servers.
|
|
|
|
type OCSPClient struct{
|
|
|
|
|
|
|
|
// HTTPClient is the HTTP client used to make OCSP requests.
|
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
|
|
|
HTTPClient *_b .Client ;
|
|
|
|
|
|
|
|
// Hash is the hash function used when constructing the OCSP
|
|
|
|
// requests. If zero, SHA-1 will be used.
|
|
|
|
Hash _gg .Hash ;};
|
2022-12-15 21:59:56 +00:00
|
|
|
|
2023-07-28 12:14:31 +00:00
|
|
|
// NewTimestampRequest returns a new timestamp request based
|
|
|
|
// on the specified options.
|
2023-08-03 17:30:04 +00:00
|
|
|
func NewTimestampRequest (body _gd .Reader ,opts *_cb .RequestOptions )(*_cb .Request ,error ){if opts ==nil {opts =&_cb .RequestOptions {};};if opts .Hash ==0{opts .Hash =_gg .SHA256 ;};if !opts .Hash .Available (){return nil ,_ac .ErrUnsupportedAlgorithm ;
|
|
|
|
};_deb :=opts .Hash .New ();if _ ,_ebd :=_gd .Copy (_deb ,body );_ebd !=nil {return nil ,_ebd ;};return &_cb .Request {HashAlgorithm :opts .Hash ,HashedMessage :_deb .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
|
2023-07-28 12:14:31 +00:00
|
|
|
};
|
2023-06-30 13:19:48 +00:00
|
|
|
|
2023-08-03 17:30:04 +00:00
|
|
|
// MakeRequest makes a CRL request to the specified server and returns the
|
|
|
|
// response. If a server URL is not provided, it is extracted from the certificate.
|
|
|
|
func (_ccc *CRLClient )MakeRequest (serverURL string ,cert *_ac .Certificate )([]byte ,error ){if _ccc .HTTPClient ==nil {_ccc .HTTPClient =_fdg ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_d .New ("\u0063e\u0072\u0074i\u0066\u0069\u0063\u0061t\u0065\u0020\u0064o\u0065\u0073\u0020\u006e\u006f\u0074\u0020\u0073\u0070ec\u0069\u0066\u0079 \u0061\u006ey\u0020\u0043\u0052\u004c\u0020\u0073e\u0072\u0076e\u0072\u0073");
|
|
|
|
};serverURL =cert .CRLDistributionPoints [0];};_fgb ,_cbb :=_ccc .HTTPClient .Get (serverURL );if _cbb !=nil {return nil ,_cbb ;};defer _fgb .Body .Close ();_fc ,_cbb :=_a .ReadAll (_fgb .Body );if _cbb !=nil {return nil ,_cbb ;};if _cce ,_ :=_de .Decode (_fc );
|
|
|
|
_cce !=nil {_fc =_cce .Bytes ;};return _fc ,nil ;};
|
|
|
|
|
|
|
|
// MakeRequest makes a OCSP request to the specified server and returns
|
|
|
|
// the parsed and raw responses. If a server URL is not provided, it is
|
|
|
|
// extracted from the certificate.
|
|
|
|
func (_dd *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_ac .Certificate )(*_aca .Response ,[]byte ,error ){if _dd .HTTPClient ==nil {_dd .HTTPClient =_fdg ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_d .New ("\u0063e\u0072\u0074i\u0066\u0069\u0063a\u0074\u0065\u0020\u0064\u006f\u0065\u0073 \u006e\u006f\u0074\u0020\u0073\u0070e\u0063\u0069\u0066\u0079\u0020\u0061\u006e\u0079\u0020\u004f\u0043S\u0050\u0020\u0073\u0065\u0072\u0076\u0065\u0072\u0073");
|
|
|
|
};serverURL =cert .OCSPServer [0];};_ba ,_ed :=_aca .CreateRequest (cert ,issuer ,&_aca .RequestOptions {Hash :_dd .Hash });if _ed !=nil {return nil ,nil ,_ed ;};_ee ,_ed :=_dd .HTTPClient .Post (serverURL ,"\u0061p\u0070\u006c\u0069\u0063\u0061\u0074\u0069\u006f\u006e\u002f\u006fc\u0073\u0070\u002d\u0072\u0065\u0071\u0075\u0065\u0073\u0074",_g .NewReader (_ba ));
|
|
|
|
if _ed !=nil {return nil ,nil ,_ed ;};defer _ee .Body .Close ();_cg ,_ed :=_a .ReadAll (_ee .Body );if _ed !=nil {return nil ,nil ,_ed ;};if _df ,_ :=_de .Decode (_cg );_df !=nil {_cg =_df .Bytes ;};_ef ,_ed :=_aca .ParseResponseForCert (_cg ,cert ,issuer );
|
|
|
|
if _ed !=nil {return nil ,nil ,_ed ;};return _ef ,_cg ,nil ;};
|
|
|
|
|
|
|
|
// NewTimestampClient returns a new timestamp client.
|
|
|
|
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_fdg ()}};
|
|
|
|
|
2023-07-28 12:14:31 +00:00
|
|
|
// GetEncodedToken executes the timestamp request and returns the DER encoded
|
|
|
|
// timestamp token bytes.
|
2023-08-03 17:30:04 +00:00
|
|
|
func (_bd *TimestampClient )GetEncodedToken (serverURL string ,req *_cb .Request )([]byte ,error ){if serverURL ==""{return nil ,_gb .Errorf ("\u006d\u0075\u0073\u0074\u0020\u0070r\u006f\u0076\u0069\u0064\u0065\u0020\u0074\u0069\u006d\u0065\u0073\u0074\u0061m\u0070\u0020\u0073\u0065\u0072\u0076\u0065r\u0020\u0055\u0052\u004c");
|
|
|
|
};if req ==nil {return nil ,_gb .Errorf ("\u0074\u0069\u006de\u0073\u0074\u0061\u006dp\u0020\u0072\u0065\u0071\u0075\u0065\u0073t\u0020\u0063\u0061\u006e\u006e\u006f\u0074\u0020\u0062\u0065\u0020\u006e\u0069\u006c");};_efd ,_gfg :=req .Marshal ();if _gfg !=nil {return nil ,_gfg ;
|
|
|
|
};_ebf ,_gfg :=_b .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_g .NewBuffer (_efd ));if _gfg !=nil {return nil ,_gfg ;};_ebf .Header .Set ("\u0043\u006f\u006et\u0065\u006e\u0074\u002d\u0054\u0079\u0070\u0065","a\u0070\u0070\u006c\u0069\u0063\u0061t\u0069\u006f\u006e\u002f\u0074\u0069\u006d\u0065\u0073t\u0061\u006d\u0070-\u0071u\u0065\u0072\u0079");
|
|
|
|
if _bd .BeforeHTTPRequest !=nil {if _fab :=_bd .BeforeHTTPRequest (_ebf );_fab !=nil {return nil ,_fab ;};};_bg :=_bd .HTTPClient ;if _bg ==nil {_bg =_fdg ();};_ca ,_gfg :=_bg .Do (_ebf );if _gfg !=nil {return nil ,_gfg ;};defer _ca .Body .Close ();_bcb ,_gfg :=_a .ReadAll (_ca .Body );
|
|
|
|
if _gfg !=nil {return nil ,_gfg ;};if _ca .StatusCode !=_b .StatusOK {return nil ,_gb .Errorf ("\u0075\u006e\u0065x\u0070\u0065\u0063\u0074e\u0064\u0020\u0048\u0054\u0054\u0050\u0020s\u0074\u0061\u0074\u0075\u0073\u0020\u0063\u006f\u0064\u0065\u003a\u0020\u0025\u0064",_ca .StatusCode );
|
|
|
|
};var _fd struct{Version _ag .RawValue ;Content _ag .RawValue ;};if _ ,_gfg =_ag .Unmarshal (_bcb ,&_fd );_gfg !=nil {return nil ,_gfg ;};return _fd .Content .FullBytes ,nil ;};
|
2023-07-28 12:14:31 +00:00
|
|
|
|
2023-08-03 17:30:04 +00:00
|
|
|
// IsCA returns true if the provided certificate appears to be a CA certificate.
|
|
|
|
func (_be *CertClient )IsCA (cert *_ac .Certificate )bool {return cert .IsCA &&_g .Equal (cert .RawIssuer ,cert .RawSubject );};
|
|
|
|
|
|
|
|
// NewCertClient returns a new certificate client.
|
|
|
|
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_fdg ()}};
|
|
|
|
|
|
|
|
// NewOCSPClient returns a new OCSP client.
|
|
|
|
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_fdg (),Hash :_gg .SHA1 }};
|
2023-07-28 12:14:31 +00:00
|
|
|
|
|
|
|
// CertClient represents a X.509 certificate client. Its primary purpose
|
|
|
|
// is to download certificates.
|
|
|
|
type CertClient struct{
|
|
|
|
|
|
|
|
// HTTPClient is the HTTP client used to make certificate requests.
|
2023-06-30 13:19:48 +00:00
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2023-08-03 17:30:04 +00:00
|
|
|
HTTPClient *_b .Client ;};func _fdg ()*_b .Client {return &_b .Client {Timeout :5*_c .Second }};
|
2023-02-07 17:17:49 +00:00
|
|
|
|
2023-05-29 17:26:33 +00:00
|
|
|
// TimestampClient represents a RFC 3161 timestamp client.
|
|
|
|
// It is used to obtain signed tokens from timestamp authority servers.
|
|
|
|
type TimestampClient struct{
|
2023-02-07 17:17:49 +00:00
|
|
|
|
2023-05-29 17:26:33 +00:00
|
|
|
// HTTPClient is the HTTP client used to make timestamp requests.
|
2023-04-06 19:57:40 +00:00
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2023-08-03 17:30:04 +00:00
|
|
|
HTTPClient *_b .Client ;
|
2023-03-01 18:45:57 +00:00
|
|
|
|
2023-05-29 17:26:33 +00:00
|
|
|
// Callbacks.
|
2023-08-03 17:30:04 +00:00
|
|
|
BeforeHTTPRequest func (_ce *_b .Request )error ;};
|
2023-05-29 17:26:33 +00:00
|
|
|
|
2023-08-03 17:30:04 +00:00
|
|
|
// NewCRLClient returns a new CRL client.
|
|
|
|
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_fdg ()}};
|