jdk/test/sun/security/x509/AlgorithmId/NonStandardNames.java
author weijun
Sat, 21 Jul 2012 19:56:53 +0800
changeset 13361 bda5c2354fc6
child 14182 3041082abb40
permissions -rw-r--r--
7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13361
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     1
/*
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     4
 *
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     8
 *
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    13
 * accompanied this code).
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    14
 *
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    18
 *
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    21
 * questions.
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    22
 */
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    23
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    24
/*
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    25
 * @test
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    26
 * @bug 7180907
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    27
 * @summary Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    28
 */
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    29
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    30
import java.security.MessageDigest;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    31
import java.security.Signature;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    32
import java.security.cert.X509Certificate;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    33
import sun.security.pkcs.ContentInfo;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    34
import sun.security.pkcs.PKCS7;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    35
import sun.security.pkcs.PKCS9Attribute;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    36
import sun.security.pkcs.PKCS9Attributes;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    37
import sun.security.pkcs.SignerInfo;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    38
import sun.security.tools.CertAndKeyGen;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    39
import sun.security.x509.AlgorithmId;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    40
import sun.security.x509.X500Name;
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    41
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    42
public class NonStandardNames {
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    43
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    45
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    46
        byte[] data = "Hello".getBytes();
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    47
        X500Name n = new X500Name("cn=Me");
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    48
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    49
        CertAndKeyGen cakg = new CertAndKeyGen("RSA", "SHA256withRSA");
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    50
        cakg.generate(1024);
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    51
        X509Certificate cert = cakg.getSelfCertificate(n, 1000);
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    52
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    53
        MessageDigest md = MessageDigest.getInstance("SHA-256");
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    54
        PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[]{
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    55
            new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID),
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    56
            new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)),
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    57
        });
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    58
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    59
        Signature s = Signature.getInstance("SHA256withRSA");
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    60
        s.initSign(cakg.getPrivateKey());
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    61
        s.update(authed.getDerEncoding());
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    62
        byte[] sig = s.sign();
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    63
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    64
        SignerInfo signerInfo = new SignerInfo(
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    65
                n,
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    66
                cert.getSerialNumber(),
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    67
                AlgorithmId.get("SHA-256"),
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    68
                authed,
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    69
                AlgorithmId.get("SHA256withRSA"),
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    70
                sig,
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    71
                null
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    72
                );
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    73
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    74
        PKCS7 pkcs7 = new PKCS7(
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    75
                new AlgorithmId[] {signerInfo.getDigestAlgorithmId()},
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    76
                new ContentInfo(data),
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    77
                new X509Certificate[] {cert},
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    78
                new SignerInfo[] {signerInfo});
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    79
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    80
        if (pkcs7.verify(signerInfo, data) == null) {
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    81
            throw new Exception("Not verified");
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    82
        }
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    83
    }
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents:
diff changeset
    84
}