jdk/test/javax/xml/crypto/dsig/KeySelectors.java
author alanb
Mon, 18 Oct 2010 10:29:59 +0100
changeset 6902 3352f8839320
parent 5506 202f599c92aa
child 11674 a657f8ba55fc
permissions -rw-r--r--
4837564: (bf) Please make DirectByteBuffer performance enhancements Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2007, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.xml.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.xml.crypto.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.xml.crypto.dsig.keyinfo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.xml.parsers.DocumentBuilderFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.xml.parsers.DocumentBuilder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import org.w3c.dom.traversal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This is a class which supplies several KeySelector implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class KeySelectors {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * KeySelector which would always return the secret key specified in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static class SecretKeySelector extends KeySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        private SecretKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        SecretKeySelector(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            key = wrapBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        SecretKeySelector(SecretKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public KeySelectorResult select(KeyInfo ki,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                        KeySelector.Purpose purpose,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                        AlgorithmMethod method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                        XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throws KeySelectorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return new SimpleKSResult(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        private SecretKey wrapBytes(final byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return new SecretKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    return "Secret key";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                public byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    return (byte[]) bytes.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * KeySelector which would retrieve the X509Certificate out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * KeyInfo element and return the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * NOTE: If there is an X509CRL in the KeyInfo element, then revoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * certificate will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static class RawX509KeySelector extends KeySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public KeySelectorResult select(KeyInfo keyInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                        KeySelector.Purpose purpose,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                        AlgorithmMethod method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                        XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throws KeySelectorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (keyInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw new KeySelectorException("Null KeyInfo object!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // search for X509Data in keyinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            Iterator iter = keyInfo.getContent().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                XMLStructure kiType = (XMLStructure) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                if (kiType instanceof X509Data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    X509Data xd = (X509Data) kiType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    Object[] entries = xd.getContent().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    X509CRL crl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    // Looking for CRL before finding certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    for (int i = 0; (i<entries.length&&crl != null); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        if (entries[i] instanceof X509CRL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            crl = (X509CRL) entries[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    Iterator xi = xd.getContent().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    boolean hasCRL = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    while (xi.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        Object o = xi.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        // skip non-X509Certificate entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        if (o instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            if ((purpose != KeySelector.Purpose.VERIFY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                (crl != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                crl.isRevoked((X509Certificate)o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                return new SimpleKSResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                    (((X509Certificate)o).getPublicKey());
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
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new KeySelectorException("No X509Certificate found!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * KeySelector which would retrieve the public key out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * KeyValue element and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * NOTE: If the key algorithm doesn't match signature algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * then the public key will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static class KeyValueKeySelector extends KeySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public KeySelectorResult select(KeyInfo keyInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                        KeySelector.Purpose purpose,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                        AlgorithmMethod method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                        XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throws KeySelectorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (keyInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new KeySelectorException("Null KeyInfo object!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            SignatureMethod sm = (SignatureMethod) method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            List list = keyInfo.getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            for (int i = 0; i < list.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                XMLStructure xmlStructure = (XMLStructure) list.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (xmlStructure instanceof KeyValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    PublicKey pk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        pk = ((KeyValue)xmlStructure).getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    } catch (KeyException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        throw new KeySelectorException(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    // make sure algorithm is compatible with method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    if (algEquals(sm.getAlgorithm(), pk.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        return new SimpleKSResult(pk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new KeySelectorException("No KeyValue element found!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        //@@@FIXME: this should also work for key types other than DSA/RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        static boolean algEquals(String algURI, String algName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (algName.equalsIgnoreCase("DSA") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                algURI.equals(SignatureMethod.DSA_SHA1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else if (algName.equalsIgnoreCase("RSA") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                (algURI.equals(SignatureMethod.RSA_SHA1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 algURI.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 algURI.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                 algURI.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * KeySelector which would perform special lookup as documented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * by the ie/baltimore/merlin-examples testcases and return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * matching public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static class CollectionKeySelector extends KeySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        private CertificateFactory certFac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        private File certDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        private Vector certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        private static final int MATCH_SUBJECT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        private static final int MATCH_ISSUER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        private static final int MATCH_SERIAL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        private static final int MATCH_SUBJECT_KEY_ID = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        private static final int MATCH_CERTIFICATE = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        CollectionKeySelector(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            certDir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                certFac = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                // not going to happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            certs = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            File[] files = new File(certDir, "certs").listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    certs.add(certFac.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                              (new FileInputStream(files[i])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                } catch (Exception ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        Vector match(int matchType, Object value, Vector pool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            Vector matchResult = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            for (int j=0; j < pool.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                X509Certificate c = (X509Certificate) pool.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                switch (matchType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                case MATCH_SUBJECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        if (c.getSubjectDN().equals(new X500Name((String)value))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                case MATCH_ISSUER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        if (c.getIssuerDN().equals(new X500Name((String)value))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                case MATCH_SERIAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    if (c.getSerialNumber().equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                case MATCH_SUBJECT_KEY_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    byte[] extension = c.getExtensionValue("2.5.29.14");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    if (extension != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            DerValue derValue = new DerValue(extension);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            DerValue derValue2 = new DerValue(derValue.getOctetString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            byte[] extVal = derValue2.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            if (Arrays.equals(extVal, (byte[]) value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        } catch (IOException ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                case MATCH_CERTIFICATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    if (c.equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return matchResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public KeySelectorResult select(KeyInfo keyInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                        KeySelector.Purpose purpose,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                        AlgorithmMethod method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                        XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throws KeySelectorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (keyInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                throw new KeySelectorException("Null KeyInfo object!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            Iterator iter = keyInfo.getContent().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                XMLStructure xmlStructure = (XMLStructure) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    if (xmlStructure instanceof KeyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        String name = ((KeyName)xmlStructure).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        PublicKey pk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            // Lookup the public key using the key name 'Xxx',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            // i.e. the public key is in "certs/xxx.crt".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            File certFile = new File(new File(certDir, "certs"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                name.toLowerCase()+".crt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            X509Certificate cert = (X509Certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                certFac.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                (new FileInputStream(certFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            pk = cert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            // assume KeyName contains subject DN and search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            // collection of certs for match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            Vector result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                match(MATCH_SUBJECT, name, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                            int numOfMatches = (result==null? 0:result.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                            if (numOfMatches != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                throw new KeySelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                    ((numOfMatches==0?"No":"More than one") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                     " match found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            pk =((X509Certificate)result.get(0)).getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        return new SimpleKSResult(pk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    } else if (xmlStructure instanceof RetrievalMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        // Lookup the public key using the retrievel method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        // NOTE: only X509Certificate type is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        RetrievalMethod rm = (RetrievalMethod) xmlStructure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        String type = rm.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        if (type.equals(X509Data.RAW_X509_CERTIFICATE_TYPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                            String uri = rm.getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                            X509Certificate cert = (X509Certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                certFac.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                (new FileInputStream(new File(certDir, uri)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            return new SimpleKSResult(cert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                            throw new KeySelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                ("Unsupported RetrievalMethod type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    } else if (xmlStructure instanceof X509Data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        List content = ((X509Data)xmlStructure).getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        int size = content.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        Vector result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        // Lookup the public key using the information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        // specified in X509Data element, i.e. searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        // over the collection of certificate files under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        // "certs" subdirectory and return those match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        for (int k = 0; k<size; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            Object obj = content.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            if (obj instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                result = match(MATCH_SUBJECT, obj, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                            } else if (obj instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                result = match(MATCH_SUBJECT_KEY_ID, obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                               certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            } else if (obj instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                result = match(MATCH_CERTIFICATE, obj, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                            } else if (obj instanceof X509IssuerSerial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                X509IssuerSerial is = (X509IssuerSerial) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                result = match(MATCH_SERIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                               is.getSerialNumber(), certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                result = match(MATCH_ISSUER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                               is.getIssuerName(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                throw new KeySelectorException("Unsupported X509Data: " + obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        int numOfMatches = (result==null? 0:result.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        if (numOfMatches != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                            throw new KeySelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                ((numOfMatches==0?"No":"More than one") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                 " match found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        return new SimpleKSResult(((X509Certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                          result.get(0)).getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    throw new KeySelectorException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new KeySelectorException("No matching key found!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    static class ByteUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        private static String mapping = "0123456789ABCDEF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        private static int numBytesPerRow = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        private static String getHex(byte value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            int low = value & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            int high = ((value >> 4) & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            char[] res = new char[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            res[0] = mapping.charAt(high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            res[1] = mapping.charAt(low);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return new String(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        static String dumpArray(byte[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            int numDumped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            StringBuffer buf = new StringBuffer(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            buf.append("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            for (int i=0;i<(in.length/numBytesPerRow); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                for (int j=0; j<(numBytesPerRow); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    buf.append("(byte)0x" + getHex(in[i*numBytesPerRow+j]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                               ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                numDumped += numBytesPerRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            while (numDumped < in.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                buf.append("(byte)0x" + getHex(in[numDumped]) + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                numDumped += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            buf.append("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
class SimpleKSResult implements KeySelectorResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    private final Key key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    SimpleKSResult(Key key) { this.key = key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public Key getKey() { return key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
}