src/java.base/share/classes/sun/security/x509/X509CertImpl.java
author darcy
Wed, 09 Oct 2019 09:57:41 -0700
changeset 58519 6e017b301287
parent 57950 4612a3cfb927
child 58679 9c3209ff7550
child 59024 b046ba510bbc
permissions -rw-r--r--
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes Reviewed-by: mullan, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53351
bdb29aa5fd31 8215694: keytool cannot generate RSASSA-PSS certificates
weijun
parents: 53082
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.*;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
    37
import java.security.spec.AlgorithmParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.*;
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
    41
import java.util.concurrent.ConcurrentHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.provider.X509Factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The X509CertImpl class represents an X.509 certificate. These certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * are widely used to support authentication and other functionality in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Internet security systems.  Common applications include Privacy Enhanced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Mail (PEM), Transport Layer Security (SSL), code signing for trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * software distribution, and Secure Electronic Transactions (SET).  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * is a commercial infrastructure ready to manage large scale deployments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * of X.509 identity certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <P>These certificates are managed and vouched for by <em>Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Authorities</em> (CAs).  CAs are services which create certificates by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * placing data in the X.509 standard format and then digitally signing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * that data.  Such signatures are quite difficult to forge.  CAs act as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * trusted third parties, making introductions between agents who have no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * direct knowledge of each other.  CA certificates are either signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * themselves, or by some other CA such as a "root" CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
53082
4c539cb11633 8214532: Update RFC 2459 references in javadoc to RFC 5280
coffeys
parents: 53018
diff changeset
    65
 * <P> Standards relating to X.509 Public Key Infrastructure for the Internet
4c539cb11633 8214532: Update RFC 2459 references in javadoc to RFC 5280
coffeys
parents: 53018
diff changeset
    66
 * can be referenced in RFC 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author Dave Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see X509CertInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
58519
6e017b301287 8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents: 57950
diff changeset
    73
