bc-java
bc-java copied to clipboard
PKCS10CertificationRequest getRequestedExtensions using reference equality
https://github.com/bcgit/bc-java/blob/d98b7cd8a05ced51c272c99a8706a7a9b4e88a46/pkix/src/main/java/org/bouncycastle/pkcs/PKCS10CertificationRequest.java#L239
== Compares memory locations of objects in Java where .equals() compares the actual objects themselves. This code becomes problematic when the object identifier specified is equal to PKCSObjectIdentifiers.pkcs_9_at_extensionRequest but not actually the same in memory:
import lombok.SneakyThrows;
import lombok.val;
import org.apache.commons.lang3.ObjectUtils;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
import javax.security.auth.x500.X500Principal;
import java.security.KeyPairGenerator;
import java.util.Arrays;
import java.util.Objects;
public class GetRequestedExtensionsTest {
@SneakyThrows
public static void main(final String[] args) {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
val keyPair = keyPairGenerator.generateKeyPair();
val contentSigner = new JcaContentSignerBuilder("SHA256withRSA").build(keyPair.getPrivate());
val certificateRequestBuilder = new JcaPKCS10CertificationRequestBuilder(
new X500Principal("CN=Subject"), keyPair.getPublic());
certificateRequestBuilder.addAttribute(new ASN1ObjectIdentifier("1.2.840.113549.1.9.14"), new DERSequence(new ASN1EncodableVector(0)));
System.out.println("== Orig ==");
val certificateRequestOrig = certificateRequestBuilder.build(contentSigner);
System.out.println("RequestedExtensions Present: " + Objects.nonNull(certificateRequestOrig.getRequestedExtensions()));
Arrays.stream(certificateRequestOrig.getAttributes())
.forEach(attribute -> {
System.out.println(attribute.getAttrType().getId() + " : " + ObjectUtils.identityToString(attribute.getAttrType()));
System.out.println(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest.getId() + " : " + ObjectUtils.identityToString(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest));
});
System.out.println("== Regen ==");
val certificateRequestRegen = new PKCS10CertificationRequest(certificateRequestOrig.getEncoded());
System.out.println("RequestedExtensions Present: " + Objects.nonNull(certificateRequestRegen.getRequestedExtensions()));
Arrays.stream(certificateRequestRegen.getAttributes())
.forEach(attribute -> {
System.out.println(attribute.getAttrType().getId() + " : " + ObjectUtils.identityToString(attribute.getAttrType()));
System.out.println(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest.getId() + " : " + ObjectUtils.identityToString(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest));
});
}
}
yields
== Orig ==
RequestedExtensions Present: false
1.2.840.113549.1.9.14 : org.bouncycastle.asn1.ASN1ObjectIdentifier@4e4aea35
1.2.840.113549.1.9.14 : org.bouncycastle.asn1.ASN1ObjectIdentifier@1442d7b5
== Regen ==
RequestedExtensions Present: true
1.2.840.113549.1.9.14 : org.bouncycastle.asn1.ASN1ObjectIdentifier@1442d7b5
1.2.840.113549.1.9.14 : org.bouncycastle.asn1.ASN1ObjectIdentifier@1442d7b5
The fix should be as simple as changing the offending line to:
if (PKCSObjectIdentifiers.pkcs_9_at_extensionRequest.equals(encodable.getAttrType()))
Thanks, Kyle
Fixed with https://github.com/bcgit/bc-java/pull/1303