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-01-08 22:34:27 +00:00
|
|
|
package sigutil ;import (_b "bytes";_bd "crypto";_c "crypto/x509";_f "encoding/asn1";_de "encoding/pem";_g "errors";_ga "fmt";_bb "github.com/unidoc/timestamp";_e "github.com/unidoc/unipdf/v3/common";_cd "golang.org/x/crypto/ocsp";_db "io";_dd "io/ioutil";
|
|
|
|
_bg "net/http";_a "time";);
|
2022-12-15 21:59:56 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// GetEncodedToken executes the timestamp request and returns the DER encoded
|
|
|
|
// timestamp token bytes.
|
|
|
|
func (_ggc *TimestampClient )GetEncodedToken (serverURL string ,req *_bb .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");};_cf ,_ag :=req .Marshal ();if _ag !=nil {return nil ,_ag ;
|
|
|
|
};_gfa ,_ag :=_bg .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_b .NewBuffer (_cf ));if _ag !=nil {return nil ,_ag ;};_gfa .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 _ggc .BeforeHTTPRequest !=nil {if _ggcb :=_ggc .BeforeHTTPRequest (_gfa );_ggcb !=nil {return nil ,_ggcb ;};};_ab :=_ggc .HTTPClient ;if _ab ==nil {_ab =_abc ();};_ef ,_ag :=_ab .Do (_gfa );if _ag !=nil {return nil ,_ag ;};defer _ef .Body .Close ();
|
|
|
|
_cdg ,_ag :=_dd .ReadAll (_ef .Body );if _ag !=nil {return nil ,_ag ;};if _ef .StatusCode !=_bg .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",_ef .StatusCode );
|
|
|
|
};var _fee struct{Version _f .RawValue ;Content _f .RawValue ;};if _ ,_ag =_f .Unmarshal (_cdg ,&_fee );_ag !=nil {return nil ,_ag ;};return _fee .Content .FullBytes ,nil ;};
|
2022-12-15 21:59:56 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// NewCertClient returns a new certificate client.
|
|
|
|
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_abc ()}};
|
2022-12-15 21:59:56 +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.
|
2023-01-08 22:34:27 +00:00
|
|
|
func (_ge *CRLClient )MakeRequest (serverURL string ,cert *_c .Certificate )([]byte ,error ){if _ge .HTTPClient ==nil {_ge .HTTPClient =_abc ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_g .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];};_gda ,_ca :=_ge .HTTPClient .Get (serverURL );if _ca !=nil {return nil ,_ca ;};defer _gda .Body .Close ();_ee ,_ca :=_dd .ReadAll (_gda .Body );if _ca !=nil {return nil ,_ca ;};if _ad ,_ :=_de .Decode (_ee );
|
|
|
|
_ad !=nil {_ee =_ad .Bytes ;};return _ee ,nil ;};
|
2022-09-10 15:35:04 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// TimestampClient represents a RFC 3161 timestamp client.
|
|
|
|
// It is used to obtain signed tokens from timestamp authority servers.
|
|
|
|
type TimestampClient struct{
|
2022-09-10 15:35:04 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// HTTPClient is the HTTP client used to make timestamp requests.
|
2022-09-10 15:35:04 +00:00
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2023-01-08 22:34:27 +00:00
|
|
|
HTTPClient *_bg .Client ;
|
2022-10-27 19:04:58 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// Callbacks.
|
|
|
|
BeforeHTTPRequest func (_eg *_bg .Request )error ;};
|
2022-12-15 21:59:56 +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.
|
2023-01-08 22:34:27 +00:00
|
|
|
HTTPClient *_bg .Client ;};
|
2021-10-22 10:53:20 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// NewOCSPClient returns a new OCSP client.
|
|
|
|
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_abc (),Hash :_bd .SHA1 }};
|
2022-03-13 12:41:53 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// GetIssuer retrieves the issuer of the provided certificate.
|
|
|
|
func (_ec *CertClient )GetIssuer (cert *_c .Certificate )(*_c .Certificate ,error ){for _ ,_af :=range cert .IssuingCertificateURL {_bde ,_gfg :=_ec .Get (_af );if _gfg !=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 ,_gfg );
|
|
|
|
continue ;};return _bde ,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");};
|
2022-09-10 15:35:04 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// IsCA returns true if the provided certificate appears to be a CA certificate.
|
|
|
|
func (_bf *CertClient )IsCA (cert *_c .Certificate )bool {return cert .IsCA &&_b .Equal (cert .RawIssuer ,cert .RawSubject );};func _abc ()*_bg .Client {return &_bg .Client {Timeout :5*_a .Second }};
|
2022-10-27 19:04:58 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// 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 (_geg *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_c .Certificate )(*_cd .Response ,[]byte ,error ){if _geg .HTTPClient ==nil {_geg .HTTPClient =_abc ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_g .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];};_bdeb ,_gg :=_cd .CreateRequest (cert ,issuer ,&_cd .RequestOptions {Hash :_geg .Hash });if _gg !=nil {return nil ,nil ,_gg ;};_gba ,_gg :=_geg .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",_b .NewReader (_bdeb ));
|
|
|
|
if _gg !=nil {return nil ,nil ,_gg ;};defer _gba .Body .Close ();_da ,_gg :=_dd .ReadAll (_gba .Body );if _gg !=nil {return nil ,nil ,_gg ;};if _dec ,_ :=_de .Decode (_da );_dec !=nil {_da =_dec .Bytes ;};_dg ,_gg :=_cd .ParseResponseForCert (_da ,cert ,issuer );
|
|
|
|
if _gg !=nil {return nil ,nil ,_gg ;};return _dg ,_da ,nil ;};
|
2022-12-15 21:59:56 +00:00
|
|
|
|
2023-01-08 22:34:27 +00:00
|
|
|
// CRLClient represents a CRL (Certificate revocation list) client.
|
|
|
|
// It is used to request revocation data from CRL servers.
|
|
|
|
type CRLClient struct{
|
2022-12-15 21:59:56 +00:00
|
|
|
|
2023-01-08 22:34:27 +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 *_bg .Client ;};
|
|
|
|
|
|
|
|
// NewCRLClient returns a new CRL client.
|
|
|
|
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_abc ()}};
|
|
|
|
|
|
|
|
// Get retrieves the certificate at the specified URL.
|
|
|
|
func (_bbc *CertClient )Get (url string )(*_c .Certificate ,error ){if _bbc .HTTPClient ==nil {_bbc .HTTPClient =_abc ();};_ff ,_gb :=_bbc .HTTPClient .Get (url );if _gb !=nil {return nil ,_gb ;};defer _ff .Body .Close ();_fb ,_gb :=_dd .ReadAll (_ff .Body );
|
|
|
|
if _gb !=nil {return nil ,_gb ;};if _gd ,_ :=_de .Decode (_fb );_gd !=nil {_fb =_gd .Bytes ;};_gac ,_gb :=_c .ParseCertificate (_fb );if _gb !=nil {return nil ,_gb ;};return _gac ,nil ;};
|
|
|
|
|
|
|
|
// NewTimestampRequest returns a new timestamp request based
|
|
|
|
// on the specified options.
|
|
|
|
func NewTimestampRequest (body _db .Reader ,opts *_bb .RequestOptions )(*_bb .Request ,error ){if opts ==nil {opts =&_bb .RequestOptions {};};if opts .Hash ==0{opts .Hash =_bd .SHA256 ;};if !opts .Hash .Available (){return nil ,_c .ErrUnsupportedAlgorithm ;
|
|
|
|
};_ac :=opts .Hash .New ();if _ ,_ce :=_db .Copy (_ac ,body );_ce !=nil {return nil ,_ce ;};return &_bb .Request {HashAlgorithm :opts .Hash ,HashedMessage :_ac .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
|
|
|
|
};
|
2022-04-27 00:10:33 +00:00
|
|
|
|
2022-06-27 19:58:38 +00:00
|
|
|
// OCSPClient represents a OCSP (Online Certificate Status Protocol) client.
|
|
|
|
// It is used to request revocation data from OCSP servers.
|
|
|
|
type OCSPClient struct{
|
2022-04-27 00:10:33 +00:00
|
|
|
|
2022-06-27 19:58:38 +00:00
|
|
|
// HTTPClient is the HTTP client used to make OCSP requests.
|
2022-06-06 22:48:24 +00:00
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2023-01-08 22:34:27 +00:00
|
|
|
HTTPClient *_bg .Client ;
|
2022-06-27 19:58:38 +00:00
|
|
|
|
|
|
|
// Hash is the hash function used when constructing the OCSP
|
|
|
|
// requests. If zero, SHA-1 will be used.
|
2023-01-08 22:34:27 +00:00
|
|
|
Hash _bd .Hash ;};
|
|
|
|
|
|
|
|
// NewTimestampClient returns a new timestamp client.
|
|
|
|
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_abc ()}};
|