Skip to content

Commit 84ae14f

Browse files
committed
Move DigestAlgorithms and OID to kernel, merge SecurityIDs into OID
DEVSIX-8565
1 parent 0acb474 commit 84ae14f

File tree

76 files changed

+476
-452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+476
-452
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ This file is part of the iText (R) project.
3131

3232
import java.io.InputStream;
3333
import java.io.OutputStream;
34+
import java.security.MessageDigest;
35+
import java.security.NoSuchAlgorithmException;
36+
import java.security.NoSuchProviderException;
3437
import java.security.cert.Certificate;
3538
import java.security.cert.CertificateException;
3639
import java.security.cert.CertificateFactory;
@@ -69,4 +72,13 @@ public static IASN1OutputStream createAsn1OutputStream(OutputStream outputStream
6972
}
7073
return BOUNCY_CASTLE_FACTORY.createASN1OutputStream(outputStream, asn1Encoding);
7174
}
75+
76+
static MessageDigest getMessageDigest(String hashAlgorithm, String provider)
77+
throws NoSuchAlgorithmException, NoSuchProviderException {
78+
if (provider == null || provider.startsWith("SunPKCS11") || provider.startsWith("SunMSCAPI")) {
79+
return MessageDigest.getInstance(DigestAlgorithms.normalizeDigestName(hashAlgorithm));
80+
} else {
81+
return MessageDigest.getInstance(hashAlgorithm, provider);
82+
}
83+
}
7284
}

sign/src/main/java/com/itextpdf/signatures/DigestAlgorithms.java renamed to kernel/src/main/java/com/itextpdf/kernel/crypto/DigestAlgorithms.java

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ This file is part of the iText (R) project.
2020
You should have received a copy of the GNU Affero General Public License
2121
along with this program. If not, see <https://www.gnu.org/licenses/>.
2222
*/
23-
package com.itextpdf.signatures;
23+
package com.itextpdf.kernel.crypto;
2424

2525
import com.itextpdf.bouncycastleconnector.BouncyCastleFactoryCreator;
2626
import com.itextpdf.commons.bouncycastle.IBouncyCastleFactory;
27-
import com.itextpdf.signatures.exceptions.SignExceptionMessageConstant;
28-
import com.itextpdf.signatures.logs.SignLogMessageConstant;
29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
27+
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
28+
import com.itextpdf.kernel.logs.KernelLogMessageConstant;
3129

3230
import java.io.IOException;
3331
import java.io.InputStream;
@@ -37,6 +35,8 @@ This file is part of the iText (R) project.
3735
import java.security.NoSuchProviderException;
3836
import java.util.HashMap;
3937
import java.util.Map;
38+
import org.slf4j.Logger;
39+
import org.slf4j.LoggerFactory;
4040

