Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8277224: sun.security.pkcs.PKCS9Attributes.toString() throws NPE
Backport-of: 6bb04626af6574ef1e8d4b7dad0389d4b59f5d08
  • Loading branch information
coffeys committed Nov 19, 2021
1 parent 6aa02d7 commit 2d69b2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Expand Up @@ -277,11 +277,13 @@ public PKCS9Attribute getAttribute(String name) {
*/
public PKCS9Attribute[] getAttributes() {
PKCS9Attribute[] attribs = new PKCS9Attribute[attributes.size()];
ObjectIdentifier oid;

int j = 0;
for (int i=1; i < PKCS9Attribute.PKCS9_OIDS.length &&
j < attribs.length; i++) {
if (PKCS9Attribute.PKCS9_OIDS[i] == null) {
continue;
}
attribs[j] = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]);

if (attribs[j] != null)
Expand Down Expand Up @@ -325,11 +327,13 @@ public String toString() {
StringBuilder sb = new StringBuilder(200);
sb.append("PKCS9 Attributes: [\n\t");

ObjectIdentifier oid;
PKCS9Attribute value;

boolean first = true;
for (int i = 1; i < PKCS9Attribute.PKCS9_OIDS.length; i++) {
if (PKCS9Attribute.PKCS9_OIDS[i] == null) {
continue;
}
value = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]);

if (value == null) continue;
Expand All @@ -340,7 +344,7 @@ public String toString() {
else
sb.append(";\n\t");

sb.append(value.toString());
sb.append(value);
}

sb.append("\n\t] (end PKCS9 Attributes)");
Expand Down
9 changes: 7 additions & 2 deletions test/jdk/sun/security/x509/AlgorithmId/NonStandardNames.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, 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 All @@ -23,7 +23,7 @@

/*
* @test
* @bug 7180907
* @bug 7180907 8277224
* @summary Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
* @modules java.base/sun.security.pkcs
* java.base/sun.security.tools.keytool
Expand Down Expand Up @@ -60,8 +60,13 @@ public static void main(String[] args) throws Exception {
PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[]{
new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID),
new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)),
new PKCS9Attribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID, "test".getBytes())
});

// test PKCS9Attributes.toString(), PKCS9Attributes.getAttributes()
System.out.println(authed);
authed.getAttributes();

Signature s = Signature.getInstance("SHA256withRSA");
s.initSign(cakg.getPrivateKey());
s.update(authed.getDerEncoding());
Expand Down

1 comment on commit 2d69b2d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.