jdk/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.pkcs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.x509.GeneralNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.x509.SerialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class represents a signing certificate attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Its attribute value is defined by the following ASN.1 definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   id-aa-signingCertificate OBJECT IDENTIFIER ::= { iso(1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs9(9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     smime(16) id-aa(2) 12 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   SigningCertificate ::=  SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *       certs       SEQUENCE OF ESSCertID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *       policies    SEQUENCE OF PolicyInformation OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   ESSCertID ::=  SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *       certHash        Hash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *       issuerSerial    IssuerSerial OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   Hash ::= OCTET STRING -- SHA1 hash of entire certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   IssuerSerial ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *       issuer         GeneralNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *       serialNumber   CertificateSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   PolicyInformation ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *       policyIdentifier   CertPolicyId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *       policyQualifiers   SEQUENCE SIZE (1..MAX) OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *               PolicyQualifierInfo OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   CertPolicyId ::= OBJECT IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   PolicyQualifierInfo ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *       policyQualifierId  PolicyQualifierId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *       qualifier        ANY DEFINED BY policyQualifierId }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   -- Implementations that recognize additional policy qualifiers MUST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   -- augment the following definition for PolicyQualifierId
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public class SigningCertificateInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private byte[] ber = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private ESSCertId[] certId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public SigningCertificateInfo(byte[] ber) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        parse(ber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        buffer.append("[\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for (int i = 0; i < certId.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            buffer.append(certId[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // format policies as a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        buffer.append("\n]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void parse(byte[] bytes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // Parse signingCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        DerValue derValue = new DerValue(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new IOException("Bad encoding for signingCertificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Parse certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        DerValue[] certs = derValue.data.getSequence(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        certId = new ESSCertId[certs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            certId[i] = new ESSCertId(certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // Parse policies, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (derValue.data.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            DerValue[] policies = derValue.data.getSequence(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            for (int i = 0; i < policies.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                // parse PolicyInformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
class ESSCertId {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static volatile HexDumpEncoder hexDumper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private byte[] certHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private GeneralNames issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private SerialNumber serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    ESSCertId(DerValue certId) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // Parse certHash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        certHash = certId.data.getDerValue().toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Parse issuerSerial, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (certId.data.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            DerValue issuerSerial = certId.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // Parse issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            issuer = new GeneralNames(issuerSerial.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // Parse serialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            serialNumber = new SerialNumber(issuerSerial.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        buffer.append("[\n\tCertificate hash (SHA-1):\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (hexDumper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            hexDumper = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        buffer.append(hexDumper.encode(certHash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (issuer != null && serialNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            buffer.append("\n\tIssuer: " + issuer + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            buffer.append("\t" + serialNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        buffer.append("\n]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}