mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-29 13:48:54 +08:00
100 lines
8.4 KiB
Go
100 lines
8.4 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 (_e "bytes";_bb "crypto";_bbf "crypto/x509";_dc "encoding/asn1";_d "encoding/pem";_gea "errors";_gf "fmt";_ec "github.com/unidoc/timestamp";_ca "github.com/unidoc/unipdf/v3/common";_ce "golang.org/x/crypto/ocsp";_ge "io";_f "io/ioutil";
|
|
_c "net/http";_g "time";);
|
|
|
|
// GetEncodedToken executes the timestamp request and returns the DER encoded
|
|
// timestamp token bytes.
|
|
func (_gg *TimestampClient )GetEncodedToken (serverURL string ,req *_ec .Request )([]byte ,error ){if serverURL ==""{return nil ,_gf .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 ,_gf .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");};_fbd ,_bdb :=req .Marshal ();if _bdb !=nil {return nil ,_bdb ;
|
|
};_dbe :=_gg .HTTPClient ;if _dbe ==nil {_dbe =_dcc ();};_be ,_bdb :=_dbe .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",_e .NewBuffer (_fbd ));
|
|
if _bdb !=nil {return nil ,_bdb ;};defer _be .Body .Close ();_bcb ,_bdb :=_f .ReadAll (_be .Body );if _bdb !=nil {return nil ,_bdb ;};if _be .StatusCode !=_c .StatusOK {return nil ,_gf .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",_be .StatusCode );
|
|
};var _fd struct{Version _dc .RawValue ;Content _dc .RawValue ;};if _ ,_bdb =_dc .Unmarshal (_bcb ,&_fd );_bdb !=nil {return nil ,_bdb ;};return _fd .Content .FullBytes ,nil ;};
|
|
|
|
// IsCA returns true if the provided certificate appears to be a CA certificate.
|
|
func (_a *CertClient )IsCA (cert *_bbf .Certificate )bool {return cert .IsCA &&_e .Equal (cert .RawIssuer ,cert .RawSubject );};
|
|
|
|
// NewCertClient returns a new certificate client.
|
|
func NewCertClient ()*CertClient {return &CertClient {HTTPClient :_dcc ()}};
|
|
|
|
// NewCRLClient returns a new CRL client.
|
|
func NewCRLClient ()*CRLClient {return &CRLClient {HTTPClient :_dcc ()}};
|
|
|
|
// 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 *_c .Client ;};
|
|
|
|
// NewTimestampClient returns a new timestamp client.
|
|
func NewTimestampClient ()*TimestampClient {return &TimestampClient {HTTPClient :_dcc ()}};
|
|
|
|
// 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 *_c .Client ;
|
|
|
|
// Hash is the hash function used when constructing the OCSP
|
|
// requests. If zero, SHA-1 will be used.
|
|
Hash _bb .Hash ;};
|
|
|
|
// Get retrieves the certificate at the specified URL.
|
|
func (_bd *CertClient )Get (url string )(*_bbf .Certificate ,error ){if _bd .HTTPClient ==nil {_bd .HTTPClient =_dcc ();};_cee ,_bf :=_bd .HTTPClient .Get (url );if _bf !=nil {return nil ,_bf ;};defer _cee .Body .Close ();_cg ,_bf :=_f .ReadAll (_cee .Body );
|
|
if _bf !=nil {return nil ,_bf ;};if _de ,_ :=_d .Decode (_cg );_de !=nil {_cg =_de .Bytes ;};_cc ,_bf :=_bbf .ParseCertificate (_cg );if _bf !=nil {return nil ,_bf ;};return _cc ,nil ;};
|
|
|
|
// NewTimestampRequest returns a new timestamp request based
|
|
// on the specified options.
|
|
func NewTimestampRequest (body _ge .Reader ,opts *_ec .RequestOptions )(*_ec .Request ,error ){if opts ==nil {opts =&_ec .RequestOptions {};};if opts .Hash ==0{opts .Hash =_bb .SHA256 ;};if !opts .Hash .Available (){return nil ,_bbf .ErrUnsupportedAlgorithm ;
|
|
};_ecb :=opts .Hash .New ();if _ ,_gfa :=_ge .Copy (_ecb ,body );_gfa !=nil {return nil ,_gfa ;};return &_ec .Request {HashAlgorithm :opts .Hash ,HashedMessage :_ecb .Sum (nil ),Certificates :opts .Certificates ,TSAPolicyOID :opts .TSAPolicyOID ,Nonce :opts .Nonce },nil ;
|
|
};func _dcc ()*_c .Client {return &_c .Client {Timeout :5*_g .Second }};
|
|
|
|
// 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 (_ff *CRLClient )MakeRequest (serverURL string ,cert *_bbf .Certificate )([]byte ,error ){if _ff .HTTPClient ==nil {_ff .HTTPClient =_dcc ();};if serverURL ==""{if len (cert .CRLDistributionPoints )==0{return nil ,_gea .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];};_bc ,_eg :=_ff .HTTPClient .Get (serverURL );if _eg !=nil {return nil ,_eg ;};defer _bc .Body .Close ();_ba ,_eg :=_f .ReadAll (_bc .Body );if _eg !=nil {return nil ,_eg ;};if _ed ,_ :=_d .Decode (_ba );_ed !=nil {_ba =_ed .Bytes ;
|
|
};return _ba ,nil ;};
|
|
|
|
// 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 (_bg *OCSPClient )MakeRequest (serverURL string ,cert ,issuer *_bbf .Certificate )(*_ce .Response ,[]byte ,error ){if _bg .HTTPClient ==nil {_bg .HTTPClient =_dcc ();};if serverURL ==""{if len (cert .OCSPServer )==0{return nil ,nil ,_gea .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];};_eb ,_cd :=_ce .CreateRequest (cert ,issuer ,&_ce .RequestOptions {Hash :_bg .Hash });if _cd !=nil {return nil ,nil ,_cd ;};_bca ,_cd :=_bg .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 (_eb ));
|
|
if _cd !=nil {return nil ,nil ,_cd ;};defer _bca .Body .Close ();_db ,_cd :=_f .ReadAll (_bca .Body );if _cd !=nil {return nil ,nil ,_cd ;};if _bge ,_ :=_d .Decode (_db );_bge !=nil {_db =_bge .Bytes ;};_ceec ,_cd :=_ce .ParseResponseForCert (_db ,cert ,issuer );
|
|
if _cd !=nil {return nil ,nil ,_cd ;};return _ceec ,_db ,nil ;};
|
|
|
|
// NewOCSPClient returns a new OCSP client.
|
|
func NewOCSPClient ()*OCSPClient {return &OCSPClient {HTTPClient :_dcc (),Hash :_bb .SHA1 }};
|
|
|
|
// GetIssuer retrieves the issuer of the provided certificate.
|
|
func (_df *CertClient )GetIssuer (cert *_bbf .Certificate )(*_bbf .Certificate ,error ){for _ ,_gff :=range cert .IssuingCertificateURL {_ded ,_fb :=_df .Get (_gff );if _fb !=nil {_ca .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 ,_fb );
|
|
continue ;};return _ded ,nil ;};return nil ,_gf .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");};
|
|
|
|
// 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 *_c .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 *_c .Client ;}; |