Search in sources :

Example 1 with DNSRecordCreationException

use of org.nhindirect.config.model.exceptions.DNSRecordCreationException in project nhin-d by DirectProject.

the class DNSUtils method createX509CERTRecord.

/**
	 * Creates a DNS CERT record containing an X509 public certificate.
	 * @param address The name or address corresponding to the certificate.
	 * @param ttl The time to live in seconds.
	 * @param cert The X509 public certificate to be stored with the name/address. 
	 * @return A DNSRecord representing a CERT type record.
	 * @throws ConfigurationStoreException
	 */
public static DNSRecord createX509CERTRecord(String address, long ttl, X509Certificate cert) throws DNSRecordCreationException {
    if (!address.endsWith("."))
        address = address + ".";
    try {
        int keyTag = 0;
        if (cert.getPublicKey() instanceof RSAKey) {
            RSAKey key = (RSAKey) cert.getPublicKey();
            byte[] modulus = key.getModulus().toByteArray();
            keyTag = (modulus[modulus.length - 2] << 8) & 0xFF00;
            keyTag |= modulus[modulus.length - 1] & 0xFF;
        }
        CERTRecord rec = new CERTRecord(Name.fromString(address), DClass.IN, ttl, CERTRecord.PKIX, keyTag, 5, /*public key alg, RFC 4034*/
        cert.getEncoded());
        return fromWire(rec.toWireCanonical());
    } catch (Exception e) {
        throw new DNSRecordCreationException("Failed to create DNS CERT record: " + e.getMessage(), e);
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) CERTRecord(org.xbill.DNS.CERTRecord) DNSRecordCreationException(org.nhindirect.config.model.exceptions.DNSRecordCreationException) IOException(java.io.IOException) DNSRecordCreationException(org.nhindirect.config.model.exceptions.DNSRecordCreationException)

Aggregations

IOException (java.io.IOException)1 RSAKey (java.security.interfaces.RSAKey)1 DNSRecordCreationException (org.nhindirect.config.model.exceptions.DNSRecordCreationException)1 CERTRecord (org.xbill.DNS.CERTRecord)1