All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.sun.xml.ws.security.opt.impl.enc.EncryptedHeader Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.ws.security.opt.impl.enc;
import com.sun.xml.security.core.xenc.CVAdapter;
import com.sun.xml.ws.api.SOAPVersion;
import com.sun.xml.ws.security.opt.api.SecurityElement;
import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
import com.sun.xml.ws.security.opt.impl.crypto.SSEData;
import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
import com.sun.xml.ws.security.secext11.EncryptedHeaderType;
import com.sun.xml.wss.impl.MessageConstants;
import com.sun.xml.wss.impl.c14n.AttributeNS;
import com.sun.xml.wss.impl.c14n.StAXEXC14nCanonicalizerImpl;
import com.sun.xml.wss.logging.LogDomainConstants;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.Key;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import javax.xml.crypto.Data;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo;
import javax.crypto.Cipher;
import com.sun.xml.wss.logging.impl.opt.crypto.LogStringsMessages;
/**
*
* @author [email protected]
*/
public class EncryptedHeader
implements SecurityHeaderElement, SecurityElementWriter{
private static final Logger logger = Logger.getLogger(LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN,
LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN_BUNDLE);
private EncryptedHeaderType eht = null;
private boolean isCanonicalized = false;
//private ObjectFactory objFac = new ObjectFactory();
private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
private Data data = null;
private Key key = null;
private CryptoProcessor dep = null;
/** Creates a new instance of EncryptedHeader */
public EncryptedHeader(EncryptedHeaderType eht, Data data,Key key,SOAPVersion soapVersion) {
this.eht = eht;
this.key = key;
this.data = data;
this.soapVersion = soapVersion;
}
public boolean refersToSecHdrWithId(String id) {
KeyInfo ki = (KeyInfo) eht.getEncryptedData().getKeyInfo();
if(ki != null){
List list = ki.getContent();
if(list.size() >0 ){
Object data = list.get(0);
if(data instanceof SecurityHeaderElement){
return ((SecurityHeaderElement)data).refersToSecHdrWithId(id);
}
}
}
if(data instanceof SSEData){
SecurityElement se = ((SSEData)data).getSecurityElement();
if(se instanceof SecurityHeaderElement ){
return ((SecurityHeaderElement)se).refersToSecHdrWithId(id);
}
}
return false;
}
public String getId() {
return eht.getId();
}
public void setId(String id) {
eht.setId(id);
}
public String getNamespaceURI() {
return MessageConstants.WSSE11_NS;
}
public String getLocalPart() {
return MessageConstants.ENCRYPTED_HEADER_LNAME;
}
public XMLStreamReader readHeader() throws XMLStreamException {
throw new UnsupportedOperationException();
}
public byte[] canonicalize(String algorithm, List namespaceDecls) {
throw new UnsupportedOperationException();
}
public boolean isCanonicalized() {
return isCanonicalized;
}
/**
* writes the encrypted header to an XMLStreamWriter
* @param streamWriter XMLStreamWriter
* @throws XMLStreamException
*/
public void writeTo(XMLStreamWriter streamWriter) throws XMLStreamException {
try {
if (streamWriter instanceof Map && !(dep != null)) {
OutputStream os = (OutputStream) ((Map) streamWriter).get("sjsxp-outputstream");
if (os != null) {
streamWriter.writeCharacters(""); // Force completion of open elems
writeTo(os);
return;
}
}
Marshaller writer = getMarshaller();
if(dep == null){
dep = new CryptoProcessor(Cipher.ENCRYPT_MODE, eht.getEncryptedData().getEncryptionMethod().getAlgorithm(), data, key);
if(streamWriter instanceof StAXEXC14nCanonicalizerImpl){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try{
dep.encryptData(bos);
}catch(IOException ie){
logger.log(Level.SEVERE, LogStringsMessages.WSS_1920_ERROR_CALCULATING_CIPHERVALUE(),ie);
throw new XMLStreamException("Error occurred while calculating Cipher Value");
}
dep.setEncryptedDataCV(bos.toByteArray());
}
}
CVAdapter adapter = new CVAdapter(dep);
writer.setAdapter(CVAdapter.class,adapter);
com.sun.xml.ws.security.secext11.ObjectFactory obj = new com.sun.xml.ws.security.secext11.ObjectFactory();
JAXBElement eh = obj.createEncryptedHeader(eht);
writer.marshal(eh,streamWriter);
} catch (jakarta.xml.bind.JAXBException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex.getMessage()), ex);
} catch (com.sun.xml.wss.XWSSecurityException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex.getMessage()), ex);
}
}
/**
* writes the encrypted header to an XMLStreamWriter
* @param streamWriter XMLStreamWriter
* @param props HashMap
* @throws XMLStreamException
*/
@SuppressWarnings("unchecked")
public void writeTo(XMLStreamWriter streamWriter, HashMap props) throws XMLStreamException {
try{
Marshaller marshaller = getMarshaller();
Iterator> itr = props.entrySet().iterator();
while(itr.hasNext()){
Map.Entry entry = itr.next();
marshaller.setProperty((String)entry.getKey(), entry.getValue());
}
writeTo(streamWriter);
}catch(JAXBException jbe){
logger.log(Level.SEVERE, LogStringsMessages.WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(jbe.getMessage()), jbe);
throw new XMLStreamException(jbe);
}
}
/**
* writes the encrypted header to an OutputStream
* @param os OutputStream
*/
public void writeTo(OutputStream os) {
try {
Marshaller writer = getMarshaller();
CryptoProcessor dep;
dep = new CryptoProcessor(Cipher.ENCRYPT_MODE, eht.getEncryptedData().getEncryptionMethod().getAlgorithm(), data, key);
CVAdapter adapter = new CVAdapter(dep);
writer.setAdapter(CVAdapter.class,adapter);
com.sun.xml.ws.security.secext11.ObjectFactory obj = new com.sun.xml.ws.security.secext11.ObjectFactory();
JAXBElement eh = obj.createEncryptedHeader(eht);
writer.marshal(eh,os);
}catch (com.sun.xml.wss.XWSSecurityException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex.getMessage()), ex);
}catch (jakarta.xml.bind.JAXBException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1916_ERROR_WRITING_ECRYPTEDHEADER(ex.getMessage()), ex);
}
}
private Marshaller getMarshaller() throws JAXBException{
return JAXBUtil.createMarshaller(soapVersion);
}
}