|
17 | 17 |
|
18 | 18 | package org.opensearch.security.ssl; |
19 | 19 |
|
| 20 | +import com.google.common.collect.ImmutableList; |
20 | 21 | import io.netty.buffer.PooledByteBufAllocator; |
21 | 22 | import io.netty.handler.ssl.ApplicationProtocolConfig; |
22 | 23 | import io.netty.handler.ssl.ClientAuth; |
|
58 | 59 | import javax.net.ssl.SSLException; |
59 | 60 | import javax.net.ssl.SSLParameters; |
60 | 61 |
|
| 62 | +import org.bouncycastle.asn1.ASN1Object; |
61 | 63 | import org.opensearch.security.ssl.util.CertFileProps; |
62 | 64 | import org.opensearch.security.ssl.util.CertFromFile; |
63 | 65 | import org.opensearch.security.ssl.util.CertFromKeystore; |
@@ -985,34 +987,27 @@ public String getSubjectAlternativeNames(X509Certificate cert) { |
985 | 987 | } |
986 | 988 |
|
987 | 989 | private List<String> getOtherName(List<?> altName) { |
988 | | - ASN1Primitive oct = null; |
989 | | - try { |
990 | | - byte[] altNameBytes = (byte[]) altName.get(1); |
991 | | - oct = (new ASN1InputStream(new ByteArrayInputStream(altNameBytes)).readObject()); |
992 | | - } catch (IOException e) { |
993 | | - throw new RuntimeException("Could not read ASN1InputStream", e); |
994 | | - } |
995 | | - if (oct instanceof ASN1TaggedObject) { |
996 | | - oct = ((ASN1TaggedObject) oct).getObject(); |
997 | | - } |
998 | | - ASN1Sequence seq = ASN1Sequence.getInstance(oct); |
999 | | - |
1000 | | - // Get object identifier from first in sequence |
1001 | | - ASN1ObjectIdentifier asnOID = (ASN1ObjectIdentifier) seq.getObjectAt(0); |
1002 | | - String oid = asnOID.getId(); |
1003 | | - |
1004 | | - // Get value of object from second element |
1005 | | - final ASN1TaggedObject obj = (ASN1TaggedObject) seq.getObjectAt(1); |
1006 | | - // Could be tagged twice due to bug in java cert.getSubjectAltName |
1007 | | - ASN1Primitive prim = obj.getObject(); |
1008 | | - if (prim instanceof ASN1TaggedObject) { |
1009 | | - prim = ASN1TaggedObject.getInstance(((ASN1TaggedObject) prim)).getObject(); |
1010 | | - } |
1011 | | - |
1012 | | - if (prim instanceof ASN1String) { |
1013 | | - return Collections.unmodifiableList(Arrays.asList(oid, ((ASN1String) prim).getString())); |
| 990 | + if (altName.size() < 2) { |
| 991 | + log.warn("Couldn't parse subject alternative names"); |
| 992 | + return null; |
| 993 | + } |
| 994 | + try (final ASN1InputStream in = new ASN1InputStream((byte[]) altName.get(1))) { |
| 995 | + final ASN1Primitive asn1Primitive = in.readObject(); |
| 996 | + final ASN1Sequence sequence = ASN1Sequence.getInstance(asn1Primitive); |
| 997 | + final ASN1ObjectIdentifier asn1ObjectIdentifier = ASN1ObjectIdentifier.getInstance(sequence.getObjectAt(0)); |
| 998 | + final ASN1TaggedObject asn1TaggedObject = ASN1TaggedObject.getInstance(sequence.getObjectAt(1)); |
| 999 | + ASN1Object maybeTaggedAsn1Primitive = asn1TaggedObject.getBaseObject(); |
| 1000 | + if (maybeTaggedAsn1Primitive instanceof ASN1TaggedObject) { |
| 1001 | + maybeTaggedAsn1Primitive = ASN1TaggedObject.getInstance(maybeTaggedAsn1Primitive).getBaseObject(); |
| 1002 | + } |
| 1003 | + if (maybeTaggedAsn1Primitive instanceof ASN1String) { |
| 1004 | + return ImmutableList.of(asn1ObjectIdentifier.getId(), maybeTaggedAsn1Primitive.toString()); |
| 1005 | + } else { |
| 1006 | + log.warn("Couldn't parse subject alternative names"); |
| 1007 | + return null; |
| 1008 | + } |
| 1009 | + } catch (final Exception ioe) { // catch all exception here since BC throws diff exceptions |
| 1010 | + throw new RuntimeException("Couldn't parse subject alternative names", ioe); |
1014 | 1011 | } |
1015 | | - |
1016 | | - return null; |
1017 | 1012 | } |
1018 | 1013 | } |
0 commit comments