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     public KeyInfo newKeyInfo(List content) {
  52         return newKeyInfo(content, null);
  53     }
  54 
  55     @SuppressWarnings("unchecked")
  56     public KeyInfo newKeyInfo(List content, String id) {
  57         return new DOMKeyInfo(content, id);
  58     }
  59 
  60     public KeyName newKeyName(String name) {
  61         return new DOMKeyName(name);
  62     }
  63 
  64     public KeyValue newKeyValue(PublicKey key)  throws KeyException {
  65         String algorithm = key.getAlgorithm();
  66         if (algorithm.equals("DSA")) {
  67             return new DOMKeyValue.DSA(key);
  68         } else if (algorithm.equals("RSA")) {
  69             return new DOMKeyValue.RSA(key);
  70         } else if (algorithm.equals("EC")) {
  71             return new DOMKeyValue.EC(key);
  72         } else {
  73             throw new KeyException("unsupported key algorithm: " + algorithm);
  74         }
  75     }
  76 
  77     public PGPData newPGPData(byte[] keyId) {
  78         return newPGPData(keyId, null, null);
  79     }
  80 
  81     @SuppressWarnings("unchecked")
  82     public PGPData newPGPData(byte[] keyId, byte[] keyPacket, List other) {
  83         return new DOMPGPData(keyId, keyPacket, other);
  84     }
  85 
  86     @SuppressWarnings("unchecked")
  87     public PGPData newPGPData(byte[] keyPacket, List other) {
  88         return new DOMPGPData(keyPacket, other);
  89     }
  90 
  91     public RetrievalMethod newRetrievalMethod(String uri) {
  92         return newRetrievalMethod(uri, null, null);
  93     }
  94 
  95     @SuppressWarnings("unchecked")
  96     public RetrievalMethod newRetrievalMethod(String uri, String type,
  97         List transforms) {
  98         if (uri == null) {
  99             throw new NullPointerException("uri must not be null");
 100         }
 101         return new DOMRetrievalMethod(uri, type, transforms);
 102     }
 103 
 104     @SuppressWarnings("unchecked")
 105     public X509Data newX509Data(List content) {
 106         return new DOMX509Data(content);
 107     }
 108 
 109     public X509IssuerSerial newX509IssuerSerial(String issuerName,
 110         BigInteger serialNumber) {
 111         return new DOMX509IssuerSerial(issuerName, serialNumber);
 112     }
 113 
 114     public boolean isFeatureSupported(String feature) {
 115         if (feature == null) {
 116             throw new NullPointerException();
 117         } else {
 118             return false;
 119         }
 120     }
 121 
 122     public URIDereferencer getURIDereferencer() {
 123         return DOMURIDereferencer.INSTANCE;
 124     }
 125 
 126     public KeyInfo unmarshalKeyInfo(XMLStructure xmlStructure)
 127         throws MarshalException {
 128         if (xmlStructure == null) {
 129             throw new NullPointerException("xmlStructure cannot be null");
 130         }
 131         if (!(xmlStructure instanceof javax.xml.crypto.dom.DOMStructure)) {
 132             throw new ClassCastException("xmlStructure must be of type DOMStructure");
 133         }
 134         Node node =
 135             ((javax.xml.crypto.dom.DOMStructure) xmlStructure).getNode();
 136         node.normalize();
 137 
 138         Element element = null;
 139         if (node.getNodeType() == Node.DOCUMENT_NODE) {
 140             element = ((Document) node).getDocumentElement();
 141         } else if (node.getNodeType() == Node.ELEMENT_NODE) {
 142             element = (Element) node;
 143         } else {
 144             throw new MarshalException
 145                 ("xmlStructure does not contain a proper Node");
 146         }
 147 
 148         // check tag
 149         String tag = element.getLocalName();
 150         if (tag == null) {
 151             throw new MarshalException("Document implementation must " +
 152                 "support DOM Level 2 and be namespace aware");
 153         }
 154         if (tag.equals("KeyInfo")) {
 155             return new DOMKeyInfo(element, new UnmarshalContext(), getProvider());
 156         } else {
 157             throw new MarshalException("invalid KeyInfo tag: " + tag);
 158         }
 159     }
 160 
 161     private static class UnmarshalContext extends DOMCryptoContext {
 162         UnmarshalContext() {}
 163     }
 164 
 165 }