4141
/**
4242
* Class that contains a map with the different message digest algorithms.
@@ -123,10 +123,10 @@ public class DigestAlgorithms {
123123
digestNames.put("1.2.840.113549.2.5", "MD5");
124124
digestNames.put("1.2.840.113549.2.2", "MD2");
125125
digestNames.put("1.3.14.3.2.26", "SHA1");
126-
digestNames.put("2.16.840.1.101.3.4.2.4", "SHA224");
127-
digestNames.put("2.16.840.1.101.3.4.2.1", "SHA256");
128-
digestNames.put("2.16.840.1.101.3.4.2.2", "SHA384");
129-
digestNames.put("2.16.840.1.101.3.4.2.3", "SHA512");
126+
digestNames.put(OID.SHA_224, "SHA224");
127+
digestNames.put(OID.SHA_256, "SHA256");
128+
digestNames.put(OID.SHA_384, "SHA384");
129+
digestNames.put(OID.SHA_512, "SHA512");
130130
digestNames.put("1.3.36.3.2.2", "RIPEMD128");
131131
digestNames.put("1.3.36.3.2.1", "RIPEMD160");
132132
digestNames.put("1.3.36.3.2.3", "RIPEMD256");
@@ -137,8 +137,6 @@ public class DigestAlgorithms {
137137
digestNames.put("1.2.840.113549.1.1.11", "SHA256");
138138
digestNames.put("1.2.840.113549.1.1.12", "SHA384");
139139
digestNames.put("1.2.840.113549.1.1.13", "SHA512");
140-
digestNames.put("1.2.840.113549.2.5", "MD5");
141-
digestNames.put("1.2.840.113549.2.2", "MD2");
142140
digestNames.put("1.2.840.10040.4.3", "SHA1");
143141
digestNames.put("2.16.840.1.101.3.4.3.1", "SHA224");
144142
digestNames.put("2.16.840.1.101.3.4.3.2", "SHA256");
@@ -148,11 +146,11 @@ public class DigestAlgorithms {
148146
digestNames.put("1.3.36.3.3.1.2", "RIPEMD160");
149147
digestNames.put("1.3.36.3.3.1.4", "RIPEMD256");
150148
digestNames.put("1.2.643.2.2.9", "GOST3411");
151-
digestNames.put("2.16.840.1.101.3.4.2.7", "SHA3-224");
152-
digestNames.put("2.16.840.1.101.3.4.2.8", "SHA3-256");
153-
digestNames.put("2.16.840.1.101.3.4.2.9", "SHA3-384");
154-
digestNames.put("2.16.840.1.101.3.4.2.10", "SHA3-512");
155-
digestNames.put("2.16.840.1.101.3.4.2.12", "SHAKE256");
149+
digestNames.put(OID.SHA3_224, "SHA3-224");
150+
digestNames.put(OID.SHA3_256, "SHA3-256");
151+
digestNames.put(OID.SHA3_384, "SHA3-384");
152+
digestNames.put(OID.SHA3_512, "SHA3-512");
153+
digestNames.put(OID.SHAKE_256, "SHAKE256");
156154

157155
fixNames.put("SHA256", SHA256);
158156
fixNames.put("SHA384", SHA384);
@@ -164,26 +162,26 @@ public class DigestAlgorithms {
164162
allowedDigests.put("MD-5", "1.2.840.113549.2.5");
165163
allowedDigests.put("SHA1", "1.3.14.3.2.26");
166164
allowedDigests.put("SHA-1", "1.3.14.3.2.26");
167-
allowedDigests.put("SHA224", "2.16.840.1.101.3.4.2.4");
168-
allowedDigests.put("SHA-224", "2.16.840.1.101.3.4.2.4");
169-
allowedDigests.put("SHA256", "2.16.840.1.101.3.4.2.1");
170-
allowedDigests.put("SHA-256", "2.16.840.1.101.3.4.2.1");
171-
allowedDigests.put("SHA384", "2.16.840.1.101.3.4.2.2");
172-
allowedDigests.put("SHA-384", "2.16.840.1.101.3.4.2.2");
173-
allowedDigests.put("SHA512", "2.16.840.1.101.3.4.2.3");
174-
allowedDigests.put("SHA-512", "2.16.840.1.101.3.4.2.3");
165+
allowedDigests.put("SHA224", OID.SHA_224);
166+
allowedDigests.put("SHA-224", OID.SHA_224);
167+
allowedDigests.put("SHA256", OID.SHA_256);
168+
allowedDigests.put("SHA-256", OID.SHA_256);
169+
allowedDigests.put("SHA384", OID.SHA_384);
170+
allowedDigests.put("SHA-384", OID.SHA_384);
171+
allowedDigests.put("SHA512", OID.SHA_512);
172+
allowedDigests.put("SHA-512", OID.SHA_512);
175173
allowedDigests.put("RIPEMD128", "1.3.36.3.2.2");
176174
allowedDigests.put("RIPEMD-128", "1.3.36.3.2.2");
177175
allowedDigests.put("RIPEMD160", "1.3.36.3.2.1");
178176
allowedDigests.put("RIPEMD-160", "1.3.36.3.2.1");
179177
allowedDigests.put("RIPEMD256", "1.3.36.3.2.3");
180178
allowedDigests.put("RIPEMD-256", "1.3.36.3.2.3");
181179
allowedDigests.put("GOST3411", "1.2.643.2.2.9");
182-
allowedDigests.put("SHA3-224", "2.16.840.1.101.3.4.2.7");
183-
allowedDigests.put("SHA3-256", "2.16.840.1.101.3.4.2.8");
184-
allowedDigests.put("SHA3-384", "2.16.840.1.101.3.4.2.9");
185-
allowedDigests.put("SHA3-512", "2.16.840.1.101.3.4.2.10");
186-
allowedDigests.put("SHAKE256", "2.16.840.1.101.3.4.2.12");
180+
allowedDigests.put("SHA3-224", OID.SHA3_224);
181+
allowedDigests.put("SHA3-256", OID.SHA3_256);
182+
allowedDigests.put("SHA3-384", OID.SHA3_384);
183+
allowedDigests.put("SHA3-512", OID.SHA3_512);
184+
allowedDigests.put("SHAKE256", OID.SHAKE_256);
187185

188186
bitLengths.put("MD2", 128);
189187
bitLengths.put("MD-2", 128);
@@ -217,7 +215,9 @@ public class DigestAlgorithms {
217215
*
218216
* @param digestOid oid of the digest algorithm
219217
* @param provider the provider you want to use to create the hash
218+
*
220219
* @return MessageDigest object
220+
*
221221
* @throws NoSuchAlgorithmException thrown when a particular cryptographic algorithm is
222222
* requested but is not available in the environment
223223
* @throws NoSuchProviderException thrown when a particular security provider is
@@ -233,15 +233,17 @@ public static MessageDigest getMessageDigestFromOid(String digestOid, String pro
233233
*
234234
* @param hashAlgorithm the algorithm you want to use to create a hash
235235
* @param provider the provider you want to use to create the hash
236+
*
236237
* @return a MessageDigest object
238+
*
237239
* @throws NoSuchAlgorithmException thrown when a particular cryptographic algorithm is
238240
* requested but is not available in the environment
239241
* @throws NoSuchProviderException thrown when a particular security provider is
240242
* requested but is not available in the environment
241243
*/
242244
public static MessageDigest getMessageDigest(String hashAlgorithm, String provider)
243245
throws NoSuchAlgorithmException, NoSuchProviderException {
244-
return SignUtils.getMessageDigest(hashAlgorithm, provider);
246+
return CryptoUtil.getMessageDigest(hashAlgorithm, provider);
245247
}
246248

