unipdf/model/sigutil/sigutil.go
2024-04-30 12:24:05 +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 (_eg "bytes";_c "crypto";_ae "crypto/x509";_f "encoding/asn1";_gc "encoding/pem";_cd "errors";_ga "fmt";_eb "github.com/unidoc/timestamp";_bg "github.com/unidoc/unipdf/v3/common";_cg "golang.org/x/crypto/ocsp";_g "io";_b "io/ioutil";
_a "net/http";_d "time";);
// IsCA returns true if the provided certificate appears to be a CA certificate.
func (_ac *CertClient )IsCA (cert *_ae .Certificate )bool {return cert .IsCA &&_eg .Equal (cert .RawIssuer ,cert .RawSubject );};
// NewOCSPClient returns a new OCSP client.
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_baf (),Hash :_c .SHA1 }};
// NewCRLClient returns a new CRL client.
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_baf ()}};
// GetIssuer retrieves the issuer of the provided certificate.
func (_gf *CertClient )GetIssuer (cert *_ae .Certificate )(*_ae .Certificate ,error ){for _ ,_ebb :=range cert .IssuingCertificateURL {_gce ,_db :=_gf .Get (_ebb );if _db !=nil {_bg .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 _gce ,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");};
// NewTimestampRequest returns a new timestamp request based
// on the specified options.
func NewTimestampRequest (body _g .Reader ,opts *_eb .RequestOptions )(*_eb .Request ,error ){if opts ==nil {opts =&_eb .RequestOptions {};};if opts .Hash ==0{opts .Hash =_c .SHA256 ;};if !opts .Hash .Available (){return nil ,_ae .ErrUnsupportedAlgorithm ;
};_age :=opts .Hash .New ();if _ ,_fe :=_g .Copy (_age ,body );_fe !=nil {return nil ,_fe ;};return &_eb .Request {HashAlgorithm :opts .Hash ,HashedMessage :_age .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
};
// NewTimestampClient returns a new timestamp client.
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_baf ()}};
// 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 (_fa *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_ae .Certificate )(*_cg .Response ,[]byte ,error ){if _fa .HTTPClient ==nil {_fa .HTTPClient =_baf ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_cd .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];};_ed ,_bd :=_cg .CreateRequest (cert ,issuer ,&_cg .RequestOptions {Hash :_fa .Hash });if _bd !=nil {return nil ,nil ,_bd ;};_gcg ,_bd :=_fa .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",_eg .NewReader (_ed ));
if _bd !=nil {return nil ,nil ,_bd ;};defer _gcg .Body .Close ();_ffa ,_bd :=_b .ReadAll (_gcg .Body );if _bd !=nil {return nil ,nil ,_bd ;};if _bdc ,_ :=_gc .Decode (_ffa );_bdc !=nil {_ffa =_bdc .Bytes ;};_gdf ,_bd :=_cg .ParseResponseForCert (_ffa ,cert ,issuer );
if _bd !=nil {return nil ,nil ,_bd ;};return _gdf ,_ffa ,nil ;};
// 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 (_dc *CRLClient )MakeRequest (serverURL string ,cert *_ae .Certificate )([]byte ,error ){if _dc .HTTPClient ==nil {_dc .HTTPClient =_baf ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_cd .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];};_gd ,_fd :=_dc .HTTPClient .Get (serverURL );if _fd !=nil {return nil ,_fd ;};defer _gd .Body .Close ();_dg ,_fd :=_b .ReadAll (_gd .Body );if _fd !=nil {return nil ,_fd ;};if _fg ,_ :=_gc .Decode (_dg );_fg !=nil {_dg =_fg .Bytes ;
};return _dg ,nil ;};
// 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 *_a .Client ;
// Callbacks.
BeforeHTTPRequest func (_fb *_a .Request )error ;};
// 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 *_a .Client ;};func _baf ()*_a .Client {return &_a .Client {Timeout :5*_d .Second }};
// GetEncodedToken executes the timestamp request and returns the DER encoded
// timestamp token bytes.
func (_aa *TimestampClient )GetEncodedToken (serverURL string ,req *_eb .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");};_dd ,_ca :=req .Marshal ();if _ca !=nil {return nil ,_ca ;
};_aea ,_ca :=_a .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_eg .NewBuffer (_dd ));if _ca !=nil {return nil ,_ca ;};_aea .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 _aa .BeforeHTTPRequest !=nil {if _gcc :=_aa .BeforeHTTPRequest (_aea );_gcc !=nil {return nil ,_gcc ;};};_gg :=_aa .HTTPClient ;if _gg ==nil {_gg =_baf ();};_bc ,_ca :=_gg .Do (_aea );if _ca !=nil {return nil ,_ca ;};defer _bc .Body .Close ();_be ,_ca :=_b .ReadAll (_bc .Body );
if _ca !=nil {return nil ,_ca ;};if _bc .StatusCode !=_a .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",_bc .StatusCode );
};var _fbf struct{Version _f .RawValue ;Content _f .RawValue ;};if _ ,_ca =_f .Unmarshal (_be ,&_fbf );_ca !=nil {return nil ,_ca ;};return _fbf .Content .FullBytes ,nil ;};
// Get retrieves the certificate at the specified URL.
func (_ge *CertClient )Get (url string )(*_ae .Certificate ,error ){if _ge .HTTPClient ==nil {_ge .HTTPClient =_baf ();};_bgf ,_ag :=_ge .HTTPClient .Get (url );if _ag !=nil {return nil ,_ag ;};defer _bgf .Body .Close ();_gad ,_ag :=_b .ReadAll (_bgf .Body );
if _ag !=nil {return nil ,_ag ;};if _ad ,_ :=_gc .Decode (_gad );_ad !=nil {_gad =_ad .Bytes ;};_cf ,_ag :=_ae .ParseCertificate (_gad );if _ag !=nil {return nil ,_ag ;};return _cf ,nil ;};
// NewCertClient returns a new certificate client.
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_baf ()}};
// 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 *_a .Client ;};
// 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 *_a .Client ;
// Hash is the hash function used when constructing the OCSP
// requests. If zero, SHA-1 will be used.
Hash _c .Hash ;};