Skip to content

Commit

Permalink
Fix missing Dispose call on local IDisposable
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Oct 2, 2023
1 parent 972d061 commit bb8defa
Show file tree
Hide file tree
Showing 20 changed files with 154 additions and 99 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/codeql/codeql-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ query-filters:
id: cs/useless-if-statement
- exclude:
id: cs/empty-block
- exclude:
id: cs/useless-assignment-to-local
2 changes: 1 addition & 1 deletion src/iTextSharp.LGPLv2.Core/System/util/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Clear()

public void Load(Stream inStream)
{
var inp = new StreamReader(inStream, EncodingsRegistry.GetEncoding(1252));
using var inp = new StreamReader(inStream, EncodingsRegistry.GetEncoding(1252));
while (true)
{
// Get next line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static GlyphList()
}

var buf = new byte[1024];
var outputStream = new MemoryStream();
using var outputStream = new MemoryStream();
while (true)
{
var size = resourceStream.Read(buf, 0, buf.Length);
Expand Down
11 changes: 7 additions & 4 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncodings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ internal static void EncodeSequence(int size, byte[] seqs, char cid, IList<char[

internal static void EncodeStream(Stream inp, IList<char[]> planes)
{
var rd = new StreamReader(inp, Encoding.ASCII);
using var rd = new StreamReader(inp, Encoding.ASCII);
string line = null;
var state = CIDNONE;
var seqs = new byte[7];
Expand Down Expand Up @@ -674,10 +674,12 @@ internal static void EncodeStream(Stream inp, IList<char[]> planes)
var tk = new StringTokenizer(line);
var t = tk.NextToken();
var size = t.Length / 2 - 1;
var start = long.Parse(t.Substring(1, t.Length - 2), NumberStyles.HexNumber,
var start = long.Parse(t.Substring(1, t.Length - 2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture);
t = tk.NextToken();
var end = long.Parse(t.Substring(1, t.Length - 2), NumberStyles.HexNumber,
var end = long.Parse(t.Substring(1, t.Length - 2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture);
t = tk.NextToken();
var cid = int.Parse(t, CultureInfo.InvariantCulture);
Expand All @@ -701,7 +703,8 @@ internal static void EncodeStream(Stream inp, IList<char[]> planes)
var tk = new StringTokenizer(line);
var t = tk.NextToken();
var size = t.Length / 2 - 1;
var start = long.Parse(t.Substring(1, t.Length - 2), NumberStyles.HexNumber,
var start = long.Parse(t.Substring(1, t.Length - 2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture);
t = tk.NextToken();
var cid = int.Parse(t, CultureInfo.InvariantCulture);
Expand Down
35 changes: 26 additions & 9 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public byte[] DecryptByteArray(byte[] b)
throw new ArgumentNullException(nameof(b));
}

var ba = new MemoryStream();
using var ba = new MemoryStream();
var dec = GetDecryptor();
var b2 = dec.Update(b, 0, b.Length);
if (b2 != null)
Expand Down Expand Up @@ -327,7 +327,7 @@ public PdfDictionary GetEncryptionDictionary()
}

#if NET40
SHA1 sh = new SHA1CryptoServiceProvider();
using var sh = new SHA1CryptoServiceProvider();
byte[] encodedRecipient = null;
byte[] seed = PublicKeyHandler.GetSeed();
sh.TransformBlock(seed, 0, seed.Length, seed, 0);
Expand Down Expand Up @@ -571,7 +571,10 @@ public void SetupByEncryptionKey(byte[] key, int keyLength)

/// <summary>
/// </summary>
public void SetupByOwnerPassword(byte[] documentId, byte[] ownerPassword, byte[] userKey, byte[] ownerKey,
public void SetupByOwnerPassword(byte[] documentId,
byte[] ownerPassword,
byte[] userKey,
byte[] ownerKey,
int permissions)
{
SetupByOwnerPad(documentId, PadPassword(ownerPassword), userKey, ownerKey, permissions);
Expand Down Expand Up @@ -758,8 +761,13 @@ private void SetupUserKey()
/// implements step d of Algorithm 2.A: Retrieving the file encryption key from an encrypted document in order to
/// decrypt it (revision 6 and later) - ISO 32000-2 section 7.6.4.3.3
/// </summary>
public void SetupByOwnerPassword(byte[] documentId, byte[] ownerPassword,
byte[] uValue, byte[] ueValue, byte[] oValue, byte[] oeValue, int permissions)
public void SetupByOwnerPassword(byte[] documentId,
byte[] ownerPassword,
byte[] uValue,
byte[] ueValue,
byte[] oValue,
byte[] oeValue,
int permissions)
{
if (oeValue == null)
{
Expand All @@ -778,8 +786,13 @@ public void SetupByOwnerPassword(byte[] documentId, byte[] ownerPassword,
/// implements step e of Algorithm 2.A: Retrieving the file encryption key from an encrypted document in order to
/// decrypt it (revision 6 and later) - ISO 32000-2 section 7.6.4.3.3
/// </summary>
public void SetupByUserPassword(byte[] documentId, byte[] userPassword,
byte[] uValue, byte[] ueValue, byte[] oValue, byte[] oeValue, int permissions)
public void SetupByUserPassword(byte[] documentId,
byte[] userPassword,
byte[] uValue,
byte[] ueValue,
byte[] oValue,
byte[] oeValue,
int permissions)
{
if (ueValue == null)
{
Expand Down Expand Up @@ -851,8 +864,12 @@ public static byte[] HashAlg2B(byte[] input, byte[] salt, byte[] userKey)
Array.Copy(k1, 0, k1, singleSequenceSize * i, singleSequenceSize);
}

var e = AesCbcNoPadding.ProcessBlock(true, k.CopyOf(16),
k1, 0, k1.Length, k.CopyOfRange(16, 32));
var e = AesCbcNoPadding.ProcessBlock(true,
k.CopyOf(16),
k1,
0,
k1.Length,
k.CopyOfRange(16, 32));

lastEByte = e[e.Length - 1] & 0xFF;

Expand Down
56 changes: 33 additions & 23 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfPKCS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public PdfPkcs7(byte[] contentsKey, byte[] certsKey)
_signCerts = _certs;
SigningCertificate = _certs[0];
CrLs = new List<object>();
var inp = new Asn1InputStream(new MemoryStream(contentsKey));
using var memoryStream = new MemoryStream(contentsKey);
using var inp = new Asn1InputStream(memoryStream);
_digest = ((DerOctetString)inp.ReadObject()).GetOctets();
_sig = SignerUtilities.GetSigner("SHA1withRSA");
_sig.Init(false, SigningCertificate.GetPublicKey());
Expand All @@ -167,7 +168,8 @@ public PdfPkcs7(byte[] contentsKey, byte[] certsKey)
/// <param name="contentsKey">the /Contents key</param>
public PdfPkcs7(byte[] contentsKey)
{
var din = new Asn1InputStream(new MemoryStream(contentsKey));
using var memoryStream = new MemoryStream(contentsKey);
using var din = new Asn1InputStream(memoryStream);

//
// Basic checks to make sure it's a PKCS#7 SignedData Object
Expand Down Expand Up @@ -353,8 +355,11 @@ public PdfPkcs7(byte[] contentsKey)
/// <param name="crlList">the certificate revocation list</param>
/// <param name="hashAlgorithm">the hash algorithm</param>
/// <param name="hasRsAdata"> true if the sub-filter is adbe.pkcs7.sha1</param>
public PdfPkcs7(ICipherParameters privKey, X509Certificate[] certChain, object[] crlList,
string hashAlgorithm, bool hasRsAdata)
public PdfPkcs7(ICipherParameters privKey,
X509Certificate[] certChain,
object[] crlList,
string hashAlgorithm,
bool hasRsAdata)
{
if (certChain == null)
{
Expand Down Expand Up @@ -691,7 +696,9 @@ public static string VerifyCertificate(X509Certificate cert, object[] crls, Date
/// <param name="crls">the certificate revocation list or null </param>
/// <param name="calendar">the date or null for the current date</param>
/// <returns> null if the certificate chain could be validade or a</returns>
public static object[] VerifyCertificates(X509Certificate[] certs, IList<X509Certificate> keystore, object[] crls,
public static object[] VerifyCertificates(X509Certificate[] certs,
IList<X509Certificate> keystore,
object[] crls,
DateTime calendar)
{
if (certs == null)
Expand Down Expand Up @@ -917,12 +924,10 @@ public byte[] GetEncodedPkcs1()
_digest = _sig.GenerateSignature();
}

var bOut = new MemoryStream();

var dout = Asn1OutputStream.Create(bOut);
using var bOut = new MemoryStream();
using var dout = Asn1OutputStream.Create(bOut);
dout.WriteObject(new DerOctetString(_digest));
dout.Dispose();

return bOut.ToArray();
}

Expand Down Expand Up @@ -1006,7 +1011,8 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli
v = new Asn1EncodableVector();
foreach (var xcert in _certs)
{
var tempstream = new Asn1InputStream(new MemoryStream(xcert.GetEncoded()));
using var memoryStream = new MemoryStream(xcert.GetEncoded());
using var tempstream = new Asn1InputStream(memoryStream);
v.Add(tempstream.ReadObject());
}

Expand Down Expand Up @@ -1034,7 +1040,8 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli
// add the authenticated attribute if present
if (secondDigest != null /*&& signingTime != null*/)
{
signerinfo.Add(new DerTaggedObject(false, 0,
signerinfo.Add(new DerTaggedObject(false,
0,
getAuthenticatedAttributeSet(secondDigest, signingTime, ocsp)));
}

Expand All @@ -1052,7 +1059,8 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli
// Sam found Adobe expects time-stamped SHA1-1 of the encrypted digest
if (tsaClient != null)
{
var tsImprint = SHA1.Create().ComputeHash(_digest);
using var sha1 = SHA1.Create();
var tsImprint = sha1.ComputeHash(_digest);
var tsToken = tsaClient.GetTimeStampToken(this, tsImprint);
if (tsToken != null)
{
Expand Down Expand Up @@ -1091,11 +1099,9 @@ public byte[] GetEncodedPkcs7(byte[] secondDigest, DateTime signingTime, ITsaCli
whole.Add(new DerObjectIdentifier(IdPkcs7SignedData));
whole.Add(new DerTaggedObject(0, new DerSequence(body)));

var bOut = new MemoryStream();

var dout = Asn1OutputStream.Create(bOut);
using var bOut = new MemoryStream();
using var dout = Asn1OutputStream.Create(bOut);
dout.WriteObject(new DerSequence(whole));

return bOut.ToArray();
}

Expand Down Expand Up @@ -1258,10 +1264,11 @@ private static Asn1Object getExtensionValue(X509Certificate cert, string oid)
return null;
}

var aIn = new Asn1InputStream(new MemoryStream(bytes));
using var memoryStream = new MemoryStream(bytes);
using var aIn = new Asn1InputStream(memoryStream);
var octs = (Asn1OctetString)aIn.ReadObject();
aIn = new Asn1InputStream(new MemoryStream(octs.GetOctets()));
return aIn.ReadObject();
using var ain = new Asn1InputStream(new MemoryStream(octs.GetOctets()));
return ain.ReadObject();
}

/// <summary>
Expand All @@ -1271,7 +1278,8 @@ private static Asn1Object getExtensionValue(X509Certificate cert, string oid)
/// <returns>a DERObject</returns>
private static Asn1Object getIssuer(byte[] enc)
{
var inp = new Asn1InputStream(new MemoryStream(enc));
using var memoryStream = new MemoryStream(enc);
using var inp = new Asn1InputStream(memoryStream);
var seq = (Asn1Sequence)inp.ReadObject();
return (Asn1Object)seq[seq[0] is DerTaggedObject ? 3 : 2];
}
Expand All @@ -1290,7 +1298,8 @@ private static string getStringFromGeneralName(Asn1Object names)
/// <returns>a DERObject</returns>
private static Asn1Object getSubject(byte[] enc)
{
var inp = new Asn1InputStream(new MemoryStream(enc));
using var memoryStream = new MemoryStream(enc);
using var inp = new Asn1InputStream(memoryStream);
var seq = (Asn1Sequence)inp.ReadObject();
return (Asn1Object)seq[seq[0] is DerTaggedObject ? 5 : 4];
}
Expand All @@ -1314,7 +1323,8 @@ private static Asn1EncodableVector buildUnauthenticatedAttributes(byte[] timeSta
// @todo: move this together with the rest of the defintions
var idTimeStampToken = "1.2.840.113549.1.9.16.2.14"; // RFC 3161 id-aa-timeStampToken

var tempstream = new Asn1InputStream(new MemoryStream(timeStampToken));
using var memoryStream = new MemoryStream(timeStampToken);
using var tempstream = new Asn1InputStream(memoryStream);
var unauthAttributes = new Asn1EncodableVector();

var v = new Asn1EncodableVector();
Expand Down Expand Up @@ -1408,7 +1418,7 @@ private void findOcsp(Asn1Sequence seq)
}

var os = (DerOctetString)seq[1];
var inp = new Asn1InputStream(os.GetOctets());
using var inp = new Asn1InputStream(os.GetOctets());
var resp = BasicOcspResponse.GetInstance(inp.ReadObject());
Ocsp = new BasicOcspResp(resp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,11 @@ public byte[] GetEncodedRecipient(int index)
pkcs7Input[23] = one;

var obj = createDerForRecipient(pkcs7Input, certificate);

var baos = new MemoryStream();

var k = Asn1OutputStream.Create(baos, Asn1Encodable.Der);

using var baos = new MemoryStream();
using var k = Asn1OutputStream.Create(baos, Asn1Encodable.Der);
k.WriteObject(obj);

cms = baos.ToArray();

recipient.Cms = cms;

return cms;
}

Expand Down Expand Up @@ -104,8 +98,8 @@ public PdfArray GetEncodedRecipients()

private static KeyTransRecipientInfo computeRecipientInfo(X509Certificate x509Certificate, byte[] abyte0)
{
var asn1Inputstream =
new Asn1InputStream(new MemoryStream(x509Certificate.GetTbsCertificate()));
using var memoryStream = new MemoryStream(x509Certificate.GetTbsCertificate());
using var asn1Inputstream = new Asn1InputStream(memoryStream);
var tbscertificatestructure =
TbsCertificateStructure.GetInstance(asn1Inputstream.ReadObject());
var algorithmidentifier = tbscertificatestructure.SubjectPublicKeyInfo.AlgorithmID;
Expand Down
Loading

0 comments on commit bb8defa

Please sign in to comment.