Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/hotspot/share/classfile/classFileParser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -3350,8 +3350,7 @@ u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* cons
CHECK_0);

RecordComponent* record_component =
RecordComponent::allocate(_loader_data, name_index, descriptor_index,
attributes_count, generic_sig_index,
RecordComponent::allocate(_loader_data, name_index, descriptor_index, generic_sig_index,
annotations, type_annotations, CHECK_0);
record_components->at_put(x, record_component);
} // End of component processing loop
Expand Down
6 changes: 2 additions & 4 deletions src/hotspot/share/oops/recordComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,12 +33,11 @@

RecordComponent* RecordComponent::allocate(ClassLoaderData* loader_data,
u2 name_index, u2 descriptor_index,
u2 attributes_count,
u2 generic_signature_index,
AnnotationArray* annotations,
AnnotationArray* type_annotations, TRAPS) {
return new (loader_data, size(), MetaspaceObj::RecordComponentType, THREAD)
RecordComponent(name_index, descriptor_index, attributes_count,
RecordComponent(name_index, descriptor_index,
generic_signature_index, annotations, type_annotations);
}

Expand All @@ -65,7 +64,6 @@ void RecordComponent::print_value_on(outputStream* st) const {
void RecordComponent::print_on(outputStream* st) const {
st->print("name_index: %d", _name_index);
st->print(" - descriptor_index: %d", _descriptor_index);
st->print(" - attributes_count: %d", _attributes_count);
if (_generic_signature_index != 0) {
st->print(" - generic_signature_index: %d", _generic_signature_index);
}
Expand Down
12 changes: 3 additions & 9 deletions src/hotspot/share/oops/recordComponent.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -36,25 +36,21 @@ class RecordComponent: public MetaspaceObj {
AnnotationArray* _type_annotations;
u2 _name_index;
u2 _descriptor_index;
u2 _attributes_count;

// generic_signature_index gets set if the Record component has a Signature
// attribute. A zero value indicates that there was no Signature attribute.
u2 _generic_signature_index;

public:
RecordComponent(u2 name_index, u2 descriptor_index, u2 attributes_count,
u2 generic_signature_index, AnnotationArray* annotations,
AnnotationArray* type_annotations):
RecordComponent(u2 name_index, u2 descriptor_index, u2 generic_signature_index,
AnnotationArray* annotations, AnnotationArray* type_annotations):
_annotations(annotations), _type_annotations(type_annotations),
_name_index(name_index), _descriptor_index(descriptor_index),
_attributes_count(attributes_count),
_generic_signature_index(generic_signature_index) { }

// Allocate instance of this class
static RecordComponent* allocate(ClassLoaderData* loader_data,
u2 name_index, u2 descriptor_index,
u2 attributes_count,
u2 generic_signature_index,
AnnotationArray* annotations,
AnnotationArray* type_annotations, TRAPS);
Expand All @@ -69,8 +65,6 @@ class RecordComponent: public MetaspaceObj {
_descriptor_index = descriptor_index;
}

u2 attributes_count() const { return _attributes_count; }

u2 generic_signature_index() const { return _generic_signature_index; }
void set_generic_signature_index(u2 generic_signature_index) {
_generic_signature_index = generic_signature_index;
Expand Down
7 changes: 5 additions & 2 deletions src/hotspot/share/prims/jvmtiClassFileReconstituter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ void JvmtiClassFileReconstituter::write_record_attribute() {
RecordComponent* component = components->at(x);
if (component->generic_signature_index() != 0) {
length += 8; // Signature attribute size
assert(component->attributes_count() > 0, "Bad component attributes count");
}
if (component->annotations() != nullptr) {
length += 6 + component->annotations()->length();
Expand All @@ -509,7 +508,11 @@ void JvmtiClassFileReconstituter::write_record_attribute() {
RecordComponent* component = components->at(i);
write_u2(component->name_index());
write_u2(component->descriptor_index());
write_u2(component->attributes_count());
u2 attributes_count = (component->generic_signature_index() != 0 ? 1 : 0)
+ (component->annotations() != nullptr ? 1 : 0)
+ (component->type_annotations() != nullptr ? 1 : 0);

write_u2(attributes_count);
if (component->generic_signature_index() != 0) {
write_signature_attribute(component->generic_signature_index());
}
Expand Down
49 changes: 41 additions & 8 deletions src/java.base/share/classes/sun/security/pkcs/PKCS7.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -530,8 +530,23 @@ public void encodeSignedData(DerOutputStream out)
* @exception SignatureException on signature handling errors.
*/
public SignerInfo verify(SignerInfo info, byte[] bytes)
throws NoSuchAlgorithmException, SignatureException {
return info.verify(this, bytes);
throws NoSuchAlgorithmException, SignatureException {
return info.verify(this, bytes, null);
}

/**
* This verifies a given SignerInfo.
*
* @param info the signer information.
* @param bytes the DER encoded content information.
* @param cert certificate used to verify; find one inside the block if null
*
* @exception NoSuchAlgorithmException on unrecognized algorithms.
* @exception SignatureException on signature handling errors.
*/
public SignerInfo verify(SignerInfo info, byte[] bytes, X509Certificate cert)
throws NoSuchAlgorithmException, SignatureException {
return info.verify(this, bytes, cert);
}

/**
Expand Down Expand Up @@ -726,6 +741,19 @@ public boolean isOldStyle() {
return this.oldStyle;
}

// Generate signed data without a specified digAlgID.
public static byte[] generateNewSignedData(
String sigalg, Provider sigProvider,
PrivateKey privateKey, X509Certificate[] signerChain,
byte[] content, boolean internalsf, boolean directsign,
Function<byte[], PKCS9Attributes> ts)
throws SignatureException, InvalidKeyException, IOException,
NoSuchAlgorithmException {
return generateSignedData(sigalg, sigProvider, privateKey, signerChain,
content, internalsf, directsign,
null, ts);
}

/**
* Generate a PKCS7 data block.
*
Expand All @@ -736,34 +764,39 @@ public boolean isOldStyle() {
* @param content the content to sign
* @param internalsf whether the content should be included in output
* @param directsign if the content is signed directly or through authattrs
* @param digAlgID digest alg to use; derive from other arguments if null
* @param ts (optional) timestamper
* @return the pkcs7 output in an array
* @throws SignatureException if signing failed
* @throws InvalidKeyException if key cannot be used
* @throws IOException should not happen here, all byte array
* @throws NoSuchAlgorithmException if siglag is bad
*/
public static byte[] generateNewSignedData(
public static byte[] generateSignedData(
String sigalg, Provider sigProvider,
PrivateKey privateKey, X509Certificate[] signerChain,
byte[] content, boolean internalsf, boolean directsign,
AlgorithmId digAlgID,
Function<byte[], PKCS9Attributes> ts)
throws SignatureException, InvalidKeyException, IOException,
NoSuchAlgorithmException {

Signature signer = SignatureUtil.fromKey(sigalg, privateKey, sigProvider);

AlgorithmId digAlgID = SignatureUtil.getDigestAlgInPkcs7SignerInfo(
signer, sigalg, privateKey, directsign);
if (digAlgID == null) {
digAlgID = SignatureUtil.getDigestAlgInPkcs7SignerInfo(
signer, sigalg, privateKey, directsign);
}
AlgorithmId sigAlgID = SignatureUtil.fromSignature(signer, privateKey);

PKCS9Attributes authAttrs = null;
if (!directsign) {
// MessageDigest
byte[] md;
String digAlgName = digAlgID.getName();
if (digAlgName.equals("SHAKE256") || digAlgName.equals("SHAKE256-LEN")) {
// No MessageDigest impl for SHAKE256 yet
if (digAlgName.equals("SHAKE256-LEN")) {
// We don't check the LEN here. Usually it is returned
// by SignatureUtil.getDigestAlgInPkcs7SignerInfo
var shaker = new SHAKE256(64);
shaker.update(content, 0, content.length);
md = shaker.digest();
Expand Down
124 changes: 76 additions & 48 deletions src/java.base/share/classes/sun/security/pkcs/SignerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,20 @@ public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
return certList;
}

/* Returns null if verify fails, this signerInfo if
verify succeeds. */
SignerInfo verify(PKCS7 block, byte[] data)
throws NoSuchAlgorithmException, SignatureException {
/**
* Verify this signerInfo in a PKCS7 block.
*
* @param block the PKCS7 object
* @param data the content to verify against; read from block if null
* @param cert certificate to verify with; read from block if null
* @return null if verify fails, this signerInfo if verify succeeds.
*/
SignerInfo verify(PKCS7 block, byte[] data, X509Certificate cert)
throws NoSuchAlgorithmException, SignatureException {

try {
Timestamp timestamp = null;
try {
timestamp = getTimestamp();
getTimestamp();
} catch (Exception e) {
// Log exception and continue. This allows for the case
// where, if there are no other errors, the code is
Expand Down Expand Up @@ -371,22 +376,19 @@ SignerInfo verify(PKCS7 block, byte[] data)
return null;

byte[] computedMessageDigest;
if (digestAlgName.equals("SHAKE256")
|| digestAlgName.equals("SHAKE256-LEN")) {
if (digestAlgName.equals("SHAKE256-LEN")) {
// RFC8419: for EdDSA in CMS, the id-shake256-len
// algorithm id must contain parameter value 512
// encoded as a positive integer value
byte[] params = digestAlgorithmId.getEncodedParams();
if (params == null) {
throw new SignatureException(
"id-shake256-len oid missing length");
}
int v = new DerValue(params).getInteger();
if (v != 512) {
throw new SignatureException(
"Unsupported id-shake256-" + v);
}
if (digestAlgName.equals("SHAKE256-LEN")) {
// RFC8419: for EdDSA in CMS, the id-shake256-len
// algorithm id must contain parameter value 512
// encoded as a positive integer value
byte[] params = digestAlgorithmId.getEncodedParams();
if (params == null) {
throw new SignatureException(
"id-shake256-len oid missing length");
}
int v = new DerValue(params).getInteger();
if (v != 512) {
throw new SignatureException(
"Unsupported id-shake256-" + v);
}
var md = new SHAKE256(64);
md.update(data, 0, data.length);
Expand Down Expand Up @@ -426,9 +428,11 @@ SignerInfo verify(PKCS7 block, byte[] data)
"SignerInfo digestEncryptionAlgorithm field", true));
}

X509Certificate cert = getCertificate(block);
if (cert == null) {
return null;
cert = getCertificate(block);
if (cert == null) {
return null;
}
}
PublicKey key = cert.getPublicKey();

Expand Down Expand Up @@ -492,9 +496,9 @@ SignerInfo verify(PKCS7 block, byte[] data)
* The digest algorithm is in the form "DIG", and the encryption
* algorithm can be in any of the 3 forms:
*
* 1. Old style key algorithm like RSA, DSA, EC, this method returns
* 1. Simple key algorithm like RSA, DSA, EC, this method returns
* DIGwithKEY.
* 2. New style signature algorithm in the form of HASHwithKEY, this
* 2. Traditional signature algorithm in the form of HASHwithKEY, this
* method returns DIGwithKEY. Please note this is not HASHwithKEY.
* 3. Modern signature algorithm like RSASSA-PSS and EdDSA, this method
* returns the signature algorithm itself but ensures digAlgId is
Expand Down Expand Up @@ -522,53 +526,77 @@ public static String makeSigAlg(AlgorithmId digAlgId, AlgorithmId encAlgId,
}

if (!AlgorithmId.get(spec.getDigestAlgorithm()).equals(digAlgId)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm");
throw new NoSuchAlgorithmException("Incompatible digest algorithm " + digAlgId);
}
return encAlg;
case "Ed25519":
if (!digAlgId.equals(SignatureUtil.EdDSADigestAlgHolder.sha512)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm");
if (!digAlgId.equalsOID(AlgorithmId.SHA512_oid)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm " + digAlgId);
}
return encAlg;
case "Ed448":
if (directSign) {
if (!digAlgId.equals(SignatureUtil.EdDSADigestAlgHolder.shake256)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm");
if (!digAlgId.equalsOID(AlgorithmId.SHAKE256_512_oid)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm " + digAlgId);
}
} else {
if (!digAlgId.equals(SignatureUtil.EdDSADigestAlgHolder.shake256$512)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm");
if (!digAlgId.equals(SignatureUtil.DigestAlgHolder.shake256lenWith512)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm " + digAlgId);
}
}
return encAlg;
case "ML-DSA-44":
// Following 3 from Table 1 inside
// https://datatracker.ietf.org/doc/html/rfc9882#name-signerinfo-content
if (!digAlgId.equalsOID(AlgorithmId.SHA256_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA384_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA512_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA3_256_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA3_384_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA3_512_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHAKE128_256_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHAKE256_512_oid)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm " + digAlgId);
}
return encAlg;
case "ML-DSA-65":
if (!digAlgId.equalsOID(AlgorithmId.SHA384_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA512_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA3_384_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA3_512_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHAKE256_512_oid)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm " + digAlgId);
}
return encAlg;
case "ML-DSA-87":
if (!digAlgId.equalsOID(AlgorithmId.SHA512_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHA3_512_oid)
&& !digAlgId.equalsOID(AlgorithmId.SHAKE256_512_oid)) {
throw new NoSuchAlgorithmException("Incompatible digest algorithm " + digAlgId);
}
return encAlg;
default:
String digAlg = digAlgId.getName();
String keyAlg = SignatureUtil.extractKeyAlgFromDwithE(encAlg);
if (keyAlg == null) {
// The encAlg used to be only the key alg
keyAlg = encAlg;
if (keyAlg == null) { // No "WITH" inside
if (encAlg.equals("RSA") || encAlg.equals("DSA") || encAlg.equals("EC")) {
keyAlg = encAlg; // Sometimes encAlgId is just the enc alg
} else {
return encAlg; // Must be a modern algorithm like EdDSA or ML-DSA
}
}
String digAlg = digAlgId.getName();
if (digAlg.startsWith("SHA-")) {
digAlg = "SHA" + digAlg.substring(4);
}
if (keyAlg.equals("EC")) keyAlg = "ECDSA";
String sigAlg = digAlg + "with" + keyAlg;
try {
Signature.getInstance(sigAlg);
return sigAlg;
} catch (NoSuchAlgorithmException e) {
// Possibly an unknown modern signature algorithm,
// in this case, encAlg should already be a signature
// algorithm.
return encAlg;
}
return digAlg + "with" + keyAlg;
}
}

/* Verify the content of the pkcs7 block. */
SignerInfo verify(PKCS7 block)
throws NoSuchAlgorithmException, SignatureException {
return verify(block, null);
return verify(block, null, null);
}

public BigInteger getVersion() {
Expand Down
Loading
Loading