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