Skip to content

Commit 5c7f832

Browse files
Refactor code in order to faciliate autoporting
DEVSIX-6119 Autoported commit. Original commit hash: [43a7744f1] Manual files: kernel/src/main/java/com/itextpdf/kernel/crypto/CryptoUtil.java
1 parent fb9a103 commit 5c7f832

File tree

5 files changed

+93
-13
lines changed

5 files changed

+93
-13
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using Org.BouncyCastle.Asn1;
25+
using iText.Commons.Utils;
26+
using iText.IO.Source;
27+
using iText.Kernel.Exceptions;
28+
using iText.Test;
29+
30+
namespace iText.Kernel.Crypto {
31+
public class CryptoUtilTest : ExtendedITextTest {
32+
[NUnit.Framework.Test]
33+
public virtual void CreateBerStreamTest() {
34+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
35+
DerOutputStream stream = CryptoUtil.CreateAsn1OutputStream(baos, Org.BouncyCastle.Asn1.Asn1Encodable.Ber);
36+
NUnit.Framework.Assert.IsNotNull(stream);
37+
}
38+
39+
[NUnit.Framework.Test]
40+
public virtual void CreateDerStreamTest() {
41+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
42+
DerOutputStream stream = CryptoUtil.CreateAsn1OutputStream(baos, Org.BouncyCastle.Asn1.Asn1Encodable.Der);
43+
NUnit.Framework.Assert.IsNotNull(stream);
44+
}
45+
46+
[NUnit.Framework.Test]
47+
public virtual void CreateUnsupportedEncodingStreamTest() {
48+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
49+
Exception e = NUnit.Framework.Assert.Catch(typeof(NotSupportedException), () => CryptoUtil.CreateAsn1OutputStream
50+
(baos, "DL"));
51+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(KernelExceptionMessageConstant.UNSUPPORTED_ASN1_ENCODING
52+
, "DL"), e.Message);
53+
}
54+
}
55+
}

itext/itext.kernel/itext/kernel/crypto/CryptoUtil.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ source product.
4343

4444
using System;
4545
using System.IO;
46+
using iText.Commons.Utils;
47+
using iText.Kernel.Exceptions;
48+
using Org.BouncyCastle.Asn1;
4649
using Org.BouncyCastle.Crypto;
4750
using Org.BouncyCastle.Pkcs;
4851
using Org.BouncyCastle.X509;
@@ -61,6 +64,29 @@ public static X509Certificate ReadPublicCertificate(Stream s) {
6164
public static ICipherParameters ReadPrivateKeyFromPkcs12KeyStore(Stream keyStore, String pkAlias, char[] pkPassword) {
6265
return new Pkcs12Store(keyStore, pkPassword).GetKey(pkAlias).Key;
6366
}
67+
68+
/// <summary>
69+
/// Creates <see cref="DerOutputStream"/> instance which can be an implementation of one of the ASN1 encodings
70+
/// writing logic. This method also asserts for unexpected ASN1 encodings.
71+
/// </summary>
72+
/// <param name="outputStream">the underlying stream</param>
73+
/// <param name="asn1Encoding">ASN1 encoding that will be used for writing. Only DER and BER are allowed as values.</param>
74+
/// <returns>a <see cref="DerOutputStream"/> instance. Exact stream implementation is chosen based on passed encoding.</returns>
75+
/// <exception cref="NotSupportedException"></exception>
76+
public static DerOutputStream CreateAsn1OutputStream(Stream outputStream, String asn1Encoding) {
77+
if (Asn1Encodable.Ber.Equals(asn1Encoding)) {
78+
return new Asn1OutputStream(outputStream);
79+
}
80+
81+
if (Asn1Encodable.Der.Equals(asn1Encoding)) {
82+
return new DerOutputStream(outputStream);
83+
}
84+
85+
throw new NotSupportedException(
86+
MessageFormatUtil.Format(KernelExceptionMessageConstant.UNSUPPORTED_ASN1_ENCODING, asn1Encoding)
87+
);
88+
}
89+
6490

6591
}
6692
}

itext/itext.kernel/itext/kernel/crypto/securityhandler/PubKeySecurityHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ source product.
5151
using Org.BouncyCastle.Security;
5252
using Org.BouncyCastle.X509;
5353
using iText.IO.Util;
54+
using iText.Kernel.Crypto;
5455
using iText.Kernel.Exceptions;
5556
using iText.Kernel.Pdf;
5657

