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/
|
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
package sigutil ;import (_f "bytes";_da "crypto";_fb "crypto/x509";_bc "encoding/asn1";_g "encoding/pem";_db "errors";_fg "fmt";_dbb "github.com/unidoc/timestamp";_fbd "github.com/unidoc/unipdf/v3/common";_fgd "golang.org/x/crypto/ocsp";_d "io";_ef "io/ioutil";
|
|
|
|
_e "net/http";_b "time";);func _df ()*_e .Client {return &_e .Client {Timeout :5*_b .Second }};
|
2021-04-17 13:46:54 +00:00
|
|
|
|
2021-09-23 22:37:42 +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 (_fa *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_fb .Certificate )(*_fgd .Response ,[]byte ,error ){if _fa .HTTPClient ==nil {_fa .HTTPClient =_df ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_db .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];};_bb ,_bcd :=_fgd .CreateRequest (cert ,issuer ,&_fgd .RequestOptions {Hash :_fa .Hash });if _bcd !=nil {return nil ,nil ,_bcd ;};_afg ,_bcd :=_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",_f .NewReader (_bb ));
|
|
|
|
if _bcd !=nil {return nil ,nil ,_bcd ;};defer _afg .Body .Close ();_ge ,_bcd :=_ef .ReadAll (_afg .Body );if _bcd !=nil {return nil ,nil ,_bcd ;};if _bgg ,_ :=_g .Decode (_ge );_bgg !=nil {_ge =_bgg .Bytes ;};_ee ,_bcd :=_fgd .ParseResponseForCert (_ge ,cert ,issuer );
|
|
|
|
if _bcd !=nil {return nil ,nil ,_bcd ;};return _ee ,_ge ,nil ;};
|
2021-03-13 21:28:23 +00:00
|
|
|
|
2021-09-23 22:37:42 +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 (_gd *CRLClient )MakeRequest (serverURL string ,cert *_fb .Certificate )([]byte ,error ){if _gd .HTTPClient ==nil {_gd .HTTPClient =_df ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_db .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];};_dgc ,_ba :=_gd .HTTPClient .Get (serverURL );if _ba !=nil {return nil ,_ba ;};defer _dgc .Body .Close ();_cf ,_ba :=_ef .ReadAll (_dgc .Body );if _ba !=nil {return nil ,_ba ;};if _eg ,_ :=_g .Decode (_cf );
|
|
|
|
_eg !=nil {_cf =_eg .Bytes ;};return _cf ,nil ;};
|
2021-06-21 14:01:56 +00:00
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
// Get retrieves the certificate at the specified URL.
|
|
|
|
func (_de *CertClient )Get (url string )(*_fb .Certificate ,error ){if _de .HTTPClient ==nil {_de .HTTPClient =_df ();};_fd ,_ea :=_de .HTTPClient .Get (url );if _ea !=nil {return nil ,_ea ;};defer _fd .Body .Close ();_aa ,_ea :=_ef .ReadAll (_fd .Body );
|
|
|
|
if _ea !=nil {return nil ,_ea ;};if _af ,_ :=_g .Decode (_aa );_af !=nil {_aa =_af .Bytes ;};_c ,_ea :=_fb .ParseCertificate (_aa );if _ea !=nil {return nil ,_ea ;};return _c ,nil ;};
|
2021-07-30 00:21:16 +00:00
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
// NewCertClient returns a new certificate client.
|
|
|
|
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_df ()}};
|
2021-07-30 00:21:16 +00:00
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
// 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.
|
2021-07-30 00:21:16 +00:00
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2021-08-13 01:33:42 +00:00
|
|
|
HTTPClient *_e .Client ;};
|
2021-06-21 14:01:56 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// OCSPClient represents a OCSP (Online Certificate Status Protocol) client.
|
|
|
|
// It is used to request revocation data from OCSP servers.
|
|
|
|
type OCSPClient struct{
|
2021-06-21 14:01:56 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// HTTPClient is the HTTP client used to make OCSP requests.
|
2021-06-21 14:01:56 +00:00
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2021-08-13 01:33:42 +00:00
|
|
|
HTTPClient *_e .Client ;
|
|
|
|
|
|
|
|
// Hash is the hash function used when constructing the OCSP
|
|
|
|
// requests. If zero, SHA-1 will be used.
|
2021-09-23 22:37:42 +00:00
|
|
|
Hash _da .Hash ;};
|
2021-08-13 01:33:42 +00:00
|
|
|
|
|
|
|
// NewTimestampRequest returns a new timestamp request based
|
|
|
|
// on the specified options.
|
2021-09-23 22:37:42 +00:00
|
|
|
func NewTimestampRequest (body _d .Reader ,opts *_dbb .RequestOptions )(*_dbb .Request ,error ){if opts ==nil {opts =&_dbb .RequestOptions {};};if opts .Hash ==0{opts .Hash =_da .SHA256 ;};if !opts .Hash .Available (){return nil ,_fb .ErrUnsupportedAlgorithm ;
|
|
|
|
};_bd :=opts .Hash .New ();if _ ,_ga :=_d .Copy (_bd ,body );_ga !=nil {return nil ,_ga ;};return &_dbb .Request {HashAlgorithm :opts .Hash ,HashedMessage :_bd .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
|
2021-08-13 01:33:42 +00:00
|
|
|
};
|
2021-06-21 14:01:56 +00:00
|
|
|
|
2021-07-30 00:21:16 +00:00
|
|
|
// GetEncodedToken executes the timestamp request and returns the DER encoded
|
|
|
|
// timestamp token bytes.
|
2021-09-23 22:37:42 +00:00
|
|
|
func (_bae *TimestampClient )GetEncodedToken (serverURL string ,req *_dbb .Request )([]byte ,error ){if serverURL ==""{return nil ,_fg .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 ,_fg .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");};_gge ,_eeb :=req .Marshal ();if _eeb !=nil {return nil ,_eeb ;
|
|
|
|
};_cfe :=_bae .HTTPClient ;if _cfe ==nil {_cfe =_df ();};_eab ,_eeb :=_cfe .Post (serverURL ,"a\u0070\u0070\u006c\u0069\u0063\u0061t\u0069\u006f\u006e\u002f\u0074\u0069\u006d\u0065\u0073t\u0061\u006d\u0070-\u0071u\u0065\u0072\u0079",_f .NewBuffer (_gge ));
|
|
|
|
if _eeb !=nil {return nil ,_eeb ;};defer _eab .Body .Close ();_ec ,_eeb :=_ef .ReadAll (_eab .Body );if _eeb !=nil {return nil ,_eeb ;};if _eab .StatusCode !=_e .StatusOK {return nil ,_fg .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",_eab .StatusCode );
|
|
|
|
};var _efe struct{Version _bc .RawValue ;Content _bc .RawValue ;};if _ ,_eeb =_bc .Unmarshal (_ec ,&_efe );_eeb !=nil {return nil ,_eeb ;};return _efe .Content .FullBytes ,nil ;};
|
2021-07-30 00:21:16 +00:00
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
// NewTimestampClient returns a new timestamp client.
|
|
|
|
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_df ()}};
|
2021-03-23 23:12:52 +00:00
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
// IsCA returns true if the provided certificate appears to be a CA certificate.
|
|
|
|
func (_eag *CertClient )IsCA (cert *_fb .Certificate )bool {return cert .IsCA &&_f .Equal (cert .RawIssuer ,cert .RawSubject );};
|
2021-07-30 00:21:16 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// CertClient represents a X.509 certificate client. Its primary purpose
|
|
|
|
// is to download certificates.
|
|
|
|
type CertClient struct{
|
2021-03-23 23:12:52 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// HTTPClient is the HTTP client used to make certificate requests.
|
2021-04-06 22:35:37 +00:00
|
|
|
// By default, an HTTP client with a 5 second timeout per request is used.
|
2021-08-13 01:33:42 +00:00
|
|
|
HTTPClient *_e .Client ;};
|
2021-04-06 22:35:37 +00:00
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
// NewOCSPClient returns a new OCSP client.
|
|
|
|
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_df (),Hash :_da .SHA1 }};
|
2021-08-13 01:33:42 +00:00
|
|
|
|
2021-09-23 22:37:42 +00:00
|
|
|
// 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 ;};
|
|
|
|
|
|
|
|
// NewCRLClient returns a new CRL client.
|
|
|
|
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_df ()}};
|
|
|
|
|
|
|
|
// GetIssuer retrieves the issuer of the provided certificate.
|
|
|
|
func (_gc *CertClient )GetIssuer (cert *_fb .Certificate )(*_fb .Certificate ,error ){for _ ,_ca :=range cert .IssuingCertificateURL {_bcf ,_cc :=_gc .Get (_ca );if _cc !=nil {_fbd .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 ,_cc );
|
|
|
|
continue ;};return _bcf ,nil ;};return nil ,_fg .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");};
|