unipdf/model/sigutil/sigutil.go

105 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/
2023-12-17 13:54:01 +00:00
package sigutil ;import (_e "bytes";_bc "crypto";_ge "crypto/x509";_dc "encoding/asn1";_ed "encoding/pem";_ac "errors";_d "fmt";_gee "github.com/unidoc/timestamp";_f "github.com/unidoc/unipdf/v3/common";_dg "golang.org/x/crypto/ocsp";_b "io";_ae "io/ioutil";
_be "net/http";_a "time";);
2023-10-07 13:58:01 +00:00
2023-11-11 11:29:03 +00:00
// OCSPClient represents a OCSP (Online Certificate Status Protocol) client.
// It is used to request revocation data from OCSP servers.
type OCSPClient struct{
2023-10-07 13:58:01 +00:00
2023-11-11 11:29:03 +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.
2023-12-17 13:54:01 +00:00
HTTPClient *_be .Client ;
2023-10-07 13:58:01 +00:00
2023-11-11 11:29:03 +00:00
// Hash is the hash function used when constructing the OCSP
// requests. If zero, SHA-1 will be used.
2023-12-17 13:54:01 +00:00
Hash _bc .Hash ;};
// NewCRLClient returns a new CRL client.
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_ef ()}};
// NewOCSPClient returns a new OCSP client.
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_ef (),Hash :_bc .SHA1 }};
// GetIssuer retrieves the issuer of the provided certificate.
func (_bg *CertClient )GetIssuer (cert *_ge .Certificate )(*_ge .Certificate ,error ){for _ ,_gag :=range cert .IssuingCertificateURL {_ba ,_ea :=_bg .Get (_gag );if _ea !=nil {_f .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 _ba ,nil ;};return nil ,_d .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");};
2023-10-07 13:58:01 +00:00
2023-11-11 11:29:03 +00:00
// NewTimestampRequest returns a new timestamp request based
// on the specified options.
2023-12-17 13:54:01 +00:00
func NewTimestampRequest (body _b .Reader ,opts *_gee .RequestOptions )(*_gee .Request ,error ){if opts ==nil {opts =&_gee .RequestOptions {};};if opts .Hash ==0{opts .Hash =_bc .SHA256 ;};if !opts .Hash .Available (){return nil ,_ge .ErrUnsupportedAlgorithm ;
};_ag :=opts .Hash .New ();if _ ,_fd :=_b .Copy (_ag ,body );_fd !=nil {return nil ,_fd ;};return &_gee .Request {HashAlgorithm :opts .Hash ,HashedMessage :_ag .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
2023-11-11 11:29:03 +00:00
};
2023-09-07 17:40:17 +00:00
2023-12-17 13:54:01 +00:00
// Get retrieves the certificate at the specified URL.
func (_eb *CertClient )Get (url string )(*_ge .Certificate ,error ){if _eb .HTTPClient ==nil {_eb .HTTPClient =_ef ();};_ad ,_de :=_eb .HTTPClient .Get (url );if _de !=nil {return nil ,_de ;};defer _ad .Body .Close ();_fa ,_de :=_ae .ReadAll (_ad .Body );
if _de !=nil {return nil ,_de ;};if _ff ,_ :=_ed .Decode (_fa );_ff !=nil {_fa =_ff .Bytes ;};_ga ,_de :=_ge .ParseCertificate (_fa );if _de !=nil {return nil ,_de ;};return _ga ,nil ;};func _ef ()*_be .Client {return &_be .Client {Timeout :5*_a .Second }};
2023-05-29 17:26:33 +00:00
2023-12-17 13:54:01 +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 (_adg *CRLClient )MakeRequest (serverURL string ,cert *_ge .Certificate )([]byte ,error ){if _adg .HTTPClient ==nil {_adg .HTTPClient =_ef ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_ac .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];};_af ,_bge :=_adg .HTTPClient .Get (serverURL );if _bge !=nil {return nil ,_bge ;};defer _af .Body .Close ();_da ,_bge :=_ae .ReadAll (_af .Body );if _bge !=nil {return nil ,_bge ;};if _bcc ,_ :=_ed .Decode (_da );
_bcc !=nil {_da =_bcc .Bytes ;};return _da ,nil ;};
// 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 *_be .Client ;};
// 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 (_dgc *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_ge .Certificate )(*_dg .Response ,[]byte ,error ){if _dgc .HTTPClient ==nil {_dgc .HTTPClient =_ef ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_ac .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];};_fb ,_geca :=_dg .CreateRequest (cert ,issuer ,&_dg .RequestOptions {Hash :_dgc .Hash });if _geca !=nil {return nil ,nil ,_geca ;};_fe ,_geca :=_dgc .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",_e .NewReader (_fb ));
if _geca !=nil {return nil ,nil ,_geca ;};defer _fe .Body .Close ();_aeg ,_geca :=_ae .ReadAll (_fe .Body );if _geca !=nil {return nil ,nil ,_geca ;};if _dgd ,_ :=_ed .Decode (_aeg );_dgd !=nil {_aeg =_dgd .Bytes ;};_fbd ,_geca :=_dg .ParseResponseForCert (_aeg ,cert ,issuer );
if _geca !=nil {return nil ,nil ,_geca ;};return _fbd ,_aeg ,nil ;};
// NewCertClient returns a new certificate client.
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_ef ()}};
2023-08-03 17:30:04 +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.
// By default, an HTTP client with a 5 second timeout per request is used.
2023-12-17 13:54:01 +00:00
HTTPClient *_be .Client ;};
2023-11-11 11:29:03 +00:00
2023-12-17 13:54:01 +00:00
// TimestampClient represents a RFC 3161 timestamp client.
// It is used to obtain signed tokens from timestamp authority servers.
type TimestampClient struct{
2023-11-11 11:29:03 +00:00
2023-12-17 13:54:01 +00:00
// 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 *_be .Client ;
2023-08-03 17:30:04 +00:00
2023-12-17 13:54:01 +00:00
// Callbacks.
BeforeHTTPRequest func (_dag *_be .Request )error ;};
2023-08-03 17:30:04 +00:00
2023-12-17 13:54:01 +00:00
// IsCA returns true if the provided certificate appears to be a CA certificate.
func (_gec *CertClient )IsCA (cert *_ge .Certificate )bool {return cert .IsCA &&_e .Equal (cert .RawIssuer ,cert .RawSubject );};
// GetEncodedToken executes the timestamp request and returns the DER encoded
// timestamp token bytes.
func (_age *TimestampClient )GetEncodedToken (serverURL string ,req *_gee .Request )([]byte ,error ){if serverURL ==""{return nil ,_d .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 ,_d .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");};_bd ,_ce :=req .Marshal ();if _ce !=nil {return nil ,_ce ;
};_gef ,_ce :=_be .NewRequest ("\u0050\u004f\u0053\u0054",serverURL ,_e .NewBuffer (_bd ));if _ce !=nil {return nil ,_ce ;};_gef .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 _age .BeforeHTTPRequest !=nil {if _fdb :=_age .BeforeHTTPRequest (_gef );_fdb !=nil {return nil ,_fdb ;};};_bed :=_age .HTTPClient ;if _bed ==nil {_bed =_ef ();};_dca ,_ce :=_bed .Do (_gef );if _ce !=nil {return nil ,_ce ;};defer _dca .Body .Close ();
_gd ,_ce :=_ae .ReadAll (_dca .Body );if _ce !=nil {return nil ,_ce ;};if _dca .StatusCode !=_be .StatusOK {return nil ,_d .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",_dca .StatusCode );
};var _faa struct{Version _dc .RawValue ;Content _dc .RawValue ;};if _ ,_ce =_dc .Unmarshal (_gd ,&_faa );_ce !=nil {return nil ,_ce ;};return _faa .Content .FullBytes ,nil ;};
// NewTimestampClient returns a new timestamp client.
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_ef ()}};