Skip to content

Commit 3578748

Browse files
yulian-gaponenkoUbuntu
authored andcommitted
Refactor code in order to faciliate autoporting
DEVSIX-6119
1 parent 6919b5c commit 3578748

File tree

4 files changed

+122
-28
lines changed

4 files changed

+122
-28
lines changed

kernel/src/main/java/com/itextpdf/kernel/crypto/CryptoUtil.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,31 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.kernel.crypto;
4444

45+
import com.itextpdf.commons.utils.MessageFormatUtil;
46+
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
47+
4548
import java.io.IOException;
4649
import java.io.InputStream;
50+
import java.io.OutputStream;
4751
import java.security.GeneralSecurityException;
4852
import java.security.KeyStore;
4953
import java.security.PrivateKey;
5054
import java.security.cert.Certificate;
5155
import java.security.cert.CertificateException;
5256
import java.security.cert.CertificateFactory;
57+
import org.bouncycastle.asn1.ASN1Encoding;
58+
import org.bouncycastle.asn1.ASN1OutputStream;
5359

5460
/**
5561
* This file is a helper class for internal usage only.
56-
* Be aware that it's API and functionality may be changed in future.
62+
* Be aware that it's API and functionality may be changed in the future.
5763
*/
5864
public class CryptoUtil {
65+
66+
private CryptoUtil() {
67+
68+
}
69+
5970
public static Certificate readPublicCertificate(InputStream is) throws CertificateException {
6071
return CertificateFactory.getInstance("X.509").generateCertificate(is);
6172
}
@@ -65,4 +76,22 @@ public static PrivateKey readPrivateKeyFromPKCS12KeyStore(InputStream keyStore,
6576
keystore.load(keyStore, pkPassword);
6677
return (PrivateKey) keystore.getKey(pkAlias, pkPassword);
6778
}
79+
80+
/**
81+
* Creates {@link ASN1OutputStream} instance and asserts for unexpected ASN1 encodings.
82+
*
83+
* @param outputStream the underlying stream
84+
* @param asn1Encoding ASN1 encoding that will be used for writing. Only DER and BER are allowed as values.
85+
* See also {@link ASN1Encoding}.
86+
*
87+
* @return an {@link ASN1OutputStream} instance. Exact stream implementation is chosen based on passed encoding.
88+
*/
89+
public static ASN1OutputStream createAsn1OutputStream(OutputStream outputStream, String asn1Encoding) {
90+
if (!ASN1Encoding.DER.equals(asn1Encoding) && !ASN1Encoding.BER.equals(asn1Encoding)) {
91+
throw new UnsupportedOperationException(
92+
MessageFormatUtil.format(KernelExceptionMessageConstant.UNSUPPORTED_ASN1_ENCODING, asn1Encoding)
93+
);
94+
}
95+
return ASN1OutputStream.create(outputStream, asn1Encoding);
96+
}
6897
}

kernel/src/main/java/com/itextpdf/kernel/crypto/securityhandler/PubKeySecurityHandler.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,26 @@ This file is part of the iText (R) project.
4444
package com.itextpdf.kernel.crypto.securityhandler;
4545

4646
import com.itextpdf.io.util.StreamUtil;
47-
import com.itextpdf.kernel.exceptions.PdfException;
47+
import com.itextpdf.kernel.crypto.CryptoUtil;
4848
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
49+
import com.itextpdf.kernel.exceptions.PdfException;
4950
import com.itextpdf.kernel.pdf.PdfArray;
5051
import com.itextpdf.kernel.pdf.PdfDictionary;
5152
import com.itextpdf.kernel.pdf.PdfLiteral;
5253
import com.itextpdf.kernel.pdf.PdfName;
5354
import com.itextpdf.kernel.security.IExternalDecryptionProcess;
55+
56+
import java.io.ByteArrayInputStream;
57+
import java.io.ByteArrayOutputStream;
58+
import java.io.IOException;
59+
import java.security.GeneralSecurityException;
60+
import java.security.Key;
61+
import java.security.MessageDigest;
62+
import java.security.PrivateKey;
63+
import java.security.cert.Certificate;
64+
import java.security.cert.X509Certificate;
65+
import java.util.ArrayList;
66+
import java.util.List;
5467
import org.bouncycastle.asn1.ASN1Encoding;
5568
import org.bouncycastle.asn1.ASN1InputStream;
5669
import org.bouncycastle.asn1.ASN1OutputStream;
@@ -69,18 +82,6 @@ This file is part of the iText (R) project.
6982
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
7083
import org.bouncycastle.asn1.x509.TBSCertificateStructure;
7184

