All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ibole.infrastructure.security.certificate.SubjectAlternativeNameHolder Maven / Gradle / Ivy

The newest version!
package com.github.ibole.infrastructure.security.certificate;

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.List; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
import org.bouncycastle.asn1.ASN1Encodable; 
import org.bouncycastle.asn1.DERSequence; 
import org.bouncycastle.asn1.x509.Extension; 
import org.bouncycastle.asn1.x509.GeneralName; 
import org.bouncycastle.cert.CertIOException; 
import org.bouncycastle.cert.X509v3CertificateBuilder; 

/*********************************************************************************************.
 * 
 * 
 * 

Copyright 2016, iBole Inc. All rights reserved. * *

*********************************************************************************************/ public class SubjectAlternativeNameHolder { private static final Pattern TAGS_PATTERN = Pattern.compile("[" + GeneralName.iPAddress + GeneralName.dNSName + "]"); private final List sans = new ArrayList(); public void addIpAddress(String ipAddress) { sans.add(new GeneralName(GeneralName.iPAddress, ipAddress)); } public void addDomainName(String subjectAlternativeName) { sans.add(new GeneralName(GeneralName.dNSName, subjectAlternativeName)); } public void fillInto(X509v3CertificateBuilder certGen) throws CertIOException { if (!sans.isEmpty()) { ASN1Encodable[] encodables = sans.toArray(new ASN1Encodable[sans .size()]); certGen.addExtension(Extension.subjectAlternativeName, false, new DERSequence(encodables)); } } public void addAll(Collection> subjectAlternativeNames) { if (subjectAlternativeNames != null) { for (List each : subjectAlternativeNames) { sans.add(parseGeneralName(each)); } } } private ASN1Encodable parseGeneralName(List nameEntry) { if (nameEntry != null && nameEntry.size() != 2) { throw new IllegalArgumentException(String.valueOf(nameEntry)); } String tag = String.valueOf(nameEntry.get(0)); Matcher m = TAGS_PATTERN.matcher(tag); if (m.matches()) { return new GeneralName(Integer.valueOf(tag), String.valueOf(nameEntry.get(1))); } throw new IllegalArgumentException(String.valueOf(nameEntry)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy