jdk/src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMKeyInfoFactory.java
author mullan
Fri, 21 Nov 2014 15:23:36 -0500
changeset 27747 3a271dc8b758
parent 25859 3317bb8137f4
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
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    11
 * "License"); you may not use this file except in compliance
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    12
 * with the License. You may obtain a copy of the License at
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    17
 * software distributed under the License is distributed on an
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    19
 * KIND, either express or implied. See the License for the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    21
 * under the License.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    22
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
/*
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
    24
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    26
/*
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    27
 * $Id: DOMKeyInfoFactory.java 1333869 2012-05-04 10:42:44Z coheigea $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.math.BigInteger;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    32
import java.security.KeyException;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    33
import java.security.PublicKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.xml.crypto.*;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    36
import javax.xml.crypto.dom.DOMCryptoContext;
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    37
import javax.xml.crypto.dsig.Transform;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.xml.crypto.dsig.keyinfo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * DOM-based implementation of KeyInfoFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public final class DOMKeyInfoFactory extends KeyInfoFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public DOMKeyInfoFactory() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    52
    public KeyInfo newKeyInfo(List<? extends XMLStructure> content) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return newKeyInfo(content, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    56
    public KeyInfo newKeyInfo(List<? extends XMLStructure> content, String id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return new DOMKeyInfo(content, id);
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 KeyName newKeyName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return new DOMKeyName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public KeyValue newKeyValue(PublicKey key)  throws KeyException {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    65
        String algorithm = key.getAlgorithm();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    66
        if (algorithm.equals("DSA")) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    67
            return new DOMKeyValue.DSA(key);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    68
        } else if (algorithm.equals("RSA")) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    69
            return new DOMKeyValue.RSA(key);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    70
        } else if (algorithm.equals("EC")) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    71
            return new DOMKeyValue.EC(key);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    72
        } else {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    73
            throw new KeyException("unsupported key algorithm: " + algorithm);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    74
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public PGPData newPGPData(byte[] keyId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return newPGPData(keyId, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    81
    public PGPData newPGPData(byte[] keyId, byte[] keyPacket,
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    82
        List<? extends XMLStructure> other) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return new DOMPGPData(keyId, keyPacket, other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    86
    public PGPData newPGPData(byte[] keyPacket,
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    87
        List<? extends XMLStructure> other) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return new DOMPGPData(keyPacket, other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public RetrievalMethod newRetrievalMethod(String uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return newRetrievalMethod(uri, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public RetrievalMethod newRetrievalMethod(String uri, String type,
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    96
        List<? extends Transform> transforms) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (uri == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new NullPointerException("uri must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return new DOMRetrievalMethod(uri, type, transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   103
    public X509Data newX509Data(List<?> content) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return new DOMX509Data(content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public X509IssuerSerial newX509IssuerSerial(String issuerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        BigInteger serialNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return new DOMX509IssuerSerial(issuerName, serialNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public boolean isFeatureSupported(String feature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (feature == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public URIDereferencer getURIDereferencer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return DOMURIDereferencer.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public KeyInfo unmarshalKeyInfo(XMLStructure xmlStructure)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws MarshalException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (xmlStructure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new NullPointerException("xmlStructure cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   129
        if (!(xmlStructure instanceof javax.xml.crypto.dom.DOMStructure)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   130
            throw new ClassCastException("xmlStructure must be of type DOMStructure");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   131
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Node node =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            ((javax.xml.crypto.dom.DOMStructure) xmlStructure).getNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        node.normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Element element = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            element = ((Document) node).getDocumentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            element = (Element) node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new MarshalException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                ("xmlStructure does not contain a proper Node");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // check tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String tag = element.getLocalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (tag == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new MarshalException("Document implementation must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                "support DOM Level 2 and be namespace aware");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (tag.equals("KeyInfo")) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   153
            return new DOMKeyInfo(element, new UnmarshalContext(), getProvider());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new MarshalException("invalid KeyInfo tag: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   158
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   159
    private static class UnmarshalContext extends DOMCryptoContext {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   160
        UnmarshalContext() {}
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   161
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   162
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}