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