jdk/test/javax/xml/crypto/dsig/KeySelectors.java
author mullan
Fri, 21 Nov 2014 15:23:36 -0500
changeset 27747 3a271dc8b758
parent 24251 da7dc40edb67
child 28308 5fdc6e6c0b97
permissions -rw-r--r--
8046949: Generify the javax.xml.crypto API Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 11674
diff changeset
     2
 * Copyright (c) 2005, 2014, 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
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   104
            for (XMLStructure kiType : keyInfo.getContent()) {
2
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
                    boolean hasCRL = false;
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   116
                    for (Object o : xd.getContent()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        // skip non-X509Certificate entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        if (o instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            if ((purpose != KeySelector.Purpose.VERIFY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                (crl != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                crl.isRevoked((X509Certificate)o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                return new SimpleKSResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                    (((X509Certificate)o).getPublicKey());
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
            throw new KeySelectorException("No X509Certificate found!");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * KeySelector which would retrieve the public key out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * KeyValue element and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * NOTE: If the key algorithm doesn't match signature algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * then the public key will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static class KeyValueKeySelector extends KeySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public KeySelectorResult select(KeyInfo keyInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                        KeySelector.Purpose purpose,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                        AlgorithmMethod method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                        XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throws KeySelectorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (keyInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                throw new KeySelectorException("Null KeyInfo object!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            SignatureMethod sm = (SignatureMethod) method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   152
            for (XMLStructure xmlStructure : keyInfo.getContent()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (xmlStructure instanceof KeyValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    PublicKey pk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        pk = ((KeyValue)xmlStructure).getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    } catch (KeyException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        throw new KeySelectorException(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    // make sure algorithm is compatible with method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    if (algEquals(sm.getAlgorithm(), pk.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        return new SimpleKSResult(pk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new KeySelectorException("No KeyValue element found!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        //@@@FIXME: this should also work for key types other than DSA/RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        static boolean algEquals(String algURI, String algName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (algName.equalsIgnoreCase("DSA") &&
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 11674
diff changeset
   172
                algURI.equals(SignatureMethod.DSA_SHA1) ||
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 11674
diff changeset
   173
                algURI.equals("http://www.w3.org/2009/xmldsig11#dsa-sha256")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else if (algName.equalsIgnoreCase("RSA") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                (algURI.equals(SignatureMethod.RSA_SHA1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                 algURI.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                 algURI.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 algURI.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * KeySelector which would perform special lookup as documented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * by the ie/baltimore/merlin-examples testcases and return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * matching public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static class CollectionKeySelector extends KeySelector {
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   196
        private CertificateFactory cf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        private File certDir;
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   198
        private Vector<X509Certificate> certs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        private static final int MATCH_SUBJECT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        private static final int MATCH_ISSUER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        private static final int MATCH_SERIAL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        private static final int MATCH_SUBJECT_KEY_ID = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        private static final int MATCH_CERTIFICATE = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        CollectionKeySelector(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            certDir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            try {
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   208
                cf = CertificateFactory.getInstance("X509");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                // not going to happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   212
            certs = new Vector<X509Certificate>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            File[] files = new File(certDir, "certs").listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            for (int i = 0; i < files.length; i++) {
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   215
                try (FileInputStream fis = new FileInputStream(files[i])) {
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   216
                    certs.add((X509Certificate)cf.generateCertificate(fis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                } catch (Exception ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   221
        Vector<X509Certificate> match(int matchType, Object value,
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   222
                                      Vector<X509Certificate> pool) {
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   223
            Vector<X509Certificate> matchResult = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            for (int j=0; j < pool.size(); j++) {
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   225
                X509Certificate c = pool.get(j);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                switch (matchType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                case MATCH_SUBJECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        if (c.getSubjectDN().equals(new X500Name((String)value))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                case MATCH_ISSUER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        if (c.getIssuerDN().equals(new X500Name((String)value))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                            matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                case MATCH_SERIAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    if (c.getSerialNumber().equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                case MATCH_SUBJECT_KEY_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    byte[] extension = c.getExtensionValue("2.5.29.14");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    if (extension != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            DerValue derValue = new DerValue(extension);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            DerValue derValue2 = new DerValue(derValue.getOctetString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            byte[] extVal = derValue2.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            if (Arrays.equals(extVal, (byte[]) value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                matchResult.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        } catch (IOException ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                case MATCH_CERTIFICATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (c.equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        matchResult.add(c);
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
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return matchResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public KeySelectorResult select(KeyInfo keyInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                        KeySelector.Purpose purpose,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                        AlgorithmMethod method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                        XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throws KeySelectorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (keyInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                throw new KeySelectorException("Null KeyInfo object!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   279
            for (XMLStructure xmlStructure : keyInfo.getContent()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    if (xmlStructure instanceof KeyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        String name = ((KeyName)xmlStructure).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        PublicKey pk = null;
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   284
                        File certFile = new File(new File(certDir, "certs"),
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   285
                                                 name.toLowerCase() + ".crt");
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   286
                        try (FileInputStream fis = new FileInputStream(certFile)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            // Lookup the public key using the key name 'Xxx',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            // i.e. the public key is in "certs/xxx.crt".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            X509Certificate cert = (X509Certificate)
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   290
                                cf.generateCertificate(fis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            pk = cert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            // assume KeyName contains subject DN and search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            // collection of certs for match
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   295
                            Vector<X509Certificate> result =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                match(MATCH_SUBJECT, name, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            int numOfMatches = (result==null? 0:result.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            if (numOfMatches != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                throw new KeySelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                    ((numOfMatches==0?"No":"More than one") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                     " match found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            }
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   303
                            pk = result.get(0).getPublicKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        return new SimpleKSResult(pk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    } else if (xmlStructure instanceof RetrievalMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        // Lookup the public key using the retrievel method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        // NOTE: only X509Certificate type is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        RetrievalMethod rm = (RetrievalMethod) xmlStructure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        String type = rm.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        if (type.equals(X509Data.RAW_X509_CERTIFICATE_TYPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            String uri = rm.getURI();
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   313
                            try (FileInputStream fis =
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   314
                                 new FileInputStream(new File(certDir, uri))) {
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   315
                                X509Certificate cert = (X509Certificate)
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   316
                                    cf.generateCertificate(fis);
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   317
                                return new SimpleKSResult(cert.getPublicKey());
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   318
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                            throw new KeySelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                ("Unsupported RetrievalMethod type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    } else if (xmlStructure instanceof X509Data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        List content = ((X509Data)xmlStructure).getContent();
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   325
                        Vector<X509Certificate> result = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        // Lookup the public key using the information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        // specified in X509Data element, i.e. searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        // over the collection of certificate files under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        // "certs" subdirectory and return those match.
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   330
                        for (Object obj : content) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            if (obj instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                result = match(MATCH_SUBJECT, obj, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                            } else if (obj instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                result = match(MATCH_SUBJECT_KEY_ID, obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                               certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            } else if (obj instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                result = match(MATCH_CERTIFICATE, obj, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                            } else if (obj instanceof X509IssuerSerial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                X509IssuerSerial is = (X509IssuerSerial) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                result = match(MATCH_SERIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                               is.getSerialNumber(), certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                result = match(MATCH_ISSUER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                               is.getIssuerName(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                throw new KeySelectorException("Unsupported X509Data: " + obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        int numOfMatches = (result==null? 0:result.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        if (numOfMatches != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            throw new KeySelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                ((numOfMatches==0?"No":"More than one") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                 " match found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        }
11674
a657f8ba55fc 7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents: 5506
diff changeset
   354
                        return new SimpleKSResult(result.get(0).getPublicKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    throw new KeySelectorException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new KeySelectorException("No matching key found!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    static class ByteUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        private static String mapping = "0123456789ABCDEF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        private static int numBytesPerRow = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        private static String getHex(byte value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            int low = value & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            int high = ((value >> 4) & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            char[] res = new char[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            res[0] = mapping.charAt(high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            res[1] = mapping.charAt(low);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return new String(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        static String dumpArray(byte[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            int numDumped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            StringBuffer buf = new StringBuffer(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            buf.append("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            for (int i=0;i<(in.length/numBytesPerRow); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                for (int j=0; j<(numBytesPerRow); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    buf.append("(byte)0x" + getHex(in[i*numBytesPerRow+j]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                               ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                numDumped += numBytesPerRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            while (numDumped < in.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                buf.append("(byte)0x" + getHex(in[numDumped]) + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                numDumped += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            buf.append("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
class SimpleKSResult implements KeySelectorResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private final Key key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    SimpleKSResult(Key key) { this.key = key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public Key getKey() { return key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
}