From a40d91115a89cd21ee018ee0f24cddb4053d5a91 Mon Sep 17 00:00:00 2001 From: Henrique-Fernandes <48771926+Henrique-Fernandes@users.noreply.github.com> Date: Tue, 24 Aug 2021 14:05:56 +0100 Subject: [PATCH] Commit HF - Adition of WebProxy Added WebProxy to TSAClientBouncyCastle. It is required when working behind a proxy server. Some how the java version has implemented the proxy but not the .net version. Also i had a look to the IText 7 Core (at least the on the NuGet), also did not have the implementation of proxy in this class. The proxy will be used when on the webrequest in GetTSAResponse() method. --- .../text/pdf/security/ITSAClient.cs | 6 ++ .../pdf/security/TSAClientBouncyCastle.cs | 59 +++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/core/iTextSharp/text/pdf/security/ITSAClient.cs b/src/core/iTextSharp/text/pdf/security/ITSAClient.cs index b197a2987..2dad09758 100644 --- a/src/core/iTextSharp/text/pdf/security/ITSAClient.cs +++ b/src/core/iTextSharp/text/pdf/security/ITSAClient.cs @@ -76,5 +76,11 @@ public interface ITSAClient { * @throws Exception - TSA request failed */ byte[] GetTimeStampToken(byte[] imprint); + + /** + * Sets the webproxy needed when working behing proxy. + * @param proxy WebProxy + */ + void SetProxy(System.Net.WebProxy proxy); } } diff --git a/src/core/iTextSharp/text/pdf/security/TSAClientBouncyCastle.cs b/src/core/iTextSharp/text/pdf/security/TSAClientBouncyCastle.cs index 3257703f0..2574e8e01 100644 --- a/src/core/iTextSharp/text/pdf/security/TSAClientBouncyCastle.cs +++ b/src/core/iTextSharp/text/pdf/security/TSAClientBouncyCastle.cs @@ -95,15 +95,25 @@ public class TSAClientBouncyCastle : ITSAClient { /** TSA request policy */ private string tsaReqPolicy = null; - + + /** Implements webproxy needed when behind a proxy */ + private WebProxy webProxy = null; /** * Creates an instance of a TSAClient that will use BouncyCastle. * @param url String - Time Stamp Authority URL (i.e. "http://tsatest1.digistamp.com/TSA") */ public TSAClientBouncyCastle(String url) : this(url, null, null, DEFAULTTOKENSIZE, DEFAULTHASHALGORITHM) { - } + } + /** + * Creates an instance of a TSAClient that will use BouncyCastle. + * @param url String - Time Stamp Authority URL (i.e. "http://tsatest1.digistamp.com/TSA") + * @param webproxy WebProxy - WebProxy + */ + public TSAClientBouncyCastle(String url, WebProxy webproxy) + : this(url, null, null, DEFAULTTOKENSIZE, DEFAULTHASHALGORITHM, webproxy) { } + /** * Creates an instance of a TSAClient that will use BouncyCastle. * @param url String - Time Stamp Authority URL (i.e. "http://tsatest1.digistamp.com/TSA") @@ -112,8 +122,19 @@ public TSAClientBouncyCastle(String url) */ public TSAClientBouncyCastle(String url, String username, String password) : this(url, username, password, DEFAULTTOKENSIZE, DEFAULTHASHALGORITHM) { - } - + } + + /** + * Creates an instance of a TSAClient that will use BouncyCastle. + * @param url String - Time Stamp Authority URL (i.e. "http://tsatest1.digistamp.com/TSA") + * @param username String - user(account) name + * @param password String - password + * @param webproxy WebProxy - WebProxy + */ + public TSAClientBouncyCastle(String url, String username, String password, WebProxy webproxy) + : this(url, username, password, DEFAULTTOKENSIZE, DEFAULTHASHALGORITHM, webproxy) { + } + /** * Constructor. * Note the token size estimate is updated by each call, as the token @@ -132,6 +153,33 @@ public TSAClientBouncyCastle(String url, String username, String password) this.digestAlgorithm = digestAlgorithm; } + /** + * Constructor. + * Note the token size estimate is updated by each call, as the token + * size is not likely to change (as long as we call the same TSA using + * the same imprint length). + * @param url String - Time Stamp Authority URL (i.e. "http://tsatest1.digistamp.com/TSA") + * @param username String - user(account) name + * @param password String - password + * @param tokSzEstimate int - estimated size of received time stamp token (DER encoded) + * @param webproxy WebProxy - WebProxy + */ + public TSAClientBouncyCastle(String url, String username, String password, int tokSzEstimate, String digestAlgorithm, WebProxy webproxy) + { + this.tsaURL = url; + this.tsaUsername = username; + this.tsaPassword = password; + this.tokenSizeEstimate = tokSzEstimate; + this.digestAlgorithm = digestAlgorithm; + this.webProxy = webproxy; + } + + /** + * Sets the webproxy needed when working behing proxy. + * @param proxy WebProxy + */ + public void SetProxy(WebProxy proxy) => this.webProxy = proxy; + /** * @param tsaInfo the tsaInfo to set */ @@ -226,6 +274,9 @@ public TSAClientBouncyCastle(String url, String username, String password) con.ContentLength = requestBytes.Length; con.ContentType = "application/timestamp-query"; con.Method = "POST"; + //Added support for webproxy + if (this.webProxy != null) con.Proxy = this.webProxy; + if ((tsaUsername != null) && !tsaUsername.Equals("") ) { string authInfo = tsaUsername + ":" + tsaPassword; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo), Base64FormattingOptions.None);