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/
|
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
package sigutil ;import (_d "bytes";_ec "crypto";_ee "crypto/x509";_g "encoding/asn1";_ece "encoding/pem";_f "errors";_c "fmt";_cf "github.com/unidoc/timestamp";_fb "github.com/unidoc/unipdf/v3/common";_gd "golang.org/x/crypto/ocsp";_de "io";_bg "io/ioutil";
|
|
|
|
_eb "net/http";_b "time";);
|
2024-04-16 11:40:43 +00:00
|
|
|
|
2024-06-27 16:15:49 +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 (_ab *CRLClient )MakeRequest (serverURL string ,cert *_ee .Certificate )([]byte ,error ){if _ab .HTTPClient ==nil {_ab .HTTPClient =_bc ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_f .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];};_af ,_ff :=_ab .HTTPClient .Get (serverURL );if _ff !=nil {return nil ,_ff ;};defer _af .Body .Close ();_gg ,_ff :=_bg .ReadAll (_af .Body );if _ff !=nil {return nil ,_ff ;};if _dg ,_ :=_ece .Decode (_gg );
|
|
|
|
_dg !=nil {_gg =_dg .Bytes ;};return _gg ,nil ;};
|
2023-08-03 17:30:04 +00:00
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
// NewTimestampClient returns a new timestamp client.
|
|
|
|
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_bc ()}};
|
2023-08-03 17:30:04 +00:00
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
// GetIssuer retrieves the issuer of the provided certificate.
|
|
|
|
func (_ded *CertClient )GetIssuer (cert *_ee .Certificate )(*_ee .Certificate ,error ){for _ ,_df :=range cert .IssuingCertificateURL {_fe ,_db :=_ded .Get (_df );if _db !=nil {_fb .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 ,_db );
|
|
|
|
continue ;};return _fe ,nil ;};return nil ,_c .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");};
|
|
|
|
|
|
|
|
// NewCRLClient returns a new CRL client.
|
|
|
|
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_bc ()}};
|
|
|
|
|
|
|
|
// 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 (_ffb *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_ee .Certificate )(*_gd .Response ,[]byte ,error ){if _ffb .HTTPClient ==nil {_ffb .HTTPClient =_bc ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_f .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];};_gdb ,_fg :=_gd .CreateRequest (cert ,issuer ,&_gd .RequestOptions {Hash :_ffb .Hash });if _fg !=nil {return nil ,nil ,_fg ;};_ef ,_fg :=_ffb .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",_d .NewReader (_gdb ));
|
|
|
|
if _fg !=nil {return nil ,nil ,_fg ;};defer _ef .Body .Close ();_aa ,_fg :=_bg .ReadAll (_ef .Body );if _fg !=nil {return nil ,nil ,_fg ;};if _fef ,_ :=_ece .Decode (_aa );_fef !=nil {_aa =_fef .Bytes ;};_ffbc ,_fg :=_gd .ParseResponseForCert (_aa ,cert ,issuer );
|
|
|
|
if _fg !=nil {return nil ,nil ,_fg ;};return _ffbc ,_aa ,nil ;};
|
|
|
|
|
|
|
|
// IsCA returns true if the provided certificate appears to be a CA certificate.
|
|
|
|
func (_dfd *CertClient )IsCA (cert *_ee .Certificate )bool {return cert .IsCA &&_d .Equal (cert .RawIssuer ,cert .RawSubject );};
|
2024-04-16 11:40:43 +00:00
|
|
|
|
2024-05-29 17:04:37 +00:00
|
|
|
// OCSPClient represents a OCSP (Online Certificate Status Protocol) client.
|
|
|
|
// It is used to request revocation data from OCSP servers.
|
|
|
|
type OCSPClient struct{
|
2024-04-16 11:40:43 +00:00
|
|
|
|
2024-05-29 17:04:37 +00:00
|
|
|
// HTTPClient is the HTTP client used to make OCSP requests.
|
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2024-06-27 16:15:49 +00:00
|
|
|
HTTPClient *_eb .Client ;
|
2024-04-30 12:24:05 +00:00
|
|
|
|
2024-05-29 17:04:37 +00:00
|
|
|
// Hash is the hash function used when constructing the OCSP
|
|
|
|
// requests. If zero, SHA-1 will be used.
|
2024-06-27 16:15:49 +00:00
|
|
|
Hash _ec .Hash ;};
|
2024-04-30 12:24:05 +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.
|
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2024-06-27 16:15:49 +00:00
|
|
|
HTTPClient *_eb .Client ;};
|
2024-04-30 12:24:05 +00:00
|
|
|
|
2024-05-29 17:04:37 +00:00
|
|
|
// NewCertClient returns a new certificate client.
|
2024-06-27 16:15:49 +00:00
|
|
|
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_bc ()}};func _bc ()*_eb .Client {return &_eb .Client {Timeout :5*_b .Second }};
|
2024-04-30 12:24:05 +00:00
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
// NewTimestampRequest returns a new timestamp request based
|
|
|
|
// on the specified options.
|
|
|
|
func NewTimestampRequest (body _de .Reader ,opts *_cf .RequestOptions )(*_cf .Request ,error ){if opts ==nil {opts =&_cf .RequestOptions {};};if opts .Hash ==0{opts .Hash =_ec .SHA256 ;};if !opts .Hash .Available (){return nil ,_ee .ErrUnsupportedAlgorithm ;
|
|
|
|
};_ea :=opts .Hash .New ();if _ ,_fea :=_de .Copy (_ea ,body );_fea !=nil {return nil ,_fea ;};return &_cf .Request {HashAlgorithm :opts .Hash ,HashedMessage :_ea .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
|
|
|
|
};
|
2024-04-30 12:24:05 +00:00
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
// CRLClient represents a CRL (Certificate revocation list) client.
|
|
|
|
// It is used to request revocation data from CRL servers.
|
|
|
|
type CRLClient struct{
|
2024-05-29 17:04:37 +00:00
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
// 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 *_eb .Client ;};
|
2024-05-29 17:04:37 +00:00
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
// TimestampClient represents a RFC 3161 timestamp client.
|
|
|
|
// It is used to obtain signed tokens from timestamp authority servers.
|
|
|
|
type TimestampClient struct{
|
2024-05-29 17:04:37 +00:00
|
|
|
|
2024-06-27 16:15:49 +00:00
|
|
|
// HTTPClient is the HTTP client used to make timestamp requests.
|
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
|
|
|
HTTPClient *_eb .Client ;
|
|
|
|
|
|
|
|
// Callbacks.
|
|
|
|
BeforeHTTPRequest func (_ddc *_eb .Request )error ;};
|
2024-05-29 17:04:37 +00:00
|
|
|
|
|
|
|
// GetEncodedToken executes the timestamp request and returns the DER encoded
|
|
|
|
// timestamp token bytes.
|
2024-06-27 16:15:49 +00:00
|
|
|
func (_dge *TimestampClient )GetEncodedToken (serverURL string ,req *_cf .Request )([]byte ,error ){if serverURL ==""{return nil ,_c .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 ,_c .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");};_ga ,_aac :=req .Marshal ();if _aac !=nil {return nil ,_aac ;
|
|
|
|
};_cfc ,_aac :=_eb .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_d .NewBuffer (_ga ));if _aac !=nil {return nil ,_aac ;};_cfc .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 _dge .BeforeHTTPRequest !=nil {if _gdf :=_dge .BeforeHTTPRequest (_cfc );_gdf !=nil {return nil ,_gdf ;};};_eeg :=_dge .HTTPClient ;if _eeg ==nil {_eeg =_bc ();};_bb ,_aac :=_eeg .Do (_cfc );if _aac !=nil {return nil ,_aac ;};defer _bb .Body .Close ();
|
|
|
|
_cc ,_aac :=_bg .ReadAll (_bb .Body );if _aac !=nil {return nil ,_aac ;};if _bb .StatusCode !=_eb .StatusOK {return nil ,_c .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",_bb .StatusCode );
|
|
|
|
};var _ggg struct{Version _g .RawValue ;Content _g .RawValue ;};if _ ,_aac =_g .Unmarshal (_cc ,&_ggg );_aac !=nil {return nil ,_aac ;};return _ggg .Content .FullBytes ,nil ;};
|
|
|
|
|
|
|
|
// NewOCSPClient returns a new OCSP client.
|
|
|
|
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_bc (),Hash :_ec .SHA1 }};
|
|
|
|
|
|
|
|
// Get retrieves the certificate at the specified URL.
|
|
|
|
func (_fbc *CertClient )Get (url string )(*_ee .Certificate ,error ){if _fbc .HTTPClient ==nil {_fbc .HTTPClient =_bc ();};_a ,_dd :=_fbc .HTTPClient .Get (url );if _dd !=nil {return nil ,_dd ;};defer _a .Body .Close ();_bf ,_dd :=_bg .ReadAll (_a .Body );
|
|
|
|
if _dd !=nil {return nil ,_dd ;};if _deg ,_ :=_ece .Decode (_bf );_deg !=nil {_bf =_deg .Bytes ;};_cg ,_dd :=_ee .ParseCertificate (_bf );if _dd !=nil {return nil ,_dd ;};return _cg ,nil ;};
|