package org.bouncycastle.cert.dane;
import java.io.IOException;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.util.Arrays;
public class DANEEntry
{
public static final int CERT_USAGE_CA = 0;
public static final int CERT_USAGE_PKIX_VALIDATE = 1;
public static final int CERT_USAGE_TRUST_ANCHOR = 2;
public static final int CERT_USAGE_ACCEPT = 3;
static final int CERT_USAGE = 0;
static final int SELECTOR = 1;
static final int MATCHING_TYPE = 2;
private final String domainName;
private final byte[] flags;
private final X509CertificateHolder certHolder;
DANEEntry(String domainName, byte[] flags, X509CertificateHolder certHolder)
{
this.flags = flags;
this.domainName = domainName;
this.certHolder = certHolder;
}
public DANEEntry(String domainName, byte[] data)
throws IOException
{
this(domainName, Arrays.copyOfRange(data, 0, 3), new X509CertificateHolder(Arrays.copyOfRange(data, 3, data.length)));
}
public byte[] getFlags()
{
return Arrays.clone(flags);
}
public X509CertificateHolder getCertificate()
{
return certHolder;
}
public String getDomainName()
{
return domainName;
}
public byte[] getRDATA()
throws IOException
{
byte[] certEnc = certHolder.getEncoded();
byte[] data = new byte[flags.length + certEnc.length];
System.arraycopy(flags, 0, data, 0, flags.length);
System.arraycopy(certEnc, 0, data, flags.length, certEnc.length);
return data;
}
public static boolean isValidCertificate(byte[] data)
{
return ((data[CERT_USAGE] >= 0 || data[CERT_USAGE] <= 3)&& data[SELECTOR] == 0 && data[MATCHING_TYPE] == 0);
}
}