unipdf/model/sigutil/sigutil.go

104 lines
8.8 KiB
Go
Raw Normal View History

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/
2024-05-29 17:04:37 +00:00
package sigutil ;import (_ee "bytes";_d "crypto";_bg "crypto/x509";_b "encoding/asn1";_eb "encoding/pem";_c "errors";_df "fmt";_da "github.com/unidoc/timestamp";_ef "github.com/unidoc/unipdf/v3/common";_eeg "golang.org/x/crypto/ocsp";_ea "io";_g "io/ioutil";
_af "net/http";_a "time";);
2024-04-16 11:40:43 +00:00
2024-05-29 17:04:37 +00:00
// CRLClient represents a CRL (Certificate revocation list) client.
// It is used to request revocation data from CRL servers.
type CRLClient struct{
2023-08-03 17:30:04 +00:00
2024-05-29 17:04:37 +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 *_af .Client ;};
2023-08-03 17:30:04 +00:00
2024-04-16 11:40:43 +00:00
// NewTimestampRequest returns a new timestamp request based
// on the specified options.
2024-05-29 17:04:37 +00:00
func NewTimestampRequest (body _ea .Reader ,opts *_da .RequestOptions )(*_da .Request ,error ){if opts ==nil {opts =&_da .RequestOptions {};};if opts .Hash ==0{opts .Hash =_d .SHA256 ;};if !opts .Hash .Available (){return nil ,_bg .ErrUnsupportedAlgorithm ;
};_bfe :=opts .Hash .New ();if _ ,_dc :=_ea .Copy (_bfe ,body );_dc !=nil {return nil ,_dc ;};return &_da .Request {HashAlgorithm :opts .Hash ,HashedMessage :_bfe .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
2024-04-16 11:40:43 +00:00
};
2024-05-29 17:04:37 +00:00
// OCSPClient represents a OCSP (Online Certificate Status Protocol) client.
// It is used to request revocation data from OCSP servers.
type OCSPClient struct{
2024-04-16 11:40:43 +00:00
2024-05-29 17:04:37 +00:00
// 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 *_af .Client ;
2024-04-30 12:24:05 +00:00
2024-05-29 17:04:37 +00:00
// Hash is the hash function used when constructing the OCSP
// requests. If zero, SHA-1 will be used.
Hash _d .Hash ;};
2024-02-11 21:29:32 +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.
2024-05-29 17:04:37 +00:00
HTTPClient *_af .Client ;
2024-02-11 21:29:32 +00:00
// Callbacks.
2024-05-29 17:04:37 +00:00
BeforeHTTPRequest func (_gbd *_af .Request )error ;};
2024-03-27 22:34:33 +00:00
2024-05-29 17:04:37 +00:00
// NewTimestampClient returns a new timestamp client.
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_dfb ()}};
2024-03-27 22:34:33 +00:00
2024-04-16 11:40:43 +00:00
// Get retrieves the certificate at the specified URL.
2024-05-29 17:04:37 +00:00
func (_ec *CertClient )Get (url string )(*_bg .Certificate ,error ){if _ec .HTTPClient ==nil {_ec .HTTPClient =_dfb ();};_ga ,_eaf :=_ec .HTTPClient .Get (url );if _eaf !=nil {return nil ,_eaf ;};defer _ga .Body .Close ();_ge ,_eaf :=_g .ReadAll (_ga .Body );
if _eaf !=nil {return nil ,_eaf ;};if _dag ,_ :=_eb .Decode (_ge );_dag !=nil {_ge =_dag .Bytes ;};_db ,_eaf :=_bg .ParseCertificate (_ge );if _eaf !=nil {return nil ,_eaf ;};return _db ,nil ;};
2024-03-27 22:34:33 +00:00
2024-05-29 17:04:37 +00:00
// IsCA returns true if the provided certificate appears to be a CA certificate.
func (_fcf *CertClient )IsCA (cert *_bg .Certificate )bool {return cert .IsCA &&_ee .Equal (cert .RawIssuer ,cert .RawSubject );};
2024-04-30 12:24:05 +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.
2024-05-29 17:04:37 +00:00
HTTPClient *_af .Client ;};
2024-04-30 12:24:05 +00:00
2024-05-29 17:04:37 +00:00
// NewCertClient returns a new certificate client.
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_dfb ()}};
2024-04-30 12:24:05 +00:00
2024-05-29 17:04:37 +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 (_bf *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_bg .Certificate )(*_eeg .Response ,[]byte ,error ){if _bf .HTTPClient ==nil {_bf .HTTPClient =_dfb ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_c .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];};_ce ,_fbe :=_eeg .CreateRequest (cert ,issuer ,&_eeg .RequestOptions {Hash :_bf .Hash });if _fbe !=nil {return nil ,nil ,_fbe ;};_fe ,_fbe :=_bf .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",_ee .NewReader (_ce ));
if _fbe !=nil {return nil ,nil ,_fbe ;};defer _fe .Body .Close ();_ebe ,_fbe :=_g .ReadAll (_fe .Body );if _fbe !=nil {return nil ,nil ,_fbe ;};if _gb ,_ :=_eb .Decode (_ebe );_gb !=nil {_ebe =_gb .Bytes ;};_fbec ,_fbe :=_eeg .ParseResponseForCert (_ebe ,cert ,issuer );
if _fbe !=nil {return nil ,nil ,_fbe ;};return _fbec ,_ebe ,nil ;};
2024-04-30 12:24:05 +00:00
2024-05-29 17:04:37 +00:00
// NewOCSPClient returns a new OCSP client.
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_dfb (),Hash :_d .SHA1 }};func _dfb ()*_af .Client {return &_af .Client {Timeout :5*_a .Second }};
// NewCRLClient returns a new CRL client.
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_dfb ()}};
// 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 (_eag *CRLClient )MakeRequest (serverURL string ,cert *_bg .Certificate )([]byte ,error ){if _eag .HTTPClient ==nil {_eag .HTTPClient =_dfb ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_c .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];};_fb ,_gf :=_eag .HTTPClient .Get (serverURL );if _gf !=nil {return nil ,_gf ;};defer _fb .Body .Close ();_bd ,_gf :=_g .ReadAll (_fb .Body );if _gf !=nil {return nil ,_gf ;};if _geg ,_ :=_eb .Decode (_bd );
_geg !=nil {_bd =_geg .Bytes ;};return _bd ,nil ;};
// GetIssuer retrieves the issuer of the provided certificate.
func (_ed *CertClient )GetIssuer (cert *_bg .Certificate )(*_bg .Certificate ,error ){for _ ,_eg :=range cert .IssuingCertificateURL {_fc ,_be :=_ed .Get (_eg );if _be !=nil {_ef .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 ,_be );
continue ;};return _fc ,nil ;};return nil ,_df .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");};
// GetEncodedToken executes the timestamp request and returns the DER encoded
// timestamp token bytes.
func (_gd *TimestampClient )GetEncodedToken (serverURL string ,req *_da .Request )([]byte ,error ){if serverURL ==""{return nil ,_df .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 ,_df .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");};_aa ,_eeb :=req .Marshal ();if _eeb !=nil {return nil ,_eeb ;
};_ba ,_eeb :=_af .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_ee .NewBuffer (_aa ));if _eeb !=nil {return nil ,_eeb ;};_ba .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 _gd .BeforeHTTPRequest !=nil {if _eac :=_gd .BeforeHTTPRequest (_ba );_eac !=nil {return nil ,_eac ;};};_eagg :=_gd .HTTPClient ;if _eagg ==nil {_eagg =_dfb ();};_bfed ,_eeb :=_eagg .Do (_ba );if _eeb !=nil {return nil ,_eeb ;};defer _bfed .Body .Close ();
_cc ,_eeb :=_g .ReadAll (_bfed .Body );if _eeb !=nil {return nil ,_eeb ;};if _bfed .StatusCode !=_af .StatusOK {return nil ,_df .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",_bfed .StatusCode );
};var _fbee struct{Version _b .RawValue ;Content _b .RawValue ;};if _ ,_eeb =_b .Unmarshal (_cc ,&_fbee );_eeb !=nil {return nil ,_eeb ;};return _fbee .Content .FullBytes ,nil ;};