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.
/*
* Copyright 2017 The Apache Software Foundation.
*
* 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 org.apache.pdfbox.pdmodel.interactive.digitalsignature;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.pdmodel.common.COSArrayList;
import org.apache.pdfbox.pdmodel.common.COSObjectable;
/**
* This class represents a certificate seed value dictionary that is in the seed value which puts
* constraints on certificates when signing documents.
*
* @author Hossam Hazem
*/
public class PDSeedValueCertificate implements COSObjectable
{
/**
* A Ff flag.
*/
public static final int FLAG_SUBJECT = 1;
/**
* A Ff flag.
*/
public static final int FLAG_ISSUER = 1 << 1;
/**
* A Ff flag.
*/
public static final int FLAG_OID = 1 << 2;
/**
* A Ff flag.
*/
public static final int FLAG_SUBJECT_DN = 1 << 3;
/**
* A Ff flag.
*/
public static final int FLAG_KEY_USAGE = 1 << 5;
/**
* A Ff flag.
*/
public static final int FLAG_URL = 1 << 6;
private final COSDictionary dictionary;
/**
* Default constructor.
*/
public PDSeedValueCertificate()
{
this.dictionary = new COSDictionary();
this.dictionary.setItem(COSName.TYPE, COSName.SV_CERT);
this.dictionary.setDirect(true);
}
/**
* Constructor.
*
* @param dict The certificate seed value dictionary.
*/
public PDSeedValueCertificate(COSDictionary dict)
{
this.dictionary = dict;
this.dictionary.setDirect(true);
}
/**
* Convert this standard java object to a COS dictionary.
*
* @return The COS dictionary that matches this Java object.
*/
@Override
public COSDictionary getCOSObject()
{
return dictionary;
}
/**
*
* @return true if the Subject is required
*/
public boolean isSubjectRequired()
{
return this.getCOSObject().getFlag(COSName.FF, FLAG_SUBJECT);
}
/**
* set true if subject shall be required as a constraint on signature.
*
* @param flag if true, the specified Subject shall be enforced as a constraint.
*/
public void setSubjectRequired(boolean flag)
{
this.getCOSObject().setFlag(COSName.FF, FLAG_SUBJECT, flag);
}
/**
*
* @return true if the Issuer is required
*/
public boolean isIssuerRequired()
{
return this.getCOSObject().getFlag(COSName.FF, FLAG_ISSUER);
}
/**
* set true if Issuer shall be required as a constraint on signature.
*
* @param flag if true, the specified Issuer shall be enforced as a constraint.
*/
public void setIssuerRequired(boolean flag)
{
this.getCOSObject().setFlag(COSName.FF, FLAG_ISSUER, flag);
}
/**
*
* @return true if the OID is required
*/
public boolean isOIDRequired()
{
return this.getCOSObject().getFlag(COSName.FF, FLAG_OID);
}
/**
* set true if OID shall be required as a constraint on signature.
*
* @param flag if true, the specified OID shall be enforced as a constraint.
*/
public void setOIDRequired(boolean flag)
{
this.getCOSObject().setFlag(COSName.FF, FLAG_OID, flag);
}
/**
*
* @return true if the Subject DN is required
*/
public boolean isSubjectDNRequired()
{
return this.getCOSObject().getFlag(COSName.FF, FLAG_SUBJECT_DN);
}
/**
* set true if subject DN shall be required as a constraint on signature.
*
* @param flag if true, the specified Subject DN shall be enforced as a constraint.
*/
public void setSubjectDNRequired(boolean flag)
{
this.getCOSObject().setFlag(COSName.FF, FLAG_SUBJECT_DN, flag);
}
/**
*
* @return true if the KeyUsage is required
*/
public boolean isKeyUsageRequired()
{
return this.getCOSObject().getFlag(COSName.FF, FLAG_KEY_USAGE);
}
/**
* set true if KeyUsage shall be required as a constraint on signature.
*
* @param flag if true, the specified KeyUsage shall be enforced as a constraint.
*/
public void setKeyUsageRequired(boolean flag)
{
this.getCOSObject().setFlag(COSName.FF, FLAG_KEY_USAGE, flag);
}
/**
*
* @return true if the URL is required
*/
public boolean isURLRequired()
{
return this.getCOSObject().getFlag(COSName.FF, FLAG_URL);
}
/**
* set true if URL shall be required as a constraint on signature.
*
* @param flag if true, the specified URL shall be enforced as a constraint.
*/
public void setURLRequired(boolean flag)
{
this.getCOSObject().setFlag(COSName.FF, FLAG_URL, flag);
}
/**
* Returns list of byte arrays that contains DER-encoded X.509v3 certificates
*
* @return a list of bytes arrays representing the subject.
*/
public List getSubject()
{
COSBase base = this.dictionary.getDictionaryObject(COSName.SUBJECT);
if (base instanceof COSArray)
{
COSArray array = (COSArray) base;
return getListOfByteArraysFromCOSArray(array);
}
return null;
}
/**
* (Optional) A list of byte arrays containing DER-encoded X.509v3 certificates that are
* acceptable for signing. if
* Subject is not null and {@link #isSubjectRequired()} is true then the subject
* constraint is enforced on the subjects in this array subjects.
*
* @param subjects list of byte arrays containing DER-encoded X.509v3 certificates that are
* acceptable for signing.
*/
public void setSubject(List subjects)
{
COSArray array = new COSArray();
for (byte[] subject : subjects)
{
array.add(new COSString(subject));
}
this.dictionary.setItem(COSName.SUBJECT, array);
}
/**
* (Optional) byte array containing DER-encoded X.509v3 certificate that is acceptable for
* signing. works like {@link #setSubject(List)} but one byte array
*
* @param subject byte array containing DER-encoded X.509v3 certificate
*/
public void addSubject(byte[] subject)
{
COSBase base = this.dictionary.getDictionaryObject(COSName.SUBJECT);
COSArray array;
if (base instanceof COSArray)
{
array = (COSArray) base;
}
else
{
array = new COSArray();
}
COSString string = new COSString(subject);
array.add(string);
this.dictionary.setItem(COSName.SUBJECT, array);
}
/**
* removes a subject from the list
*
* @param subject byte array containing DER-encoded X.509v3 certificate
*/
public void removeSubject(byte[] subject)
{
COSBase base = this.dictionary.getDictionaryObject(COSName.SUBJECT);
if (base instanceof COSArray)
{
COSArray array = (COSArray) base;
array.remove(new COSString(subject));
}
}
/**
* Returns list of maps that contains subject distinguished names like [(cn: John Doe, o: Doe), (cn: John Smith)]
* both keys are typically of the form 'cn', 'o', 'email', '2.5.4.43'; and values are text strings.
*
* @return a list of maps containing the subject distinguished names
*/
public List