unipdf/model/sigutil/sigutil.go
2023-09-07 17:40:17 +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";_f "crypto";_bf "crypto/x509";_ce "encoding/asn1";_cca "encoding/pem";_ca "errors";_cc "fmt";_ae "github.com/unidoc/timestamp";_ed "github.com/unidoc/unipdf/v3/common";_cea "golang.org/x/crypto/ocsp";_b "io";_gc "io/ioutil";
_e "net/http";_g "time";);func _bea ()*_e .Client {return &_e .Client {Timeout :5*_g .Second }};
// GetIssuer retrieves the issuer of the provided certificate.
func (_ba *CertClient )GetIssuer (cert *_bf .Certificate )(*_bf .Certificate ,error ){for _ ,_beb :=range cert .IssuingCertificateURL {_eb ,_ea :=_ba .Get (_beb );if _ea !=nil {_ed .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 ,_ea );
continue ;};return _eb ,nil ;};return nil ,_cc .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");};
// Get retrieves the certificate at the specified URL.
func (_bg *CertClient )Get (url string )(*_bf .Certificate ,error ){if _bg .HTTPClient ==nil {_bg .HTTPClient =_bea ();};_be ,_ec :=_bg .HTTPClient .Get (url );if _ec !=nil {return nil ,_ec ;};defer _be .Body .Close ();_cd ,_ec :=_gc .ReadAll (_be .Body );
if _ec !=nil {return nil ,_ec ;};if _cad ,_ :=_cca .Decode (_cd );_cad !=nil {_cd =_cad .Bytes ;};_ga ,_ec :=_bf .ParseCertificate (_cd );if _ec !=nil {return nil ,_ec ;};return _ga ,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 *_e .Client ;};
// 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 *_e .Client ;};
// NewTimestampRequest returns a new timestamp request based
// on the specified options.
func NewTimestampRequest (body _b .Reader ,opts *_ae .RequestOptions )(*_ae .Request ,error ){if opts ==nil {opts =&_ae .RequestOptions {};};if opts .Hash ==0{opts .Hash =_f .SHA256 ;};if !opts .Hash .Available (){return nil ,_bf .ErrUnsupportedAlgorithm ;
};_gd :=opts .Hash .New ();if _ ,_af :=_b .Copy (_gd ,body );_af !=nil {return nil ,_af ;};return &_ae .Request {HashAlgorithm :opts .Hash ,HashedMessage :_gd .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },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 (_ac *CRLClient )MakeRequest (serverURL string ,cert *_bf .Certificate )([]byte ,error ){if _ac .HTTPClient ==nil {_ac .HTTPClient =_bea ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_ca .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 ,_cb :=_ac .HTTPClient .Get (serverURL );if _cb !=nil {return nil ,_cb ;};defer _fa .Body .Close ();_ef ,_cb :=_gc .ReadAll (_fa .Body );if _cb !=nil {return nil ,_cb ;};if _bd ,_ :=_cca .Decode (_ef );
_bd !=nil {_ef =_bd .Bytes ;};return _ef ,nil ;};
// NewTimestampClient returns a new timestamp client.
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_bea ()}};
// 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 *_e .Client ;
// Hash is the hash function used when constructing the OCSP
// requests. If zero, SHA-1 will be used.
Hash _f .Hash ;};
// IsCA returns true if the provided certificate appears to be a CA certificate.
func (_cf *CertClient )IsCA (cert *_bf .Certificate )bool {return cert .IsCA &&_c .Equal (cert .RawIssuer ,cert .RawSubject );};
// 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 (_fc *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_bf .Certificate )(*_cea .Response ,[]byte ,error ){if _fc .HTTPClient ==nil {_fc .HTTPClient =_bea ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_ca .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];};_gg ,_ggd :=_cea .CreateRequest (cert ,issuer ,&_cea .RequestOptions {Hash :_fc .Hash });if _ggd !=nil {return nil ,nil ,_ggd ;};_eaa ,_ggd :=_fc .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 (_gg ));
if _ggd !=nil {return nil ,nil ,_ggd ;};defer _eaa .Body .Close ();_ag ,_ggd :=_gc .ReadAll (_eaa .Body );if _ggd !=nil {return nil ,nil ,_ggd ;};if _gb ,_ :=_cca .Decode (_ag );_gb !=nil {_ag =_gb .Bytes ;};_fg ,_ggd :=_cea .ParseResponseForCert (_ag ,cert ,issuer );
if _ggd !=nil {return nil ,nil ,_ggd ;};return _fg ,_ag ,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 *_e .Client ;
// Callbacks.
BeforeHTTPRequest func (_ggg *_e .Request )error ;};
// NewCertClient returns a new certificate client.
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_bea ()}};
// NewOCSPClient returns a new OCSP client.
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_bea (),Hash :_f .SHA1 }};
// GetEncodedToken executes the timestamp request and returns the DER encoded
// timestamp token bytes.
func (_da *TimestampClient )GetEncodedToken (serverURL string ,req *_ae .Request )([]byte ,error ){if serverURL ==""{return nil ,_cc .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 ,_cc .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");};_ecc ,_bee :=req .Marshal ();if _bee !=nil {return nil ,_bee ;
};_ebb ,_bee :=_e .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_c .NewBuffer (_ecc ));if _bee !=nil {return nil ,_bee ;};_ebb .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 _da .BeforeHTTPRequest !=nil {if _eg :=_da .BeforeHTTPRequest (_ebb );_eg !=nil {return nil ,_eg ;};};_bde :=_da .HTTPClient ;if _bde ==nil {_bde =_bea ();};_gbf ,_bee :=_bde .Do (_ebb );if _bee !=nil {return nil ,_bee ;};defer _gbf .Body .Close ();
_edc ,_bee :=_gc .ReadAll (_gbf .Body );if _bee !=nil {return nil ,_bee ;};if _gbf .StatusCode !=_e .StatusOK {return nil ,_cc .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",_gbf .StatusCode );
};var _gda struct{Version _ce .RawValue ;Content _ce .RawValue ;};if _ ,_bee =_ce .Unmarshal (_edc ,&_gda );_bee !=nil {return nil ,_bee ;};return _gda .Content .FullBytes ,nil ;};
// NewCRLClient returns a new CRL client.
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_bea ()}};