/*******************************************************************************
* Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria
* PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a
* joint initiative of the Federal Chancellery Austria and Graz University of
* Technology.
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* http://www.osor.eu/eupl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
******************************************************************************/
package at.gv.egiz.sl.util;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import at.gv.egiz.pdfas.common.exceptions.PDFIOException;
import at.gv.egiz.pdfas.common.utils.PDFUtils;
import at.gv.egiz.pdfas.lib.api.IConfigurationConstants;
import at.gv.egiz.pdfas.lib.api.sign.SignParameter;
import at.gv.egiz.sl.schema.Base64OptRefContentType;
import at.gv.egiz.sl.schema.Base64XMLLocRefOptRefContentType;
import at.gv.egiz.sl.schema.CMSDataObjectRequiredMetaType;
import at.gv.egiz.sl.schema.CreateCMSSignatureRequestType;
import at.gv.egiz.sl.schema.ExcludedByteRangeType;
import at.gv.egiz.sl.schema.InfoboxReadParamsAssocArrayType;
import at.gv.egiz.sl.schema.InfoboxReadParamsAssocArrayType.ReadValue;
import at.gv.egiz.sl.schema.InfoboxReadRequestType;
import at.gv.egiz.sl.schema.MetaInfoType;
import at.gv.egiz.sl.schema.ObjectFactory;
public abstract class BaseSLConnector implements ISLConnector,
IConfigurationConstants {
private static final Logger logger = LoggerFactory
.getLogger(BaseSLConnector.class);
public static final String SecureSignatureKeypair = "SecureSignatureKeypair";
public static final String PDF_MIME_TYPE = "application/pdf";
public static final String PDF_MIME_TYPE_DESC = "Adobe PDF-File";
public static final String DETACHED = "detached";
public static final String XMLREQUEST = "XMLRequest";
public static final String WHITELIST_VERSION_ENABLED = "whitelistVersion.enabled";
public static final String WHITELIST_VALUE_PRE = "whiteListVersions";
private static List whiteListregEx = new ArrayList();
public static String responseHeader;
protected ObjectFactory of = new ObjectFactory();
public InfoboxReadRequestType createInfoboxReadRequest(
SignParameter parameter) {
InfoboxReadRequestType request = new InfoboxReadRequestType();
request.setInfoboxIdentifier("Certificates");
InfoboxReadParamsAssocArrayType readData = new InfoboxReadParamsAssocArrayType();
ReadValue readValue = new ReadValue();
readValue.setKey(SecureSignatureKeypair);
readData.setReadValue(readValue);
request.setAssocArrayParameters(readData);
return request;
}
public RequestPackage createCMSRequest(byte[] signatureData,
int[] byteRange, SignParameter parameter) throws PDFIOException {
boolean base64 = true;
String requestType = parameter.getConfiguration().getValue(
SL_REQUEST_TYPE);
if (requestType != null) {
if (requestType.equals(SL_REQUEST_TYPE_BASE64)) {
base64 = true;
} else if (requestType.equals(SL_REQUEST_TYPE_UPLOAD)) {
base64 = false;
}
}
byte[] data = PDFUtils.blackOutSignature(signatureData, byteRange);
RequestPackage pack = new RequestPackage();
int[] exclude_range = PDFUtils.buildExcludeRange(byteRange);
logger.info("Exclude Byte Range: " + exclude_range[0] + " "
+ exclude_range[1]);
MetaInfoType metaInfoType = new MetaInfoType();
metaInfoType.setMimeType(PDF_MIME_TYPE);
// == Base64OptRefContentType
Base64OptRefContentType base64OptRefContentType = new Base64OptRefContentType();
if (base64) {
base64OptRefContentType.setBase64Content(data);
} else {
base64OptRefContentType.setReference("formdata:fileupload");
pack.setSignatureData(signatureData);
pack.setByteRange(byteRange);
}
// == CMSDataObjectRequiredMetaType
CMSDataObjectRequiredMetaType cmsDataObjectRequiredMetaType = new CMSDataObjectRequiredMetaType();
cmsDataObjectRequiredMetaType.setMetaInfo(metaInfoType);
cmsDataObjectRequiredMetaType.setContent(base64OptRefContentType);
if (byteRange.length > 0) {
ExcludedByteRangeType excludeByteRange = new ExcludedByteRangeType();
excludeByteRange.setFrom(new BigInteger(String
.valueOf(exclude_range[0])));
excludeByteRange.setTo(new BigInteger(String
.valueOf(exclude_range[1])));
cmsDataObjectRequiredMetaType
.setExcludedByteRange(excludeByteRange);
}
// == CreateCMSSignatureRequestType
CreateCMSSignatureRequestType request = new CreateCMSSignatureRequestType();
request.setKeyboxIdentifier(SecureSignatureKeypair);
request.setDataObject(cmsDataObjectRequiredMetaType);
request.setStructure(DETACHED);
//whiteListregEx.add("1.2 MOCCA");
if (parameter.getConfiguration().hasValue(IConfigurationConstants.SIG_PADES_FORCE_FLAG))
{
if (IConfigurationConstants.TRUE.equalsIgnoreCase(parameter.getConfiguration().getValue(IConfigurationConstants.SIG_PADES_FORCE_FLAG)))
{
request.setPAdESFlag(true);
}
}
else
if (parameter.getConfiguration().hasValue(IConfigurationConstants.SIG_PADES_INTELL_FLAG)){
if (IConfigurationConstants.TRUE.equalsIgnoreCase(parameter.getConfiguration().getValue(IConfigurationConstants.SIG_PADES_INTELL_FLAG)))
{
boolean intellFlag=isProvidePdfVersionInWhitelist(responseHeader, parameter);
if (intellFlag)
{
request.setPAdESFlag(true);
}
}}
pack.setRequestType(request);
return pack;
}
public static boolean isWhiteListEnabled(SignParameter parameter) {
boolean whiteListConfig = parameter.getConfiguration().hasValue(WHITELIST_VERSION_ENABLED);
if (whiteListConfig)
{
String value = parameter.getConfiguration().getValue(WHITELIST_VERSION_ENABLED);
if (value != null) {
if (value.equals("true")) {
whiteListregEx.add(parameter.getConfiguration().getValue(WHITELIST_VALUE_PRE));
return true;
}
}}
return false;
}
public static synchronized boolean isProvidePdfVersionInWhitelist(String bkuVersionInformation, SignParameter parameter) {
if (isWhiteListEnabled(parameter)) {
Iterator patterns = whiteListregEx.iterator();
while (patterns.hasNext()) {
String pattern = patterns.next();
try {
if (bkuVersionInformation.contains(pattern)) {
return true;
}
//TODO: extend with BlackList
} catch (Throwable e) {
logger.warn("Error in matching regex: " + pattern, e);
}
}
return false;
}
return true;
}
}