/*
* Copyright 2008 Federal Chancellery Austria and
* Graz University of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.gv.egiz.bku.slcommands.impl;
import iaik.asn1.CodingException;
import iaik.asn1.DerCoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import at.buergerkarte.namespaces.personenbindung._20020506_.CompressedIdentityLinkType;
import at.buergerkarte.namespaces.securitylayer._1.AnyChildrenType;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxAssocArrayPairType;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadDataAssocArrayType;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsBinaryFileType;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadRequestType;
import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType.ReadKeys;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType.ReadPairs;
import at.buergerkarte.namespaces.securitylayer._1.InfoboxReadParamsAssocArrayType.ReadValue;
import at.gv.egiz.bku.slcommands.InfoboxReadCommand;
import at.gv.egiz.bku.slcommands.SLCommand;
import at.gv.egiz.bku.slcommands.SLCommandContext;
import at.gv.egiz.bku.slcommands.SLResult;
import at.gv.egiz.bku.slexceptions.SLCommandException;
import at.gv.egiz.bku.slexceptions.SLExceptionMessages;
import at.gv.egiz.bku.slexceptions.SLRuntimeException;
import at.gv.egiz.idlink.CompressedIdentityLinkFactory;
import at.gv.egiz.idlink.IdentityLinkTransformer;
import at.gv.egiz.idlink.ans1.IdentityLink;
import at.gv.egiz.stal.InfoboxReadRequest;
import at.gv.egiz.stal.InfoboxReadResponse;
import at.gv.egiz.stal.STALRequest;
/**
* This class implements the security layer command
* InfoboxReadRequest.
*
* NOTE: Currently the only supported infobox identifier is '
* IdentityLink'.
*
*
* @author mcentner
*/
public class InfoboxReadCommandImpl extends SLCommandImpl implements
InfoboxReadCommand {
/**
* Logging facility.
*/
protected static Log log = LogFactory.getLog(InfoboxReadCommandImpl.class);
public static final String SEARCH_STRING_PATTERN = ".&&[^/](/.&&[^/])*";
public static final String INFOBOX_IDENTIFIER_CERTIFICATES = "Certificates";
public static final String BOX_SPECIFIC_PARAMETER_IDENTITY_LINK_DOMAIN_IDENTIFIER = "IdentityLinkDomainIdentifier";
public static final String INFOBOX_IDENTIFIER_IDENTITY_LINK = "IdentityLink";
public static final String[] INFOXBOX_CERTIFICATES_KEYS = new String[] {
"SecureSignatureKeypair",
"CertifiedKeypair" };
private static final int ASSOC_ARRAY_READ_KEYS = 1;
private static final int ASSOC_ARRAY_READ_PAIRS = 2;
private static final int ASSOC_ARRAY_READ_VALUE = 3;
/**
* The InfoboxIdentifier
*/
protected String infoboxIdentifier;
/**
* The IdentityLinkDomainIdentifier value of an IdentyLink infobox.
*/
protected String identityLinkDomainIdentifier;
/**
* The list of certificates to be read from an Certificates infobox.
*/
protected List certificates;
/**
* The result type.
*/
protected int assocArrayResult;
/**
* Is content XML entity?
*/
protected boolean isXMLEntity;
@Override
public String getName() {
return "InfoboxReadRequest";
}
/**
* @return the infoboxIdentifier
*/
public String getInfoboxIdentifier() {
return infoboxIdentifier;
}
@Override
public void init(SLCommandContext ctx, Object request) throws SLCommandException {
super.init(ctx, request);
InfoboxReadRequestType req = getRequestValue();
infoboxIdentifier = req.getInfoboxIdentifier();
if (INFOBOX_IDENTIFIER_IDENTITY_LINK.equals(infoboxIdentifier)) {
if (req.getAssocArrayParameters() != null) {
log.info("Got AssocArrayParameters but Infobox type is BinaryFile.");
throw new SLCommandException(4010);
}
InfoboxReadParamsBinaryFileType binaryFileParameters = req.getBinaryFileParameters();
if (binaryFileParameters != null) {
isXMLEntity = binaryFileParameters.isContentIsXMLEntity();
log.debug("Got ContentIsXMLEntity=" + isXMLEntity + ".");
}
AnyChildrenType boxSpecificParameters = req.getBoxSpecificParameters();
if (boxSpecificParameters != null) {
// check BoxSpecificParameters
List