247249
/**
@@ -250,7 +252,9 @@ public static MessageDigest getMessageDigest(String hashAlgorithm, String provid
250252
* @param data the message of which you want to create a hash
251253
* @param hashAlgorithm the algorithm used to create the hash
252254
* @param provider the provider used to create the hash
255+
*
253256
* @return the hash
257+
*
254258
* @throws GeneralSecurityException when requested cryptographic algorithm or security provider
255259
* is not available
256260
* @throws IOException signals that an I/O exception has occurred
@@ -262,11 +266,13 @@ public static byte[] digest(InputStream data, String hashAlgorithm, String provi
262266
}
263267

264268
/**
265-
* Create a digest based on the inputstream.
269+
* Create a digest based on the input stream.
266270
*
267271
* @param data data to be digested
268272
* @param messageDigest algorithm to be used
273+
*
269274
* @return digest of the data
275+
*
270276
* @throws IOException signals that an I/O exception has occurred
271277
*/
272278
public static byte[] digest(InputStream data, MessageDigest messageDigest)
@@ -291,7 +297,7 @@ public static String getDigest(String oid) {
291297
if (ret == null) {
292298
try {
293299
String digest = getMessageDigest(oid, BOUNCY_CASTLE_FACTORY.getProviderName()).getAlgorithm();
294-
LOGGER.warn(SignLogMessageConstant.ALGORITHM_NOT_FROM_SPEC);
300+
LOGGER.warn(KernelLogMessageConstant.ALGORITHM_NOT_FROM_SPEC);
295301
return digest;
296302
} catch (Exception e) {
297303
return oid;
@@ -305,6 +311,7 @@ public static String getDigest(String oid) {
305311
* Normalize the digest name.
306312
*
307313
* @param algo the name to be normalized
314+
*
308315
* @return normalized name
309316
*/
310317
public static String normalizeDigestName(String algo) {
@@ -318,21 +325,22 @@ public static String normalizeDigestName(String algo) {
318325
* Returns the id of a digest algorithms that is allowed in PDF,
319326
* or null if it isn't allowed.
320327
*
321-
* @param name The name of the digest algorithm.
322-
* @return An oid.
328+
* @param name the name of the digest algorithm
329+
*
330+
* @return an oid
323331
*/
324332
public static String getAllowedDigest(String name) {
325333
if (name == null) {
326334
throw new IllegalArgumentException(
327-
SignExceptionMessageConstant.THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL);
335+
KernelExceptionMessageConstant.THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL);
328336
}
329337
String allowedDigest = allowedDigests.get(name.toUpperCase());
330338
if (allowedDigest != null) {
331339
return allowedDigest;
332340
}
333341
allowedDigest = BOUNCY_CASTLE_FACTORY.getDigestAlgorithmOid(name.toUpperCase());
334342
if (allowedDigest != null) {
335-
LOGGER.warn(SignLogMessageConstant.ALGORITHM_NOT_FROM_SPEC);
343+
LOGGER.warn(KernelLogMessageConstant.ALGORITHM_NOT_FROM_SPEC);
336344
}
337345
return allowedDigest;
338346
}
@@ -341,12 +349,13 @@ public static String getAllowedDigest(String name) {
341349
* Retrieve the output length in bits of the given digest algorithm.
342350
*
343351
* @param name the name of the digest algorithm
352+
*
344353
* @return the length of the output of the algorithm in bits
345354
*/
346355
public static int getOutputBitLength(String name) {
347356
if (name == null) {
348357
throw new IllegalArgumentException(
349-
SignExceptionMessageConstant.THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL);
358+
KernelExceptionMessageConstant.THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL);
350359
}
351360
return bitLengths.get(name).intValue();
352361
}

sign/src/main/java/com/itextpdf/signatures/OID.java renamed to kernel/src/main/java/com/itextpdf/kernel/crypto/OID.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This file is part of the iText (R) project.
2020
You should have received a copy of the GNU Affero General Public License
2121
along with this program. If not, see <https://www.gnu.org/licenses/>.
2222
*/
23-
package com.itextpdf.signatures;
23+
package com.itextpdf.kernel.crypto;
2424

2525
import java.util.Arrays;
2626
import java.util.Collections;
@@ -31,6 +31,45 @@ This file is part of the iText (R) project.
3131
* Class containing all the OID values used by iText.
3232
*/
3333
public final class OID {
34+
public static final String PKCS7_DATA = "1.2.840.113549.1.7.1";
35+
public static final String PKCS7_SIGNED_DATA = "1.2.840.113549.1.7.2";
36+
public static final String RSA = "1.2.840.113549.1.1.1";
37+
public static final String RSASSA_PSS = "1.2.840.113549.1.1.10";
38+
public static final String RSA_WITH_SHA256 = "1.2.840.113549.1.1.11";
39+
public static final String AA_SIGNING_CERTIFICATE_V1 = "1.2.840.113549.1.9.16.2.12";
40+
public static final String AA_SIGNING_CERTIFICATE_V2 = "1.2.840.113549.1.9.16.2.47";
41+
public static final String MGF1 = "1.2.840.113549.1.1.8";
42+
public static final String AA_TIME_STAMP_TOKEN = "1.2.840.113549.1.9.16.2.14";
43+
public static final String AUTHENTICATED_DATA = "1.2.840.113549.1.9.16.1.2";
44+
public static final String CONTENT_TYPE = "1.2.840.113549.1.9.3";
45+
public static final String MESSAGE_DIGEST = "1.2.840.113549.1.9.4";
46+
public static final String SIGNING_TIME = "1.2.840.113549.1.9.5";
47+
public static final String CMS_ALGORITHM_PROTECTION = "1.2.840.113549.1.9.52";
48+
public static final String DSA = "1.2.840.10040.4.1";
49+
public static final String ECDSA = "1.2.840.10045.2.1";
50+
public static final String ADBE_REVOCATION = "1.2.840.113583.1.1.8";
51+
public static final String TSA = "1.2.840.113583.1.1.9.1";
52+
53+
public static final String RSA_WITH_SHA3_512 = "2.16.840.1.101.3.4.3.16";
54+
public static final String SHA_224 = "2.16.840.1.101.3.4.2.4";
55+
public static final String SHA_256 = "2.16.840.1.101.3.4.2.1";
56+
public static final String SHA_384 = "2.16.840.1.101.3.4.2.2";
57+
public static final String SHA_512 = "2.16.840.1.101.3.4.2.3";
58+
public static final String SHA3_224 = "2.16.840.1.101.3.4.2.7";
59+
public static final String SHA3_256 = "2.16.840.1.101.3.4.2.8";
60+
public static final String SHA3_384 = "2.16.840.1.101.3.4.2.9";
61+
public static final String SHA3_512 = "2.16.840.1.101.3.4.2.10";
62+
public static final String SHAKE_256 = "2.16.840.1.101.3.4.2.12";
63+
64+
public static final String ED25519 = "1.3.101.112";
65+
public static final String ED448 = "1.3.101.113";
66+
public static final String OCSP = "1.3.6.1.5.5.7.48.1";
67+
public static final String CA_ISSUERS = "1.3.6.1.5.5.7.48.2";
68+
public static final String RI_OCSP_RESPONSE = "1.3.6.1.5.5.7.16.2";
69+
70+
public static final String KDF_PDF_MAC_WRAP_KDF = "1.0.32004.1.1";
71+
public static final String CT_PDF_MAC_INTEGRITY_INFO = "1.0.32004.1.0";
72+
3473

3574
private OID() {
3675
// Empty on purpose. Avoiding instantiation of this class.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ public final class KernelExceptionMessageConstant {
371371
+ "TagTreePointer is in invalid state: it points at removed element use TagTreePointer#moveToRoot.";
372372
public static final String THERE_IS_NO_ASSOCIATE_PDF_WRITER_FOR_MAKING_INDIRECTS = "There is no associate "
373373
+ "PdfWriter for making indirects.";
374+
public static final String THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL = "The name of the digest algorithm is null.";
374375
public static final String THIS_DECODE_PARAMETER_TYPE_IS_NOT_SUPPORTED = "Decode parameter type {0} is not "
375376
+ "supported.";
376377
public static final String THIS_FILTER_IS_NOT_SUPPORTED = "Filter {0} is not supported.";

kernel/src/main/java/com/itextpdf/kernel/logs/KernelLogMessageConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public final class KernelLogMessageConstant {
104104

105105
public static final String FINGERPRINT_DISABLED_BUT_NO_REQUIRED_LICENCE = "Fingerprint disabling is only " +
106106
"available in non AGPL mode. Fingerprint will be added at the end of the document.";
107+
public static final String ALGORITHM_NOT_FROM_SPEC =
108+
"Requested algorithm might not be supported by the pdf specification.";
107109

108110
private KernelLogMessageConstant() {
109111
//Private constructor will prevent the instantiation of this class directly

0 commit comments

Comments
 (0)