Skip to content

Commit 7832abf

Browse files
committed
Improve code quality
1 parent 52ccca6 commit 7832abf

4 files changed

Lines changed: 266 additions & 225 deletions

File tree

src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/security/CertificateUtil.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ public static class CertificateUtil
1818
/// <returns>the CRL URL, or null</returns>
1919
public static string GetCrlUrl(X509Certificate certificate)
2020
{
21+
if (certificate == null)
22+
{
23+
throw new ArgumentNullException(nameof(certificate));
24+
}
25+
2126
try
2227
{
2328
var extension = certificate.GetExtensionValue(X509Extensions.CrlDistributionPoints);
29+
2430
if (extension is null)
2531
{
2632
return null;
2733
}
2834

29-
var crlDistPoint = CrlDistPoint.GetInstance(
30-
Asn1Object.FromByteArray(extension.GetOctets()));
35+
var crlDistPoint = CrlDistPoint.GetInstance(Asn1Object.FromByteArray(extension.GetOctets()));
36+
3137
if (crlDistPoint is null)
3238
{
3339
return null;
@@ -36,9 +42,11 @@ public static string GetCrlUrl(X509Certificate certificate)
3642
foreach (var dp in crlDistPoint.GetDistributionPoints())
3743
{
3844
var name = dp.DistributionPointName;
45+
3946
if (name != null && name.Type == DistributionPointName.FullName)
4047
{
4148
var generalNames = GeneralNames.GetInstance(name.Name);
49+
4250
foreach (var gn in generalNames.GetNames())
4351
{
4452
if (gn.TagNo == GeneralName.UniformResourceIdentifier)
@@ -65,6 +73,11 @@ public static string GetCrlUrl(X509Certificate certificate)
6573
/// <returns>the OCSP URL, or null</returns>
6674
public static string GetOcspUrl(X509Certificate certificate)
6775
{
76+
if (certificate == null)
77+
{
78+
throw new ArgumentNullException(nameof(certificate));
79+
}
80+
6881
try
6982
{
7083
return GetAccessDescriptionUrl(certificate, X509ObjectIdentifiers.OcspAccessMethod);
@@ -83,6 +96,11 @@ public static string GetOcspUrl(X509Certificate certificate)
8396
/// <returns>the TSA URL, or null</returns>
8497
public static string GetTsaUrl(X509Certificate certificate)
8598
{
99+
if (certificate == null)
100+
{
101+
throw new ArgumentNullException(nameof(certificate));
102+
}
103+
86104
try
87105
{
88106
return GetAccessDescriptionUrl(certificate, new DerObjectIdentifier(IdAdTimestamping));
@@ -93,18 +111,17 @@ public static string GetTsaUrl(X509Certificate certificate)
93111
}
94112
}
95113

96-
private static string GetAccessDescriptionUrl(
97-
X509Certificate certificate,
98-
DerObjectIdentifier accessMethod)
114+
private static string GetAccessDescriptionUrl(X509Certificate certificate, DerObjectIdentifier accessMethod)
99115
{
100116
var extension = certificate.GetExtensionValue(X509Extensions.AuthorityInfoAccess);
117+
101118
if (extension is null)
102119
{
103120
return null;
104121
}
105122

106-
var authInfoAccesss = AuthorityInformationAccess.GetInstance(
107-
Asn1Object.FromByteArray(extension.GetOctets()));
123+
var authInfoAccesss = AuthorityInformationAccess.GetInstance(Asn1Object.FromByteArray(extension.GetOctets()));
124+
108125
if (authInfoAccesss is null)
109126
{
110127
return null;
Lines changed: 100 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
namespace iTextSharp.text.pdf.security;
2-
31
using Org.BouncyCastle.Crypto;
42
using Org.BouncyCastle.Security;
53

4+
namespace iTextSharp.text.pdf.security;
5+
66
/// <summary>
77
/// Utility class providing digest algorithm constants and hash computation methods.
88
/// Maps OIDs to digest names and provides <c>Digest()</c> methods for streams and byte arrays.
@@ -29,80 +29,77 @@ public static class DigestAlgorithms
2929

3030
static DigestAlgorithms()
3131
{
32-
_digestNames["1.2.840.113549.2.5"] = "MD5";
33-
_digestNames["1.2.840.113549.2.2"] = "MD2";
34-
_digestNames["1.3.14.3.2.26"] = "SHA1";
35-
_digestNames["2.16.840.1.101.3.4.2.4"] = "SHA224";
36-
_digestNames["2.16.840.1.101.3.4.2.1"] = "SHA256";
37-
_digestNames["2.16.840.1.101.3.4.2.2"] = "SHA384";
38-
_digestNames["2.16.840.1.101.3.4.2.3"] = "SHA512";
39-
_digestNames["1.3.36.3.2.2"] = "RIPEMD128";
40-
_digestNames["1.3.36.3.2.1"] = "RIPEMD160";
41-
_digestNames["1.3.36.3.2.3"] = "RIPEMD256";
42-
_digestNames["1.2.840.113549.1.1.4"] = "MD5";
43-
_digestNames["1.2.840.113549.1.1.2"] = "MD2";
44-
_digestNames["1.2.840.113549.1.1.5"] = "SHA1";
45-
_digestNames["1.2.840.113549.1.1.14"] = "SHA224";
46-
_digestNames["1.2.840.113549.1.1.11"] = "SHA256";
47-
_digestNames["1.2.840.113549.1.1.12"] = "SHA384";
48-
_digestNames["1.2.840.113549.1.1.13"] = "SHA512";
49-
_digestNames["1.2.840.10040.4.3"] = "SHA1";
50-
_digestNames["2.16.840.1.101.3.4.3.1"] = "SHA224";
51-
_digestNames["2.16.840.1.101.3.4.3.2"] = "SHA256";
52-
_digestNames["2.16.840.1.101.3.4.3.3"] = "SHA384";
53-
_digestNames["2.16.840.1.101.3.4.3.4"] = "SHA512";
54-
_digestNames["1.3.36.3.3.1.3"] = "RIPEMD128";
55-
_digestNames["1.3.36.3.3.1.2"] = "RIPEMD160";
56-
_digestNames["1.3.36.3.3.1.4"] = "RIPEMD256";
57-
_digestNames["1.2.643.2.2.9"] = "GOST3411";
58-
59-
_allowedDigests["MD2"] = "1.2.840.113549.2.2";
60-
_allowedDigests["MD-2"] = "1.2.840.113549.2.2";
61-
_allowedDigests["MD5"] = "1.2.840.113549.2.5";
62-
_allowedDigests["MD-5"] = "1.2.840.113549.2.5";
63-
_allowedDigests["SHA1"] = "1.3.14.3.2.26";
64-
_allowedDigests["SHA-1"] = "1.3.14.3.2.26";
65-
_allowedDigests["SHA224"] = "2.16.840.1.101.3.4.2.4";
66-
_allowedDigests["SHA-224"] = "2.16.840.1.101.3.4.2.4";
67-
_allowedDigests["SHA256"] = "2.16.840.1.101.3.4.2.1";
68-
_allowedDigests["SHA-256"] = "2.16.840.1.101.3.4.2.1";
69-
_allowedDigests["SHA384"] = "2.16.840.1.101.3.4.2.2";
70-
_allowedDigests["SHA-384"] = "2.16.840.1.101.3.4.2.2";
71-
_allowedDigests["SHA512"] = "2.16.840.1.101.3.4.2.3";
72-
_allowedDigests["SHA-512"] = "2.16.840.1.101.3.4.2.3";
73-
_allowedDigests["RIPEMD128"] = "1.3.36.3.2.2";
74-
_allowedDigests["RIPEMD-128"] = "1.3.36.3.2.2";
75-
_allowedDigests["RIPEMD160"] = "1.3.36.3.2.1";
76-
_allowedDigests["RIPEMD-160"] = "1.3.36.3.2.1";
77-
_allowedDigests["RIPEMD256"] = "1.3.36.3.2.3";
78-
_allowedDigests["RIPEMD-256"] = "1.3.36.3.2.3";
79-
_allowedDigests["GOST3411"] = "1.2.643.2.2.9";
32+
_digestNames[key: "1.2.840.113549.2.5"] = "MD5";
33+
_digestNames[key: "1.2.840.113549.2.2"] = "MD2";
34+
_digestNames[key: "1.3.14.3.2.26"] = "SHA1";
35+
_digestNames[key: "2.16.840.1.101.3.4.2.4"] = "SHA224";
36+
_digestNames[key: "2.16.840.1.101.3.4.2.1"] = "SHA256";
37+
_digestNames[key: "2.16.840.1.101.3.4.2.2"] = "SHA384";
38+
_digestNames[key: "2.16.840.1.101.3.4.2.3"] = "SHA512";
39+
_digestNames[key: "1.3.36.3.2.2"] = "RIPEMD128";
40+
_digestNames[key: "1.3.36.3.2.1"] = "RIPEMD160";
41+
_digestNames[key: "1.3.36.3.2.3"] = "RIPEMD256";
42+
_digestNames[key: "1.2.840.113549.1.1.4"] = "MD5";
43+
_digestNames[key: "1.2.840.113549.1.1.2"] = "MD2";
44+
_digestNames[key: "1.2.840.113549.1.1.5"] = "SHA1";
45+
_digestNames[key: "1.2.840.113549.1.1.14"] = "SHA224";
46+
_digestNames[key: "1.2.840.113549.1.1.11"] = "SHA256";
47+
_digestNames[key: "1.2.840.113549.1.1.12"] = "SHA384";
48+
_digestNames[key: "1.2.840.113549.1.1.13"] = "SHA512";
49+
_digestNames[key: "1.2.840.10040.4.3"] = "SHA1";
50+
_digestNames[key: "2.16.840.1.101.3.4.3.1"] = "SHA224";
51+
_digestNames[key: "2.16.840.1.101.3.4.3.2"] = "SHA256";
52+
_digestNames[key: "2.16.840.1.101.3.4.3.3"] = "SHA384";
53+
_digestNames[key: "2.16.840.1.101.3.4.3.4"] = "SHA512";
54+
_digestNames[key: "1.3.36.3.3.1.3"] = "RIPEMD128";
55+
_digestNames[key: "1.3.36.3.3.1.2"] = "RIPEMD160";
56+
_digestNames[key: "1.3.36.3.3.1.4"] = "RIPEMD256";
57+
_digestNames[key: "1.2.643.2.2.9"] = "GOST3411";
58+
59+
_allowedDigests[key: "MD2"] = "1.2.840.113549.2.2";
60+
_allowedDigests[key: "MD-2"] = "1.2.840.113549.2.2";
61+
_allowedDigests[key: "MD5"] = "1.2.840.113549.2.5";
62+
_allowedDigests[key: "MD-5"] = "1.2.840.113549.2.5";
63+
_allowedDigests[key: "SHA1"] = "1.3.14.3.2.26";
64+
_allowedDigests[key: "SHA-1"] = "1.3.14.3.2.26";
65+
_allowedDigests[key: "SHA224"] = "2.16.840.1.101.3.4.2.4";
66+
_allowedDigests[key: "SHA-224"] = "2.16.840.1.101.3.4.2.4";
67+
_allowedDigests[key: "SHA256"] = "2.16.840.1.101.3.4.2.1";
68+
_allowedDigests[key: "SHA-256"] = "2.16.840.1.101.3.4.2.1";
69+
_allowedDigests[key: "SHA384"] = "2.16.840.1.101.3.4.2.2";
70+
_allowedDigests[key: "SHA-384"] = "2.16.840.1.101.3.4.2.2";
71+
_allowedDigests[key: "SHA512"] = "2.16.840.1.101.3.4.2.3";
72+
_allowedDigests[key: "SHA-512"] = "2.16.840.1.101.3.4.2.3";
73+
_allowedDigests[key: "RIPEMD128"] = "1.3.36.3.2.2";
74+
_allowedDigests[key: "RIPEMD-128"] = "1.3.36.3.2.2";
75+
_allowedDigests[key: "RIPEMD160"] = "1.3.36.3.2.1";
76+
_allowedDigests[key: "RIPEMD-160"] = "1.3.36.3.2.1";
77+
_allowedDigests[key: "RIPEMD256"] = "1.3.36.3.2.3";
78+
_allowedDigests[key: "RIPEMD-256"] = "1.3.36.3.2.3";
79+
_allowedDigests[key: "GOST3411"] = "1.2.643.2.2.9";
8080
}
8181

8282
/// <summary>
8383
/// Returns a message digest instance for the given algorithm name.
8484
/// </summary>
8585
/// <param name="hashAlgorithm">the digest algorithm name (e.g. "SHA-256")</param>
86-
/// <returns>an <see cref="IDigest"/> instance</returns>
87-
public static IDigest GetMessageDigest(string hashAlgorithm) =>
88-
DigestUtilities.GetDigest(hashAlgorithm);
86+
/// <returns>an <see cref="IDigest" /> instance</returns>
87+
public static IDigest GetMessageDigest(string hashAlgorithm) => DigestUtilities.GetDigest(hashAlgorithm);
8988

9089
/// <summary>
9190
/// Returns a message digest instance for the given OID.
9291
/// </summary>
9392
/// <param name="digestOid">the digest OID</param>
94-
/// <returns>an <see cref="IDigest"/> instance</returns>
95-
public static IDigest GetMessageDigestFromOid(string digestOid) =>
96-
DigestUtilities.GetDigest(digestOid);
93+
/// <returns>an <see cref="IDigest" /> instance</returns>
94+
public static IDigest GetMessageDigestFromOid(string digestOid) => DigestUtilities.GetDigest(digestOid);
9795

9896
/// <summary>
9997
/// Computes the hash of a stream using the specified algorithm.
10098
/// </summary>
10199
/// <param name="data">the input stream</param>
102100
/// <param name="hashAlgorithm">the digest algorithm name</param>
103101
/// <returns>the hash bytes</returns>
104-
public static byte[] Digest(Stream data, string hashAlgorithm) =>
105-
Digest(data, GetMessageDigest(hashAlgorithm));
102+
public static byte[] Digest(Stream data, string hashAlgorithm) => Digest(data, GetMessageDigest(hashAlgorithm));
106103

107104
/// <summary>
108105
/// Computes the hash of a stream using the given digest instance.
@@ -112,54 +109,85 @@ public static byte[] Digest(Stream data, string hashAlgorithm) =>
112109
/// <returns>the hash bytes</returns>
113110
public static byte[] Digest(Stream data, IDigest messageDigest)
114111
{
112+
if (data == null)
113+
{
114+
throw new ArgumentNullException(nameof(data));
115+
}
116+
117+
if (messageDigest == null)
118+
{
119+
throw new ArgumentNullException(nameof(messageDigest));
120+
}
121+
115122
var buf = new byte[8192];
116123
int n;
117-
while ((n = data.Read(buf, 0, buf.Length)) > 0)
124+
125+
while ((n = data.Read(buf, offset: 0, buf.Length)) > 0)
118126
{
119-
messageDigest.BlockUpdate(buf, 0, n);
127+
messageDigest.BlockUpdate(buf, inOff: 0, n);
120128
}
121129

122130
var result = new byte[messageDigest.GetDigestSize()];
123-
messageDigest.DoFinal(result, 0);
131+
messageDigest.DoFinal(result, outOff: 0);
132+
124133
return result;
125134
}
126135

127136
/// <summary>
128137
/// Computes the hash of a byte array segment.
129138
/// </summary>
130-
public static byte[] Digest(string algo, byte[] b, int offset, int len) =>
131-
Digest(DigestUtilities.GetDigest(algo), b, offset, len);
139+
public static byte[] Digest(string algo, byte[] b, int offset, int len)
140+
=> Digest(DigestUtilities.GetDigest(algo), b, offset, len);
132141

133142
/// <summary>
134143
/// Computes the hash of a byte array.
135144
/// </summary>
136-
public static byte[] Digest(string algo, byte[] b) =>
137-
Digest(DigestUtilities.GetDigest(algo), b, 0, b.Length);
145+
public static byte[] Digest(string algo, byte[] b)
146+
{
147+
if (b == null)
148+
{
149+
throw new ArgumentNullException(nameof(b));
150+
}
151+
152+
return Digest(DigestUtilities.GetDigest(algo), b, offset: 0, b.Length);
153+
}
138154

139155
/// <summary>
140156
/// Computes the hash of a byte array segment using a digest instance.
141157
/// </summary>
142158
public static byte[] Digest(IDigest d, byte[] b, int offset, int len)
143159
{
160+
if (d == null)
161+
{
162+
throw new ArgumentNullException(nameof(d));
163+
}
164+
144165
d.BlockUpdate(b, offset, len);
145166
var result = new byte[d.GetDigestSize()];
146-
d.DoFinal(result, 0);
167+
d.DoFinal(result, outOff: 0);
168+
147169
return result;
148170
}
149171

150172
/// <summary>
151173
/// Computes the hash of a byte array using a digest instance.
152174
/// </summary>
153-
public static byte[] Digest(IDigest d, byte[] b) =>
154-
Digest(d, b, 0, b.Length);
175+
public static byte[] Digest(IDigest d, byte[] b)
176+
{
177+
if (b == null)
178+
{
179+
throw new ArgumentNullException(nameof(b));
180+
}
181+
182+
return Digest(d, b, offset: 0, b.Length);
183+
}
155184

156185
/// <summary>
157186
/// Returns the human-readable digest name for the given OID.
158187
/// </summary>
159188
/// <param name="oid">the digest OID</param>
160189
/// <returns>the digest name, or the OID itself if unknown</returns>
161-
public static string GetDigest(string oid) =>
162-
_digestNames.TryGetValue(oid, out var name) ? name : oid;
190+
public static string GetDigest(string oid) => _digestNames.TryGetValue(oid, out var name) ? name : oid;
163191

164192
/// <summary>
165193
/// Returns the OID of a digest algorithm that is allowed in PDF,
@@ -170,6 +198,7 @@ public static string GetDigest(string oid) =>
170198
public static string GetAllowedDigests(string name)
171199
{
172200
_allowedDigests.TryGetValue(name, out var oid);
201+
173202
return oid;
174203
}
175-
}
204+
}

0 commit comments

Comments
 (0)