@SuppressWarnings("serial") // See writeReplace method in Certificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
public class X509CertImpl extends X509Certificate implements DerEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 54483
diff changeset
    76
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final long serialVersionUID = -3457612960190864406L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
    79
    private static final char DOT = '.';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Public attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static final String NAME = "x509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final String INFO = X509CertInfo.NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final String ALG_ID = "algorithm";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final String SIGNATURE = "signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static final String SIGNED_CERT = "signed_cert";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The following are defined for ease-of-use. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * are the most frequently retrieved attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // x509.info.subject.dname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final String SUBJECT_DN = NAME + DOT + INFO + DOT +
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
    95
                               X509CertInfo.SUBJECT + DOT + X509CertInfo.DN_NAME;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // x509.info.issuer.dname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static final String ISSUER_DN = NAME + DOT + INFO + DOT +
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
    98
                               X509CertInfo.ISSUER + DOT + X509CertInfo.DN_NAME;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // x509.info.serialNumber.number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final String SERIAL_ID = NAME + DOT + INFO + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                               X509CertInfo.SERIAL_NUMBER + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                               CertificateSerialNumber.NUMBER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // x509.info.key.value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static final String PUBLIC_KEY = NAME + DOT + INFO + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                               X509CertInfo.KEY + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                               CertificateX509Key.KEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // x509.info.version.value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final String VERSION = NAME + DOT + INFO + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                               X509CertInfo.VERSION + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                               CertificateVersion.VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // x509.algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static final String SIG_ALG = NAME + DOT + ALG_ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // x509.signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static final String SIG = NAME + DOT + SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // when we sign and decode we set this to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // this is our means to make certificates immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private boolean readOnly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // Certificate data, and its envelope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private byte[]              signedCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected X509CertInfo      info = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected AlgorithmId       algId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected byte[]            signature = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // recognized extension OIDS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final String KEY_USAGE_OID = "2.5.29.15";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static final String EXTENDED_KEY_USAGE_OID = "2.5.29.37";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static final String BASIC_CONSTRAINT_OID = "2.5.29.19";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static final String SUBJECT_ALT_NAME_OID = "2.5.29.17";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final String ISSUER_ALT_NAME_OID = "2.5.29.18";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static final String AUTH_INFO_ACCESS_OID = "1.3.6.1.5.5.7.1.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // number of standard key usage bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static final int NUM_STANDARD_KEY_USAGE = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // SubjectAlterntativeNames cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private Collection<List<?>> subjectAlternativeNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // IssuerAlternativeNames cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private Collection<List<?>> issuerAlternativeNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // ExtendedKeyUsage cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private List<String> extKeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // AuthorityInformationAccess cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private Set<AccessDescription> authInfoAccess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * PublicKey that has previously been used to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * the signature of this certificate. Null if the certificate has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * yet been verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private PublicKey verifiedPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * If verifiedPublicKey is not null, name of the provider used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * successfully verify the signature of this certificate, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * empty String if no provider was explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private String verifiedProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * If verifiedPublicKey is not null, result of the verification using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * verifiedPublicKey and verifiedProvider. If true, verification was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * successful, if false, it failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private boolean verificationResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public X509CertImpl() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Unmarshals a certificate from its encoded form, parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * encoded bytes.  This form of constructor is used by agents which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * need to examine and use certificate contents.  That is, this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * one of the more commonly used constructors.  Note that the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * must include only a certificate, and no "garbage" may be left at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * the end.  If you need to ignore data at the end of a certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * use another constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param certData the encoded bytes, with no trailing padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception CertificateException on parsing and initialization errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public X509CertImpl(byte[] certData) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            parse(new DerValue(certData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            signedCert = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   193
            throw new CertificateException("Unable to initialize, " + e, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * unmarshals an X.509 certificate from an input stream.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * certificate is RFC1421 hex-encoded, then it must begin with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * the line X509Factory.BEGIN_CERT and end with the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * X509Factory.END_CERT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param in an input stream holding at least one certificate that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *        be either DER-encoded or RFC1421 hex-encoded version of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *        DER-encoded certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @exception CertificateException on parsing and initialization errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public X509CertImpl(InputStream in) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        DerValue der = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        BufferedInputStream inBuffered = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // First try reading stream as HEX-encoded DER-encoded bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // since not mistakable for raw DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            inBuffered.mark(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            der = readRFC1421Cert(inBuffered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                // Next, try reading stream as raw DER-encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                inBuffered.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                der = new DerValue(inBuffered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            } catch (IOException ioe1) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   225
                throw new CertificateException("Input stream must be " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   226
                                               "either DER-encoded bytes " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   227
                                               "or RFC1421 hex-encoded " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   228
                                               "DER-encoded bytes: " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   229
                                               ioe1.getMessage(), ioe1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            parse(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            signedCert = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   236
            throw new CertificateException("Unable to parse DER value of " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   237
                                           "certificate, " + ioe, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * read input stream as HEX-encoded DER-encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param in InputStream to read
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 29596
diff changeset
   245
     * @return DerValue corresponding to decoded HEX-encoded bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws IOException if stream can not be interpreted as RFC1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *                     encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private DerValue readRFC1421Cert(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        DerValue der = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        String line = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        BufferedReader certBufferedReader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            new BufferedReader(new InputStreamReader(in, "ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            line = certBufferedReader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (IOException ioe1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new IOException("Unable to read InputStream: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                  ioe1.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (line.equals(X509Factory.BEGIN_CERT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            /* stream appears to be hex-encoded bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            ByteArrayOutputStream decstream = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                while ((line = certBufferedReader.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    if (line.equals(X509Factory.END_CERT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        der = new DerValue(decstream.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    } else {
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 25859
diff changeset
   269
                        decstream.write(Pem.decode(line));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } catch (IOException ioe2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                throw new IOException("Unable to read InputStream: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                      + ioe2.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new IOException("InputStream is not RFC1421 hex-encoded " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                  "DER bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Construct an initialized X509 Certificate. The certificate is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * in raw form and has to be signed to be useful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 29596
diff changeset
   287
     * @param certInfo the X509CertificateInfo which the Certificate is to be
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 29596
diff changeset
   288
     *             created from.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public X509CertImpl(X509CertInfo certInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        this.info = certInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Unmarshal a certificate from its encoded form, parsing a DER value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * This form of constructor is used by agents which need to examine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * and use certificate contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param derVal the der value containing the encoded cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @exception CertificateException on parsing and initialization errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public X509CertImpl(DerValue derVal) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            parse(derVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            signedCert = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   307
            throw new CertificateException("Unable to initialize, " + e, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Appends the certificate to an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param out an input stream to which the certificate is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception CertificateEncodingException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void encode(OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (signedCert == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new CertificateEncodingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                          "Null certificate to encode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            out.write(signedCert.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new CertificateEncodingException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * DER encode this object onto an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Implements the <code>DerEncoder</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param out the output stream on which to write the DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @exception IOException on encoding error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void derEncode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (signedCert == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new IOException("Null certificate to encode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        out.write(signedCert.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Returns the encoded form of this certificate. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * assumed that each certificate type would have only a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * form of encoding; for example, X.509 certificates would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * be encoded as ASN.1 DER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @exception CertificateEncodingException if an encoding error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public byte[] getEncoded() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return getEncodedInternal().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returned the encoding as an uncloned byte array. Callers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * guarantee that they neither modify it nor expose it to untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public byte[] getEncodedInternal() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (signedCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new CertificateEncodingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                          "Null certificate to encode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return signedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Throws an exception if the certificate was not signed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * verification key provided.  Successfully verifying a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * does <em>not</em> indicate that one should trust the entity which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * it represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param key the public key used for verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @exception NoSuchProviderException if there's no default provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public void verify(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    throws CertificateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        InvalidKeyException, NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        verify(key, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Throws an exception if the certificate was not signed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * verification key provided.  Successfully verifying a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * does <em>not</em> indicate that one should trust the entity which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * it represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param key the public key used for verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @param sigProvider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @exception NoSuchProviderException on incorrect provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public synchronized void verify(PublicKey key, String sigProvider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throws CertificateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            InvalidKeyException, NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (sigProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            sigProvider = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if ((verifiedPublicKey != null) && verifiedPublicKey.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            // this certificate has already been verified using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            // this public key. Make sure providers match, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (sigProvider.equals(verifiedProvider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (verificationResult) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    throw new SignatureException("Signature does not match.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (signedCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new CertificateEncodingException("Uninitialized certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Verify the signature ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Signature sigVerf = null;
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   427
        String sigName = algId.getName();
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 50204
diff changeset
   428
        if (sigProvider.isEmpty()) {
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   429
            sigVerf = Signature.getInstance(sigName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } else {
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   431
            sigVerf = Signature.getInstance(sigName, sigProvider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   433
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   434
        try {
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   435
            SignatureUtil.initVerifyWithParam(sigVerf, key,
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   436
                SignatureUtil.getParamSpec(sigName, getSigAlgParams()));
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   437
        } catch (ProviderException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   438
            throw new CertificateException(e.getMessage(), e.getCause());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   439
        } catch (InvalidAlgorithmParameterException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   440
            throw new CertificateException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   441
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   442
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        byte[] rawCert = info.getEncodedInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        sigVerf.update(rawCert, 0, rawCert.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // verify may throw SignatureException for invalid encodings, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        verificationResult = sigVerf.verify(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        verifiedPublicKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        verifiedProvider = sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (verificationResult == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw new SignatureException("Signature does not match.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
13406
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   457
     * Throws an exception if the certificate was not signed using the
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   458
     * verification key provided.  This method uses the signature verification
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   459
     * engine supplied by the specified provider. Note that the specified
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   460
     * Provider object does not have to be registered in the provider list.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   461
     * Successfully verifying a certificate does <em>not</em> indicate that one
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   462
     * should trust the entity which it represents.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   463
     *
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   464
     * @param key the public key used for verification.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   465
     * @param sigProvider the provider.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   466
     *
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   467
     * @exception NoSuchAlgorithmException on unsupported signature
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   468
     * algorithms.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   469
     * @exception InvalidKeyException on incorrect key.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   470
     * @exception SignatureException on signature errors.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   471
     * @exception CertificateException on encoding errors.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   472
     */
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   473
    public synchronized void verify(PublicKey key, Provider sigProvider)
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   474
            throws CertificateException, NoSuchAlgorithmException,
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   475
            InvalidKeyException, SignatureException {
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   476
        if (signedCert == null) {
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   477
            throw new CertificateEncodingException("Uninitialized certificate");
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   478
        }
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   479
        // Verify the signature ...
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   480
        Signature sigVerf = null;
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   481
        String sigName = algId.getName();
13406
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   482
        if (sigProvider == null) {
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   483
            sigVerf = Signature.getInstance(sigName);
13406
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   484
        } else {
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   485
            sigVerf = Signature.getInstance(sigName, sigProvider);
13406
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   486
        }
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   487
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   488
        try {
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   489
            SignatureUtil.initVerifyWithParam(sigVerf, key,
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   490
                SignatureUtil.getParamSpec(sigName, getSigAlgParams()));
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   491
        } catch (ProviderException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   492
            throw new CertificateException(e.getMessage(), e.getCause());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   493
        } catch (InvalidAlgorithmParameterException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   494
            throw new CertificateException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   495
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   496
13406
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   497
        byte[] rawCert = info.getEncodedInfo();
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   498
        sigVerf.update(rawCert, 0, rawCert.length);
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   499
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   500
        // verify may throw SignatureException for invalid encodings, etc.
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   501
        verificationResult = sigVerf.verify(signature);
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   502
        verifiedPublicKey = key;
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   503
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   504
        if (verificationResult == false) {
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   505
            throw new SignatureException("Signature does not match.");
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   506
        }
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   507
    }
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   508
33d7bf574b42 7026347: Certificate and X509CRL should have verify(PublicKey key, Provider sigProvider)
mullan
parents: 13040
diff changeset
   509
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Creates an X.509 certificate, and signs it using the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * (associating a signature algorithm and an X.500 name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * This operation is used to implement the certificate generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * functionality of a certificate authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param key the private key used for signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param algorithm the name of the signature algorithm used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @exception NoSuchProviderException if there's no default provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public void sign(PrivateKey key, String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    throws CertificateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        InvalidKeyException, NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        sign(key, algorithm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Creates an X.509 certificate, and signs it using the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * (associating a signature algorithm and an X.500 name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * This operation is used to implement the certificate generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * functionality of a certificate authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param key the private key used for signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param algorithm the name of the signature algorithm used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @exception NoSuchProviderException on incorrect provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public void sign(PrivateKey key, String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    throws CertificateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        InvalidKeyException, NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        try {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   552
            sign(key, null, algorithm, provider);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   553
        } catch (InvalidAlgorithmParameterException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   554
            // should not happen; re-throw just in case
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   555
            throw new SignatureException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   556
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   557
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   558
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   559
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   560
     * Creates an X.509 certificate, and signs it using the given key
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   561
     * (associating a signature algorithm and an X.500 name), signature
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   562
     * parameters, and security provider. If the given provider name
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   563
     * is null or empty, the implementation look up will be based on
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   564
     * provider configurations.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   565
     * This operation is used to implement the certificate generation
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   566
     * functionality of a certificate authority.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   567
     *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   568
     * @param key the private key used for signing
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   569
     * @param signingParams the parameters used for signing
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   570
     * @param algorithm the name of the signature algorithm used
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   571
     * @param provider the name of the provider, may be null
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   572
     *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   573
     * @exception NoSuchAlgorithmException on unsupported signature
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   574
     *            algorithms
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   575
     * @exception InvalidKeyException on incorrect key
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   576
     * @exception InvalidAlgorithmParameterException on invalid signature
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   577
     *            parameters
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   578
     * @exception NoSuchProviderException on incorrect provider
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   579
     * @exception SignatureException on signature errors
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   580
     * @exception CertificateException on encoding errors
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   581
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   582
    public void sign(PrivateKey key, AlgorithmParameterSpec signingParams,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   583
            String algorithm, String provider)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   584
            throws CertificateException, NoSuchAlgorithmException,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   585
            InvalidKeyException, InvalidAlgorithmParameterException,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   586
            NoSuchProviderException, SignatureException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   587
        try {
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   588
            if (readOnly) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                throw new CertificateEncodingException(
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   590
                        "cannot over-write existing certificate");
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   591
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            Signature sigEngine = null;
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   593
            if (provider == null || provider.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                sigEngine = Signature.getInstance(algorithm);
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   595
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                sigEngine = Signature.getInstance(algorithm, provider);
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   597
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
54483
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   599
            SignatureUtil.initSignWithParam(sigEngine, key, signingParams,
ac20c3bdc55d 8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents: 53351
diff changeset
   600
                    null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   602
            // in case the name is reset
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   603
            if (signingParams != null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   604
                algId = AlgorithmId.get(sigEngine.getParameters());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   605
            } else {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   606
                algId = AlgorithmId.get(algorithm);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47478
diff changeset
   607
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            // encode certificate info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            info.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            byte[] rawCert = tmp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            // encode algorithm identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            algId.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            // Create and encode the signature itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            sigEngine.update(rawCert, 0, rawCert.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            signature = sigEngine.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            tmp.putBitString(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            // Wrap the signed data in a SEQUENCE { data, algorithm, sig }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            signedCert = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            readOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            throw new CertificateEncodingException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Checks that the certificate is currently valid, i.e. the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * time is within the specified validity period.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @exception CertificateExpiredException if the certificate has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @exception CertificateNotYetValidException if the certificate is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * yet valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public void checkValidity()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    throws CertificateExpiredException, CertificateNotYetValidException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        Date date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        checkValidity(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Checks that the specified date is within the certificate's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * validity period, or basically if the certificate would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * valid at the specified date/time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param date the Date to check against to see if this certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *        is valid at that date/time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @exception CertificateExpiredException if the certificate has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * with respect to the <code>date</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @exception CertificateNotYetValidException if the certificate is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * yet valid with respect to the <code>date</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    public void checkValidity(Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    throws CertificateExpiredException, CertificateNotYetValidException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        CertificateValidity interval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            interval = (CertificateValidity)info.get(CertificateValidity.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            throw new CertificateNotYetValidException("Incorrect validity period");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (interval == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            throw new CertificateNotYetValidException("Null validity period");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        interval.valid(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Return the requested attribute from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Note that the X509CertInfo is not cloned for performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Callers must ensure that they do not modify it. All other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * attributes are cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @param name the name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @exception CertificateParsingException on invalid attribute identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    public Object get(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        X509AttributeName attr = new X509AttributeName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        String id = attr.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (!(id.equalsIgnoreCase(NAME))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            throw new CertificateParsingException("Invalid root of "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                          + "attribute name, expected [" + NAME +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                          "], received " + "[" + id + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        attr = new X509AttributeName(attr.getSuffix());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        id = attr.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (id.equalsIgnoreCase(INFO)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (attr.getSuffix() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    return info.get(attr.getSuffix());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    throw new CertificateParsingException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    throw new CertificateParsingException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        } else if (id.equalsIgnoreCase(ALG_ID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            return(algId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        } else if (id.equalsIgnoreCase(SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (signature != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                return signature.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        } else if (id.equalsIgnoreCase(SIGNED_CERT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            if (signedCert != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                return signedCert.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            throw new CertificateParsingException("Attribute name not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                 + "recognized or get() not allowed for the same: " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Set the requested attribute in the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @param name the name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @param obj the value of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @exception CertificateException on invalid attribute identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @exception IOException on encoding error of attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public void set(String name, Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        // check if immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (readOnly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            throw new CertificateException("cannot over-write existing"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                           + " certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        X509AttributeName attr = new X509AttributeName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        String id = attr.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (!(id.equalsIgnoreCase(NAME))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            throw new CertificateException("Invalid root of attribute name,"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                           + " expected [" + NAME + "], received " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        attr = new X509AttributeName(attr.getSuffix());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        id = attr.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if (id.equalsIgnoreCase(INFO)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if (attr.getSuffix() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                if (!(obj instanceof X509CertInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    throw new CertificateException("Attribute value should"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                    + " be of type X509CertInfo.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                info = (X509CertInfo)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                signedCert = null;  //reset this as certificate data has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                info.set(attr.getSuffix(), obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                signedCert = null;  //reset this as certificate data has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            throw new CertificateException("Attribute name not recognized or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                              "set() not allowed for the same: " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * Delete the requested attribute from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @param name the name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @exception CertificateException on invalid attribute identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @exception IOException on other errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public void delete(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // check if immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (readOnly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            throw new CertificateException("cannot over-write existing"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                                           + " certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        X509AttributeName attr = new X509AttributeName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        String id = attr.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (!(id.equalsIgnoreCase(NAME))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            throw new CertificateException("Invalid root of attribute name,"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                                   + " expected ["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                   + NAME + "], received " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        attr = new X509AttributeName(attr.getSuffix());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        id = attr.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (id.equalsIgnoreCase(INFO)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (attr.getSuffix() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                info = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                info.delete(attr.getSuffix());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        } else if (id.equalsIgnoreCase(ALG_ID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            algId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        } else if (id.equalsIgnoreCase(SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            signature = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        } else if (id.equalsIgnoreCase(SIGNED_CERT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            signedCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            throw new CertificateException("Attribute name not recognized or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                              "delete() not allowed for the same: " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        elements.addElement(NAME + DOT + INFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        elements.addElement(NAME + DOT + ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        elements.addElement(NAME + DOT + SIGNATURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        elements.addElement(NAME + DOT + SIGNED_CERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        return elements.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        return(NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Returns a printable representation of the certificate.  This does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * contain all the information available to distinguish this from any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * other certificate.  The certificate must be fully constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * before this function may be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        if (info == null || algId == null || signature == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        HexDumpEncoder encoder = new HexDumpEncoder();
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   845
        return "[\n" + info + '\n' +
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   846
            "  Algorithm: [" + algId + "]\n" +
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   847
            "  Signature:\n" + encoder.encodeBuffer(signature) + "\n]";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    // the strongly typed gets, as per java.security.cert.X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Gets the publickey from this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @return the publickey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public PublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            PublicKey key = (PublicKey)info.get(CertificateX509Key.NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                + DOT + CertificateX509Key.KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Gets the version number from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @return the version number, i.e. 1, 2 or 3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public int getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            int vers = ((Integer)info.get(CertificateVersion.NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                        + DOT + CertificateVersion.VERSION)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return vers+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * Gets the serial number from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @return the serial number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    public BigInteger getSerialNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        SerialNumber ser = getSerialNumberObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        return ser != null ? ser.getNumber() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * Gets the serial number from the certificate as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * a SerialNumber object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @return the serial number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public SerialNumber getSerialNumberObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            SerialNumber ser = (SerialNumber)info.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                              CertificateSerialNumber.NAME + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                              CertificateSerialNumber.NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
           return ser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Gets the subject distinguished name from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @return the subject name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public Principal getSubjectDN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        try {
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   926
            Principal subject = (Principal)info.get(X509CertInfo.SUBJECT + DOT +
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   927
                                                    X509CertInfo.DN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * Get subject name as X500Principal. Overrides implementation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * X509Certificate with a slightly more efficient version that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * also aware of X509CertImpl mutability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    public X500Principal getSubjectX500Principal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            X500Principal subject = (X500Principal)info.get(
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   945
                                            X509CertInfo.SUBJECT + DOT +
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   946
                                            "x500principal");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * Gets the issuer distinguished name from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @return the issuer name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    public Principal getIssuerDN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        try {
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   962
            Principal issuer = (Principal)info.get(X509CertInfo.ISSUER + DOT +
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   963
                                                   X509CertInfo.DN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            return issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * Get issuer name as X500Principal. Overrides implementation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * X509Certificate with a slightly more efficient version that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * also aware of X509CertImpl mutability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    public X500Principal getIssuerX500Principal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            X500Principal issuer = (X500Principal)info.get(
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   981
                                            X509CertInfo.ISSUER + DOT +
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14342
diff changeset
   982
                                            "x500principal");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            return issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * Gets the notBefore date from the validity period of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @return the start date of the validity period.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public Date getNotBefore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            Date d = (Date) info.get(CertificateValidity.NAME + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                                        CertificateValidity.NOT_BEFORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * Gets the notAfter date from the validity period of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @return the end date of the validity period.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    public Date getNotAfter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            Date d = (Date) info.get(CertificateValidity.NAME + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                                     CertificateValidity.NOT_AFTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * Gets the DER encoded certificate informations, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * <code>tbsCertificate</code> from this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * This can be used to verify the signature independently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @return the DER encoded certificate information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @exception CertificateEncodingException if an encoding error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    public byte[] getTBSCertificate() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            return info.getEncodedInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            throw new CertificateEncodingException("Uninitialized certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * Gets the raw Signature bits from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * @return the signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    public byte[] getSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        if (signature == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            return null;
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
  1046
        return signature.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * Gets the signature algorithm name for the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * For example, the string "SHA-1/DSA" or "DSS".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @return the signature algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    public String getSigAlgName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (algId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        return (algId.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * Gets the signature algorithm OID string from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * For example, the string "1.2.840.10040.4.3"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @return the signature algorithm oid string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public String getSigAlgOID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (algId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        ObjectIdentifier oid = algId.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return (oid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * Gets the DER encoded signature algorithm parameters from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * certificate's signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @return the DER encoded signature algorithm parameters, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *         null if no parameters are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    public byte[] getSigAlgParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (algId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            return algId.getEncodedParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * Gets the Issuer Unique Identity from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @return the Issuer Unique Identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    public boolean[] getIssuerUniqueID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            UniqueIdentity id = (UniqueIdentity)info.get(
13789
639c51fe428c 4647343: IDENT variable in sun.security.x509 classes not used
mullan
parents: 13406
diff changeset
  1102
                                 X509CertInfo.ISSUER_ID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            if (id == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                return (id.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * Gets the Subject Unique Identity from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @return the Subject Unique Identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    public boolean[] getSubjectUniqueID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            UniqueIdentity id = (UniqueIdentity)info.get(
13789
639c51fe428c 4647343: IDENT variable in sun.security.x509 classes not used
mullan
parents: 13406
diff changeset
  1122
                                 X509CertInfo.SUBJECT_ID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            if (id == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                return (id.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
16492
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1132
    public KeyIdentifier getAuthKeyId() {
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1133
        AuthorityKeyIdentifierExtension aki
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1134
            = getAuthorityKeyIdentifierExtension();
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1135
        if (aki != null) {
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1136
            try {
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1137
                return (KeyIdentifier)aki.get(
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1138
                    AuthorityKeyIdentifierExtension.KEY_ID);
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1139
            } catch (IOException ioe) {} // not possible
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1140
        }
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1141
        return null;
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1142
    }
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 16020
diff changeset
  1143
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    /**
21819
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1145
     * Returns the subject's key identifier, or null
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1146
     */
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1147
    public KeyIdentifier getSubjectKeyId() {
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1148
        SubjectKeyIdentifierExtension ski = getSubjectKeyIdentifierExtension();
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1149
        if (ski != null) {
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1150
            try {
22041
3638966a5c02 8030082: Fix raw types lint warnings, etc. in various sun.security libraries
darcy
parents: 21819
diff changeset
  1151
                return ski.get(SubjectKeyIdentifierExtension.KEY_ID);
21819
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1152
            } catch (IOException ioe) {} // not possible
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1153
        }
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1154
        return null;
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1155
    }
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1156
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 20177
diff changeset
  1157
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Get AuthorityKeyIdentifier extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @return AuthorityKeyIdentifier object or null (if no such object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * in certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    public AuthorityKeyIdentifierExtension getAuthorityKeyIdentifierExtension()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        return (AuthorityKeyIdentifierExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            getExtension(PKIXExtensions.AuthorityKey_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Get BasicConstraints extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @return BasicConstraints object or null (if no such object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    public BasicConstraintsExtension getBasicConstraintsExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        return (BasicConstraintsExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            getExtension(PKIXExtensions.BasicConstraints_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * Get CertificatePoliciesExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * @return CertificatePoliciesExtension or null (if no such object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    public CertificatePoliciesExtension getCertificatePoliciesExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        return (CertificatePoliciesExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            getExtension(PKIXExtensions.CertificatePolicies_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * Get ExtendedKeyUsage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @return ExtendedKeyUsage extension object or null (if no such object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * in certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    public ExtendedKeyUsageExtension getExtendedKeyUsageExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        return (ExtendedKeyUsageExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            getExtension(PKIXExtensions.ExtendedKeyUsage_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * Get IssuerAlternativeName extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @return IssuerAlternativeName object or null (if no such object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    public IssuerAlternativeNameExtension getIssuerAlternativeNameExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        return (IssuerAlternativeNameExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            getExtension(PKIXExtensions.IssuerAlternativeName_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Get NameConstraints extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @return NameConstraints object or null (if no such object in certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    public NameConstraintsExtension getNameConstraintsExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return (NameConstraintsExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            getExtension(PKIXExtensions.NameConstraints_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * Get PolicyConstraints extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @return PolicyConstraints object or null (if no such object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    public PolicyConstraintsExtension getPolicyConstraintsExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        return (PolicyConstraintsExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            getExtension(PKIXExtensions.PolicyConstraints_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * Get PolicyMappingsExtension extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * @return PolicyMappingsExtension object or null (if no such object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * in certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    public PolicyMappingsExtension getPolicyMappingsExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        return (PolicyMappingsExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            getExtension(PKIXExtensions.PolicyMappings_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * Get PrivateKeyUsage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @return PrivateKeyUsage object or null (if no such object in certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    public PrivateKeyUsageExtension getPrivateKeyUsageExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        return (PrivateKeyUsageExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            getExtension(PKIXExtensions.PrivateKeyUsage_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * Get SubjectAlternativeName extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * @return SubjectAlternativeName object or null (if no such object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    public SubjectAlternativeNameExtension getSubjectAlternativeNameExtension()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        return (SubjectAlternativeNameExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            getExtension(PKIXExtensions.SubjectAlternativeName_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * Get SubjectKeyIdentifier extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @return SubjectKeyIdentifier object or null (if no such object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    public SubjectKeyIdentifierExtension getSubjectKeyIdentifierExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        return (SubjectKeyIdentifierExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            getExtension(PKIXExtensions.SubjectKey_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * Get CRLDistributionPoints extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @return CRLDistributionPoints object or null (if no such object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    public CRLDistributionPointsExtension getCRLDistributionPointsExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        return (CRLDistributionPointsExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            getExtension(PKIXExtensions.CRLDistributionPoints_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * Return true if a critical extension is found that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * not supported, otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    public boolean hasUnsupportedCriticalExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            CertificateExtensions exts = (CertificateExtensions)info.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                                         CertificateExtensions.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            if (exts == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            return exts.hasUnsupportedCriticalExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * Gets a Set of the extension(s) marked CRITICAL in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * certificate. In the returned set, each extension is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * represented by its OID string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @return a set of the extension oid strings in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * certificate that are marked critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    public Set<String> getCriticalExtensionOIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            CertificateExtensions exts = (CertificateExtensions)info.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                                         CertificateExtensions.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            if (exts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            }
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1313
            Set<String> extSet = new TreeSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            for (Extension ex : exts.getAllExtensions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                if (ex.isCritical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    extSet.add(ex.getExtensionId().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            return extSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * Gets a Set of the extension(s) marked NON-CRITICAL in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * certificate. In the returned set, each extension is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * represented by its OID string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @return a set of the extension oid strings in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * certificate that are NOT marked critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    public Set<String> getNonCriticalExtensionOIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            CertificateExtensions exts = (CertificateExtensions)info.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                                         CertificateExtensions.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            if (exts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            }
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1343
            Set<String> extSet = new TreeSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            for (Extension ex : exts.getAllExtensions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                if (!ex.isCritical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    extSet.add(ex.getExtensionId().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            extSet.addAll(exts.getUnparseableExtensions().keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            return extSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * Gets the extension identified by the given ObjectIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @param oid the Object Identifier value for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * @return Extension or null if certificate does not contain this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *         extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    public Extension getExtension(ObjectIdentifier oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            CertificateExtensions extensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                extensions = (CertificateExtensions)info.get(CertificateExtensions.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            if (extensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            } else {
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1377
                Extension ex = extensions.getExtension(oid.toString());
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1378
                if (ex != null) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1379
                    return ex;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1380
                }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1381
                for (Extension ex2: extensions.getAllExtensions()) {
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30649
diff changeset
  1382
                    if (ex2.getExtensionId().equals(oid)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                        //XXXX May want to consider cloning this
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1384
                        return ex2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                /* no such extension in this certificate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    public Extension getUnparseableExtension(ObjectIdentifier oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            CertificateExtensions extensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                extensions = (CertificateExtensions)info.get(CertificateExtensions.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            if (extensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                return extensions.getUnparseableExtensions().get(oid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * Gets the DER encoded extension identified by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * oid String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * @param oid the Object Identifier value for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    public byte[] getExtensionValue(String oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            ObjectIdentifier findOID = new ObjectIdentifier(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            String extAlias = OIDMap.getName(findOID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            Extension certExt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            CertificateExtensions exts = (CertificateExtensions)info.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                                     CertificateExtensions.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            if (extAlias == null) { // may be unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                // get the extensions, search thru' for this oid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                if (exts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                for (Extension ex : exts.getAllExtensions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    ObjectIdentifier inCertOID = ex.getExtensionId();
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30649
diff changeset
  1438
                    if (inCertOID.equals(findOID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                        certExt = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            } else { // there's sub-class that can handle this extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                    certExt = (Extension)this.get(extAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    // get() throws an Exception instead of returning null, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            if (certExt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                if (exts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                    certExt = exts.getUnparseableExtensions().get(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                if (certExt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            byte[] extData = certExt.getExtensionValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            if (extData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            out.putOctetString(extData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * Get a boolean array representing the bits of the KeyUsage extension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * (oid = 2.5.29.15).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @return the bit values of this extension as an array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    public boolean[] getKeyUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            String extAlias = OIDMap.getName(PKIXExtensions.KeyUsage_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            if (extAlias == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            KeyUsageExtension certExt = (KeyUsageExtension)this.get(extAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            if (certExt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            boolean[] ret = certExt.getBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            if (ret.length < NUM_STANDARD_KEY_USAGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                boolean[] usageBits = new boolean[NUM_STANDARD_KEY_USAGE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                System.arraycopy(ret, 0, usageBits, 0, ret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                ret = usageBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * This method are the overridden implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * getExtendedKeyUsage method in X509Certificate in the Sun
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * provider. It is better performance-wise since it returns cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    public synchronized List<String> getExtendedKeyUsage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        if (readOnly && extKeyUsage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            return extKeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            ExtendedKeyUsageExtension ext = getExtendedKeyUsageExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            extKeyUsage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                Collections.unmodifiableList(ext.getExtendedKeyUsage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            return extKeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * This static method is the default implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * getExtendedKeyUsage method in X509Certificate. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * X509Certificate provider generally should overwrite this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * provide among other things caching for better performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    public static List<String> getExtendedKeyUsage(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            byte[] ext = cert.getExtensionValue(EXTENDED_KEY_USAGE_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            if (ext == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            DerValue val = new DerValue(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            byte[] data = val.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            ExtendedKeyUsageExtension ekuExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                new ExtendedKeyUsageExtension(Boolean.FALSE, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            return Collections.unmodifiableList(ekuExt.getExtendedKeyUsage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1537
            throw new CertificateParsingException(ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    /**
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
  1542
     * Get the certificate constraints path length from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * the critical BasicConstraints extension, (oid = 2.5.29.19).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * @return the length of the constraint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    public int getBasicConstraints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            String extAlias = OIDMap.getName(PKIXExtensions.BasicConstraints_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            if (extAlias == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            BasicConstraintsExtension certExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                        (BasicConstraintsExtension)this.get(extAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            if (certExt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            if (((Boolean)certExt.get(BasicConstraintsExtension.IS_CA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                 ).booleanValue() == true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                return ((Integer)certExt.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                        BasicConstraintsExtension.PATH_LEN)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * Converts a GeneralNames structure into an immutable Collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * alternative names (subject or issuer) in the form required by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * {@link #getSubjectAlternativeNames} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * {@link #getIssuerAlternativeNames}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * @param names the GeneralNames to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * @return an immutable Collection of alternative names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    private static Collection<List<?>> makeAltNames(GeneralNames names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        if (names.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            return Collections.<List<?>>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        }
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1580
        List<List<?>> newNames = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        for (GeneralName gname : names.names()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            GeneralNameInterface name = gname.getName();
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1583
            List<Object> nameEntry = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            nameEntry.add(Integer.valueOf(name.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            switch (name.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            case GeneralNameInterface.NAME_RFC822:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                nameEntry.add(((RFC822Name) name).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            case GeneralNameInterface.NAME_DNS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                nameEntry.add(((DNSName) name).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            case GeneralNameInterface.NAME_DIRECTORY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                nameEntry.add(((X500Name) name).getRFC2253Name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            case GeneralNameInterface.NAME_URI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                nameEntry.add(((URIName) name).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            case GeneralNameInterface.NAME_IP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                    nameEntry.add(((IPAddressName) name).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                    // IPAddressName in cert is bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                    throw new RuntimeException("IPAddress cannot be parsed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                        ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            case GeneralNameInterface.NAME_OID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                nameEntry.add(((OIDName) name).getOID().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                // add DER encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                DerOutputStream derOut = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    name.encode(derOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                    // should not occur since name has already been decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                    // from cert (this would indicate a bug in our code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                    throw new RuntimeException("name cannot be encoded", ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                nameEntry.add(derOut.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            newNames.add(Collections.unmodifiableList(nameEntry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        return Collections.unmodifiableCollection(newNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * Checks a Collection of altNames and clones any name entries of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * byte [].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     */ // only partially generified due to javac bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    private static Collection<List<?>> cloneAltNames(Collection<List<?>> altNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        boolean mustClone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        for (List<?> nameEntry : altNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            if (nameEntry.get(1) instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                // must clone names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                mustClone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        if (mustClone) {
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1641
            List<List<?>> namesCopy = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            for (List<?> nameEntry : altNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                Object nameObject = nameEntry.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                if (nameObject instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    List<Object> nameEntryCopy =
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
  1646
                                        new ArrayList<>(nameEntry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    nameEntryCopy.set(1, ((byte[])nameObject).clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    namesCopy.add(Collections.unmodifiableList(nameEntryCopy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                    namesCopy.add(nameEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            return Collections.unmodifiableCollection(namesCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            return altNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * This method are the overridden implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * getSubjectAlternativeNames method in X509Certificate in the Sun
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * provider. It is better performance-wise since it returns cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    public synchronized Collection<List<?>> getSubjectAlternativeNames()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        // return cached value if we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        if (readOnly && subjectAlternativeNames != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            return cloneAltNames(subjectAlternativeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        SubjectAlternativeNameExtension subjectAltNameExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            getSubjectAlternativeNameExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        if (subjectAltNameExt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        GeneralNames names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1678
            names = subjectAltNameExt.get(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1679
                    SubjectAlternativeNameExtension.SUBJECT_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            return Collections.<List<?>>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        subjectAlternativeNames = makeAltNames(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        return subjectAlternativeNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * This static method is the default implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * getSubjectAlternaitveNames method in X509Certificate. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * X509Certificate provider generally should overwrite this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * provide among other things caching for better performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    public static Collection<List<?>> getSubjectAlternativeNames(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            byte[] ext = cert.getExtensionValue(SUBJECT_ALT_NAME_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            DerValue val = new DerValue(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            byte[] data = val.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            SubjectAlternativeNameExtension subjectAltNameExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                new SubjectAlternativeNameExtension(Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                                                    data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            GeneralNames names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1710
                names = subjectAltNameExt.get(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1711
                        SubjectAlternativeNameExtension.SUBJECT_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            }  catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                return Collections.<List<?>>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            return makeAltNames(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1718
            throw new CertificateParsingException(ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * This method are the overridden implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * getIssuerAlternativeNames method in X509Certificate in the Sun
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * provider. It is better performance-wise since it returns cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
    public synchronized Collection<List<?>> getIssuerAlternativeNames()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        // return cached value if we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        if (readOnly && issuerAlternativeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            return cloneAltNames(issuerAlternativeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        IssuerAlternativeNameExtension issuerAltNameExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            getIssuerAlternativeNameExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        if (issuerAltNameExt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        GeneralNames names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1741
            names = issuerAltNameExt.get(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1742
                    IssuerAlternativeNameExtension.ISSUER_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            return Collections.<List<?>>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        issuerAlternativeNames = makeAltNames(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        return issuerAlternativeNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * This static method is the default implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * getIssuerAlternaitveNames method in X509Certificate. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * X509Certificate provider generally should overwrite this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * provide among other things caching for better performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    public static Collection<List<?>> getIssuerAlternativeNames(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            byte[] ext = cert.getExtensionValue(ISSUER_ALT_NAME_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            DerValue val = new DerValue(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            byte[] data = val.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            IssuerAlternativeNameExtension issuerAltNameExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                new IssuerAlternativeNameExtension(Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                                                    data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            GeneralNames names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1773
                names = issuerAltNameExt.get(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1774
                        IssuerAlternativeNameExtension.ISSUER_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            }  catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                return Collections.<List<?>>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            return makeAltNames(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1781
            throw new CertificateParsingException(ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    public AuthorityInfoAccessExtension getAuthorityInfoAccessExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        return (AuthorityInfoAccessExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            getExtension(PKIXExtensions.AuthInfoAccess_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    /************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * Cert is a SIGNED ASN.1 macro, a three elment sequence:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     *  - Data to be signed (ToBeSigned) -- the "raw" cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     *  - Signature algorithm (SigAlgId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     *  - The signature bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * This routine unmarshals the certificate, saving the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * parts away for later verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    private void parse(DerValue val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        // check if can over write the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        if (readOnly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            throw new CertificateParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                      "cannot over-write existing certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        if (val.data == null || val.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            throw new CertificateParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                      "invalid DER-encoded certificate data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        signedCert = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        DerValue[] seq = new DerValue[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        seq[0] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        seq[1] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        seq[2] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        if (val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            throw new CertificateParsingException("signed overrun, bytes = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                     + val.data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        if (seq[0].tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            throw new CertificateParsingException("signed fields invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        algId = AlgorithmId.parse(seq[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        signature = seq[2].getBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        if (seq[1].data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            throw new CertificateParsingException("algid field overrun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        if (seq[2].data.available() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            throw new CertificateParsingException("signed fields overrun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        // The CertificateInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        info = new X509CertInfo(seq[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        // the "inner" and "outer" signature algorithms must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        AlgorithmId infoSigAlg = (AlgorithmId)info.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                                              CertificateAlgorithmId.NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                                              + DOT +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                                              CertificateAlgorithmId.ALGORITHM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        if (! algId.equals(infoSigAlg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            throw new CertificateException("Signature algorithm mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        readOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * Extract the subject or issuer X500Principal from an X509Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * Parses the encoded form of the cert to preserve the principal's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    private static X500Principal getX500Principal(X509Certificate cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            boolean getIssuer) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        byte[] encoded = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        DerInputStream derIn = new DerInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        DerValue tbsCert = derIn.getSequence(3)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        DerInputStream tbsIn = tbsCert.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        DerValue tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        tmp = tbsIn.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        // skip version number if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        if (tmp.isContextSpecific((byte)0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
          tmp = tbsIn.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        // tmp always contains serial number now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        tmp = tbsIn.getDerValue();              // skip signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        tmp = tbsIn.getDerValue();              // issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        if (getIssuer == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            tmp = tbsIn.getDerValue();          // skip validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            tmp = tbsIn.getDerValue();          // subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        byte[] principalBytes = tmp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        return new X500Principal(principalBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * Extract the subject X500Principal from an X509Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * Called from java.security.cert.X509Certificate.getSubjectX500Principal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    public static X500Principal getSubjectX500Principal(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            return getX500Principal(cert, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            throw new RuntimeException("Could not parse subject", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * Extract the issuer X500Principal from an X509Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * Called from java.security.cert.X509Certificate.getIssuerX500Principal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    public static X500Principal getIssuerX500Principal(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            return getX500Principal(cert, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            throw new RuntimeException("Could not parse issuer", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * Returned the encoding of the given certificate for internal use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * Callers must guarantee that they neither modify it nor expose it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * to untrusted code. Uses getEncodedInternal() if the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * is instance of X509CertImpl, getEncoded() otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    public static byte[] getEncodedInternal(Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            return ((X509CertImpl)cert).getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            return cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * Utility method to convert an arbitrary instance of X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * to a X509CertImpl. Does a cast if possible, otherwise reparses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * the encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    public static X509CertImpl toImpl(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            return (X509CertImpl)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            return X509Factory.intern(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * Utility method to test if a certificate is self-issued. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * the case iff the subject and issuer X500Principals are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
    public static boolean isSelfIssued(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        X500Principal subject = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        X500Principal issuer = cert.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        return subject.equals(issuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * Utility method to test if a certificate is self-signed. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * the case iff the subject and issuer X500Principals are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * AND the certificate's subject public key can be used to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * the certificate. In case of exception, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    public static boolean isSelfSigned(X509Certificate cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        String sigProvider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        if (isSelfIssued(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                if (sigProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                    cert.verify(cert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                    cert.verify(cert.getPublicKey(), sigProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                // In case of exception, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    }
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1963
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1964
    private ConcurrentHashMap<String,String> fingerprints =
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1965
            new ConcurrentHashMap<>(2);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1966
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1967
    public String getFingerprint(String algorithm) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1968
        return fingerprints.computeIfAbsent(algorithm,
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 35283
diff changeset
  1969
            x -> getFingerprint(x, this));
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1970
    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1971
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1972
    /**
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1973
     * Gets the requested finger print of the certificate. The result
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1974
     * only contains 0-9 and A-F. No small case, no colon.
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1975
     */
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 35283
diff changeset
  1976
    public static String getFingerprint(String algorithm,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 35283
diff changeset
  1977
            X509Certificate cert) {
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1978
        try {
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 35283
diff changeset
  1979
            byte[] encCertInfo = cert.getEncoded();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 35283
diff changeset
  1980
            MessageDigest md = MessageDigest.getInstance(algorithm);
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1981
            byte[] digest = md.digest(encCertInfo);
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1982
            StringBuilder sb = new StringBuilder(digest.length * 2);
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1983
            for (int i = 0; i < digest.length; i++) {
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1984
                byte2hex(digest[i], sb);
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1985
            }
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1986
            return sb.toString();
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1987
        } catch (NoSuchAlgorithmException | CertificateEncodingException e) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1988
            // ignored
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1989
        }
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1990
        return "";
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1991
    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1992
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1993
    /**
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1994
     * Converts a byte to hex digit and writes to the supplied builder
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1995
     */
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  1996
    private static void byte2hex(byte b, StringBuilder buf) {
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1997
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1998
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  1999
        int high = ((b & 0xf0) >> 4);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  2000
        int low = (b & 0x0f);
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  2001
        buf.append(hexChars[high])
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
  2002
            .append(hexChars[low]);
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16492
diff changeset
  2003
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
}