use of org.webpki.asn1.ASN1String in project bitcoinj by bitcoinj.
the class X509Utils method getDisplayNameFromCertificate.
/**
* Returns either a string that "sums up" the certificate for humans, in a similar manner to what you might see
* in a web browser, or null if one cannot be extracted. This will typically be the common name (CN) field, but
* can also be the org (O) field, org+location+country if withLocation is set, or the email
* address for S/MIME certificates.
*/
@Nullable
public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException {
X500Name name = new X500Name(certificate.getSubjectX500Principal().getName());
String commonName = null, org = null, location = null, country = null;
for (RDN rdn : name.getRDNs()) {
AttributeTypeAndValue pair = rdn.getFirst();
String val = ((ASN1String) pair.getValue()).getString();
ASN1ObjectIdentifier type = pair.getType();
if (type.equals(RFC4519Style.cn))
commonName = val;
else if (type.equals(RFC4519Style.o))
org = val;
else if (type.equals(RFC4519Style.l))
location = val;
else if (type.equals(RFC4519Style.c))
country = val;
}
final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames();
String altName = null;
if (subjectAlternativeNames != null)
for (final List<?> subjectAlternativeName : subjectAlternativeNames) if (// rfc822name
(Integer) subjectAlternativeName.get(0) == 1)
altName = (String) subjectAlternativeName.get(1);
if (org != null) {
return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org;
} else if (commonName != null) {
return commonName;
} else {
return altName;
}
}
use of org.webpki.asn1.ASN1String in project Falcon-File-Transfer-Optimizer by arif-zaman.
the class X509NameHelper method toString.
private static String toString(ASN1Sequence seq) {
if (seq == null) {
return null;
}
Enumeration e = seq.getObjects();
StringBuffer buf = new StringBuffer();
while (e.hasMoreElements()) {
ASN1Set set = (ASN1Set) e.nextElement();
Enumeration ee = set.getObjects();
buf.append('/');
while (ee.hasMoreElements()) {
ASN1Sequence s = (ASN1Sequence) ee.nextElement();
ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) s.getObjectAt(0);
String sym = (String) X509Name.DefaultSymbols.get(oid);
if (sym == null) {
buf.append(oid.getId());
} else {
buf.append(sym);
}
buf.append('=');
buf.append(((ASN1String) s.getObjectAt(1)).getString());
if (ee.hasMoreElements()) {
buf.append('+');
}
}
}
return buf.toString();
}
use of org.webpki.asn1.ASN1String in project cas by apereo.
the class X509UPNExtractorUtils method getUPNStringFromSequence.
/**
* Get UPN String.
*
* @param seq ASN1Sequence abstraction representing subject alternative name.
* First element is the object identifier, second is the object itself.
* @return UPN string or null
*/
private String getUPNStringFromSequence(final ASN1Sequence seq) {
val id = seq != null ? ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0)) : null;
if (id != null && UPN_OBJECTID.equals(id.getId())) {
val obj = (ASN1TaggedObject) seq.getObjectAt(1);
val primitiveObj = obj.getObject();
val func = FunctionUtils.doIf(Predicates.instanceOf(ASN1TaggedObject.class), () -> ASN1TaggedObject.getInstance(primitiveObj).getObject(), () -> primitiveObj);
val prim = func.apply(primitiveObj);
if (prim instanceof ASN1OctetString) {
return new String(((ASN1OctetString) prim).getOctets(), StandardCharsets.UTF_8);
}
if (prim instanceof ASN1String) {
return ((ASN1String) prim).getString();
}
}
return null;
}
use of org.webpki.asn1.ASN1String in project robovm by robovm.
the class IETFUtils method canonicalize.
public static String canonicalize(String s) {
String value = Strings.toLowerCase(s.trim());
if (value.length() > 0 && value.charAt(0) == '#') {
ASN1Primitive obj = decodeObject(value);
if (obj instanceof ASN1String) {
value = Strings.toLowerCase(((ASN1String) obj).getString().trim());
}
}
value = stripInternalSpaces(value);
return value;
}
use of org.webpki.asn1.ASN1String in project XobotOS by xamarin.
the class IETFUtils method valueToString.
public static String valueToString(ASN1Encodable value) {
StringBuffer vBuf = new StringBuffer();
if (value instanceof ASN1String && !(value instanceof DERUniversalString)) {
String v = ((ASN1String) value).getString();
if (v.length() > 0 && v.charAt(0) == '#') {
vBuf.append("\\" + v);
} else {
vBuf.append(v);
}
} else {
vBuf.append("#" + bytesToString(Hex.encode(value.getDERObject().getDEREncoded())));
}
int end = vBuf.length();
int index = 0;
if (vBuf.length() >= 2 && vBuf.charAt(0) == '\\' && vBuf.charAt(1) == '#') {
index += 2;
}
while (index != end) {
if ((vBuf.charAt(index) == ',') || (vBuf.charAt(index) == '"') || (vBuf.charAt(index) == '\\') || (vBuf.charAt(index) == '+') || (vBuf.charAt(index) == '=') || (vBuf.charAt(index) == '<') || (vBuf.charAt(index) == '>') || (vBuf.charAt(index) == ';')) {
vBuf.insert(index, "\\");
index++;
end++;
}
index++;
}
return vBuf.toString();
}
Aggregations