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