@@ -207,7 +208,7 @@ private byte[] GetEncodedRecipient(int index) {
207208
pkcs7input[22] = two;
208209
pkcs7input[23] = one;
209210
MemoryStream baos = new MemoryStream();
210-
DerOutputStream k = DerOutputStream.Create(baos, Org.BouncyCastle.Asn1.Asn1Encodable.Der);
211+
DerOutputStream k = CryptoUtil.CreateAsn1OutputStream(baos, Org.BouncyCastle.Asn1.Asn1Encodable.Der);
211212
Asn1Object obj = CreateDERForRecipient(pkcs7input, (X509Certificate)certificate);
212213
k.WriteObject(obj);
213214
cms = baos.ToArray();

itext/itext.kernel/itext/kernel/exceptions/KernelExceptionMessageConstant.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,22 @@ public sealed class KernelExceptionMessageConstant {
2929

3030
public const String ANNOTATION_SHALL_HAVE_REFERENCE_TO_PAGE = "Annotation shall have reference to page.";
3131

32-
public const String APPEND_MODE_REQUIRES_A_DOCUMENT_WITHOUT_ERRORS_EVEN_IF_RECOVERY_IS_POSSIBLE = "Append "
33-
+ "mode requires a document without errors, even if recovery is possible.";
32+
public const String APPEND_MODE_REQUIRES_A_DOCUMENT_WITHOUT_ERRORS_EVEN_IF_RECOVERY_IS_POSSIBLE = "Append mode requires a document without errors, even if recovery is possible.";
3433

3534
public const String BAD_CERTIFICATE_AND_KEY = "Bad public key certificate and/or private key.";
3635

37-
public const String BAD_USER_PASSWORD = "Bad user password. Password is not provided or wrong password " +
38-
"provided. Correct password should be passed to PdfReader constructor with properties. " + "See ReaderProperties#setPassword() method.";
36+
public const String BAD_USER_PASSWORD = "Bad user password. Password is not provided or wrong password provided. Correct password should be passed "
37+
+ "to PdfReader constructor with properties. See ReaderProperties#setPassword() method.";
3938

4039
public const String CANNOT_ADD_KID_TO_THE_FLUSHED_ELEMENT = "Cannot add kid to the flushed element.";
4140

42-
public const String CANNOT_BE_EMBEDDED_DUE_TO_LICENSING_RESTRICTIONS = "{0} cannot be embedded due to " +
43-
"licensing restrictions.";
41+
public const String CANNOT_BE_EMBEDDED_DUE_TO_LICENSING_RESTRICTIONS = "{0} cannot be embedded due to licensing restrictions.";
4442

4543
public const String CANNOT_CLOSE_DOCUMENT = "Cannot close document.";
4644

47-
public const String CANNOT_CLOSE_DOCUMENT_WITH_ALREADY_FLUSHED_PDF_CATALOG = "Cannot close document with "
48-
+ "already flushed PDF Catalog.";
45+
public const String CANNOT_CLOSE_DOCUMENT_WITH_ALREADY_FLUSHED_PDF_CATALOG = "Cannot close document with already flushed PDF Catalog.";
4946

50-
public const String CANNOT_CONVERT_PDF_ARRAY_TO_AN_ARRAY_OF_BOOLEANS = "Cannot convert PdfArray to an " +
51-
"array of booleans";
47+
public const String CANNOT_CONVERT_PDF_ARRAY_TO_AN_ARRAY_OF_BOOLEANS = "Cannot convert PdfArray to an array of booleans";
5248

5349
public const String CANNOT_CONVERT_PDF_ARRAY_TO_DOUBLE_ARRAY = "Cannot convert PdfArray to an array " + "of doubles.";
5450

@@ -470,12 +466,14 @@ public const String CONTENT_STREAM_MUST_NOT_INVOKE_OPERATORS_THAT_SPECIFY_COLORS
470466

471467
public const String UNKNOWN_PDF_EXCEPTION = "Unknown PdfException.";
472468

469+
public const String UNSUPPORTED_ASN1_ENCODING = "Unknown ASN1-encoding {0}. Only DER and BER encodings are supported!";
470+
473471
public const String UNSUPPORTED_FONT_EMBEDDING_STRATEGY = "Unsupported font embedding strategy.";
474472

475473
public const String UNSUPPORTED_XOBJECT_TYPE = "Unsupported XObject type.";
476474

477475
public const String WHEN_ADDING_OBJECT_REFERENCE_TO_THE_TAG_TREE_IT_MUST_BE_CONNECTED_TO_NOT_FLUSHED_OBJECT
478-
= "" + "When adding object reference to the tag tree, it must be connected to not flushed object.";
476+
= "When adding object reference to the tag tree, it must be connected to not flushed object.";
479477

480478
public const String WHITE_POINT_IS_INCORRECTLY_SPECIFIED = "White point is incorrectly specified.";
481479

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6919b5c97acb1eb2784a38f17730272d732e8b21
1+
3578748626f7e3c0b910f50baebb96f5783e3c02

0 commit comments

Comments
 (0)