unipdf/model/sigutil/sigutil.go
2024-03-27 22:34:33 +00:00

104 lines
8.8 KiB
Go

//
// 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/
package sigutil ;import (_c "bytes";_a "crypto";_ae "crypto/x509";_gc "encoding/asn1";_bg "encoding/pem";_b "errors";_ga "fmt";_ge "github.com/unidoc/timestamp";_e "github.com/unidoc/unipdf/v3/common";_da "golang.org/x/crypto/ocsp";_g "io";_ac "io/ioutil";
_cg "net/http";_ca "time";);
// 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 (_ff *CRLClient )MakeRequest (serverURL string ,cert *_ae .Certificate )([]byte ,error ){if _ff .HTTPClient ==nil {_ff .HTTPClient =_gd ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_b .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];};_fa ,_aag :=_ff .HTTPClient .Get (serverURL );if _aag !=nil {return nil ,_aag ;};defer _fa .Body .Close ();_eb ,_aag :=_ac .ReadAll (_fa .Body );if _aag !=nil {return nil ,_aag ;};if _fb ,_ :=_bg .Decode (_eb );
_fb !=nil {_eb =_fb .Bytes ;};return _eb ,nil ;};
// NewCRLClient returns a new CRL client.
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_gd ()}};
// Get retrieves the certificate at the specified URL.
func (_f *CertClient )Get (url string )(*_ae .Certificate ,error ){if _f .HTTPClient ==nil {_f .HTTPClient =_gd ();};_cgb ,_ag :=_f .HTTPClient .Get (url );if _ag !=nil {return nil ,_ag ;};defer _cgb .Body .Close ();_gca ,_ag :=_ac .ReadAll (_cgb .Body );
if _ag !=nil {return nil ,_ag ;};if _ba ,_ :=_bg .Decode (_gca );_ba !=nil {_gca =_ba .Bytes ;};_cgg ,_ag :=_ae .ParseCertificate (_gca );if _ag !=nil {return nil ,_ag ;};return _cgg ,nil ;};
// 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 *_cg .Client ;
// Hash is the hash function used when constructing the OCSP
// requests. If zero, SHA-1 will be used.
Hash _a .Hash ;};
// 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 *_cg .Client ;};
// TimestampClient represents a RFC 3161 timestamp client.
// It is used to obtain signed tokens from timestamp authority servers.
type TimestampClient struct{
// 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 *_cg .Client ;
// Callbacks.
BeforeHTTPRequest func (_aca *_cg .Request )error ;};
// IsCA returns true if the provided certificate appears to be a CA certificate.
func (_agf *CertClient )IsCA (cert *_ae .Certificate )bool {return cert .IsCA &&_c .Equal (cert .RawIssuer ,cert .RawSubject );};
// GetIssuer retrieves the issuer of the provided certificate.
func (_ef *CertClient )GetIssuer (cert *_ae .Certificate )(*_ae .Certificate ,error ){for _ ,_fd :=range cert .IssuingCertificateURL {_bd ,_cd :=_ef .Get (_fd );if _cd !=nil {_e .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 ,_cd );
continue ;};return _bd ,nil ;};return nil ,_ga .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");};
// NewCertClient returns a new certificate client.
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_gd ()}};func _gd ()*_cg .Client {return &_cg .Client {Timeout :5*_ca .Second }};
// 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.
HTTPClient *_cg .Client ;};
// NewTimestampClient returns a new timestamp client.
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_gd ()}};
// 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 (_ea *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_ae .Certificate )(*_da .Response ,[]byte ,error ){if _ea .HTTPClient ==nil {_ea .HTTPClient =_gd ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_b .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];};_ad ,_dc :=_da .CreateRequest (cert ,issuer ,&_da .RequestOptions {Hash :_ea .Hash });if _dc !=nil {return nil ,nil ,_dc ;};_gg ,_dc :=_ea .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",_c .NewReader (_ad ));
if _dc !=nil {return nil ,nil ,_dc ;};defer _gg .Body .Close ();_ec ,_dc :=_ac .ReadAll (_gg .Body );if _dc !=nil {return nil ,nil ,_dc ;};if _fg ,_ :=_bg .Decode (_ec );_fg !=nil {_ec =_fg .Bytes ;};_gf ,_dc :=_da .ParseResponseForCert (_ec ,cert ,issuer );
if _dc !=nil {return nil ,nil ,_dc ;};return _gf ,_ec ,nil ;};
// NewTimestampRequest returns a new timestamp request based
// on the specified options.
func NewTimestampRequest (body _g .Reader ,opts *_ge .RequestOptions )(*_ge .Request ,error ){if opts ==nil {opts =&_ge .RequestOptions {};};if opts .Hash ==0{opts .Hash =_a .SHA256 ;};if !opts .Hash .Available (){return nil ,_ae .ErrUnsupportedAlgorithm ;
};_gcab :=opts .Hash .New ();if _ ,_gae :=_g .Copy (_gcab ,body );_gae !=nil {return nil ,_gae ;};return &_ge .Request {HashAlgorithm :opts .Hash ,HashedMessage :_gcab .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
};
// NewOCSPClient returns a new OCSP client.
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_gd (),Hash :_a .SHA1 }};
// GetEncodedToken executes the timestamp request and returns the DER encoded
// timestamp token bytes.
func (_dcf *TimestampClient )GetEncodedToken (serverURL string ,req *_ge .Request )([]byte ,error ){if serverURL ==""{return nil ,_ga .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 ,_ga .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");};_fgd ,_bb :=req .Marshal ();if _bb !=nil {return nil ,_bb ;
};_gga ,_bb :=_cg .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_c .NewBuffer (_fgd ));if _bb !=nil {return nil ,_bb ;};_gga .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 _dcf .BeforeHTTPRequest !=nil {if _cb :=_dcf .BeforeHTTPRequest (_gga );_cb !=nil {return nil ,_cb ;};};_ce :=_dcf .HTTPClient ;if _ce ==nil {_ce =_gd ();};_dec ,_bb :=_ce .Do (_gga );if _bb !=nil {return nil ,_bb ;};defer _dec .Body .Close ();_cag ,_bb :=_ac .ReadAll (_dec .Body );
if _bb !=nil {return nil ,_bb ;};if _dec .StatusCode !=_cg .StatusOK {return nil ,_ga .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",_dec .StatusCode );
};var _cec struct{Version _gc .RawValue ;Content _gc .RawValue ;};if _ ,_bb =_gc .Unmarshal (_cag ,&_cec );_bb !=nil {return nil ,_bb ;};return _cec .Content .FullBytes ,nil ;};