72-
import java.io.ByteArrayInputStream;
73-
import java.io.ByteArrayOutputStream;
74-
import java.io.IOException;
75-
import java.security.GeneralSecurityException;
76-
import java.security.Key;
77-
import java.security.MessageDigest;
78-
import java.security.PrivateKey;
79-
import java.security.cert.Certificate;
80-
import java.security.cert.X509Certificate;
81-
import java.util.ArrayList;
82-
import java.util.List;
83-
8485
/**
8586
* @author Aiken Sam ([email protected])
8687
*/
@@ -251,7 +252,7 @@ private byte[] getEncodedRecipient(int index) throws IOException, GeneralSecurit
251252
pkcs7input[23] = one;
252253

253254
ByteArrayOutputStream baos = new ByteArrayOutputStream();
254-
ASN1OutputStream k = ASN1OutputStream.create(baos, ASN1Encoding.DER);
255+
ASN1OutputStream k = CryptoUtil.createAsn1OutputStream(baos, ASN1Encoding.DER);
255256
ASN1Primitive obj = createDERForRecipient(pkcs7input, (X509Certificate) certificate);
256257
k.writeObject(obj);
257258
cms = baos.toByteArray();

kernel/src/main/java/com/itextpdf/kernel/exceptions/KernelExceptionMessageConstant.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public final class KernelExceptionMessageConstant {
2929
public static final String AMOUNT_OF_BYTES_LESS_THAN_ZERO =
3030
"Amount of bytes in the PDF document cannot be less than zero";
3131
public static final String ANNOTATION_SHALL_HAVE_REFERENCE_TO_PAGE = "Annotation shall have reference to page.";
32-
public static final 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 static final String APPEND_MODE_REQUIRES_A_DOCUMENT_WITHOUT_ERRORS_EVEN_IF_RECOVERY_IS_POSSIBLE =
33+
"Append mode requires a document without errors, even if recovery is possible.";
3434
public static final String BAD_CERTIFICATE_AND_KEY = "Bad public key certificate and/or private key.";
35-
public static final String BAD_USER_PASSWORD = "Bad user password. Password is not provided or wrong password "
36-
+ "provided. Correct password should be passed to PdfReader constructor with properties. "
37-
+ "See ReaderProperties#setPassword() method.";
35+
public static final String BAD_USER_PASSWORD =
36+
"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.";
3838
public static final String CANNOT_ADD_KID_TO_THE_FLUSHED_ELEMENT = "Cannot add kid to the flushed element.";
39-
public static final String CANNOT_BE_EMBEDDED_DUE_TO_LICENSING_RESTRICTIONS = "{0} cannot be embedded due to "
40-
+ "licensing restrictions.";
39+
public static final String CANNOT_BE_EMBEDDED_DUE_TO_LICENSING_RESTRICTIONS =
40+
"{0} cannot be embedded due to licensing restrictions.";
4141
public static final String CANNOT_CLOSE_DOCUMENT = "Cannot close document.";
42-
public static final String CANNOT_CLOSE_DOCUMENT_WITH_ALREADY_FLUSHED_PDF_CATALOG = "Cannot close document with "
43-
+ "already flushed PDF Catalog.";
44-
public static final String CANNOT_CONVERT_PDF_ARRAY_TO_AN_ARRAY_OF_BOOLEANS = "Cannot convert PdfArray to an "
45-
+ "array of booleans";
42+
public static final String CANNOT_CLOSE_DOCUMENT_WITH_ALREADY_FLUSHED_PDF_CATALOG =
43+
"Cannot close document with already flushed PDF Catalog.";
44+
public static final String CANNOT_CONVERT_PDF_ARRAY_TO_AN_ARRAY_OF_BOOLEANS =
45+
"Cannot convert PdfArray to an array of booleans";
4646
public static final String CANNOT_CONVERT_PDF_ARRAY_TO_DOUBLE_ARRAY = "Cannot convert PdfArray to an array "
4747
+ "of doubles.";
4848
public static final String CANNOT_CONVERT_PDF_ARRAY_TO_INT_ARRAY = "Cannot convert PdfArray to an array "
@@ -318,10 +318,12 @@ public final class KernelExceptionMessageConstant {
318318
public static final String UNKNOWN_ENCRYPTION_TYPE_V = "Unknown encryption type V == {0}.";
319319
public static final String UNKNOWN_GRAPHICS_STATE_DICTIONARY = "{0} is an unknown graphics state dictionary.";
320320
public static final String UNKNOWN_PDF_EXCEPTION = "Unknown PdfException.";
321+
public static final String UNSUPPORTED_ASN1_ENCODING =
322+
"Unknown ASN1-encoding {0}. Only DER and BER encodings are supported!";
321323
public static final String UNSUPPORTED_FONT_EMBEDDING_STRATEGY = "Unsupported font embedding strategy.";
322324
public static final String UNSUPPORTED_XOBJECT_TYPE = "Unsupported XObject type.";
323-
public static final String WHEN_ADDING_OBJECT_REFERENCE_TO_THE_TAG_TREE_IT_MUST_BE_CONNECTED_TO_NOT_FLUSHED_OBJECT = ""
324-
+ "When adding object reference to the tag tree, it must be connected to not flushed object.";
325+
public static final String WHEN_ADDING_OBJECT_REFERENCE_TO_THE_TAG_TREE_IT_MUST_BE_CONNECTED_TO_NOT_FLUSHED_OBJECT =
326+
"When adding object reference to the tag tree, it must be connected to not flushed object.";
325327
public static final String WHITE_POINT_IS_INCORRECTLY_SPECIFIED = "White point is incorrectly specified.";
326328
public static final String WMF_IMAGE_EXCEPTION = "WMF image exception.";
327329
public static final String WRONG_MEDIA_BOX_SIZE_TOO_FEW_ARGUMENTS = "Wrong media box size: {0}. Need at least 4 "
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
package com.itextpdf.kernel.crypto;
24+
25+
import com.itextpdf.commons.utils.MessageFormatUtil;
26+
import com.itextpdf.io.source.ByteArrayOutputStream;
27+
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
28+
import com.itextpdf.test.ExtendedITextTest;
29+
import com.itextpdf.test.annotations.type.UnitTest;
30+
31+
import org.bouncycastle.asn1.ASN1Encoding;
32+
import org.bouncycastle.asn1.ASN1OutputStream;
33+
import org.junit.Assert;
34+
import org.junit.Test;
35+
import org.junit.experimental.categories.Category;
36+
37+
@Category(UnitTest.class)
38+
public class CryptoUtilTest extends ExtendedITextTest {
39+
@Test
40+
public void createBerStreamTest() {
41+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
42+
ASN1OutputStream stream = CryptoUtil.createAsn1OutputStream(baos, ASN1Encoding.BER);
43+
Assert.assertNotNull(stream);
44+
}
45+
46+
@Test
47+
public void createDerStreamTest() {
48+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
49+
ASN1OutputStream stream = CryptoUtil.createAsn1OutputStream(baos, ASN1Encoding.DER);
50+
Assert.assertNotNull(stream);
51+
}
52+
53+
@Test
54+
public void createUnsupportedEncodingStreamTest() {
55+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
56+
Exception e = Assert.assertThrows(UnsupportedOperationException.class,
57+
() -> CryptoUtil.createAsn1OutputStream(baos, "DL")
58+
);
59+
Assert.assertEquals(MessageFormatUtil.format(KernelExceptionMessageConstant.UNSUPPORTED_ASN1_ENCODING, "DL"),
60+
e.getMessage());
61+
}
62+
}

0 commit comments

Comments
 (0)