directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Osipov (Jira)" <j...@apache.org>
Subject [jira] [Created] (DIRKRB-741) ASN.1 decodes overflows on Turkish CA
Date Wed, 06 May 2020 16:56:00 GMT
Michael Osipov created DIRKRB-741:
-------------------------------------

             Summary: ASN.1 decodes overflows on Turkish CA
                 Key: DIRKRB-741
                 URL: https://issues.apache.org/jira/browse/DIRKRB-741
             Project: Directory Kerberos
          Issue Type: Bug
    Affects Versions: 2.0.0
            Reporter: Michael Osipov


I am trying to process the subjects of public CAs. Kerby ASN.1 chokes on one. Selfcontained
example:

{code;java}
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;

import javax.security.auth.x500.X500Principal;

import org.apache.kerby.asn1.Asn1;
import org.apache.kerby.asn1.type.Asn1Sequence;
import org.apache.kerby.asn1.type.Asn1Type;

public class KerbyTester {

	private static final String CERT_1 = "MIIGSzCCBDOgAwIBAgIIamg+nFGby1MwDQYJKoZIhvcNAQELBQAwgbIxCzAJBgNV"
			+ "BAYTAlRSMQ8wDQYDVQQHDAZBbmthcmExQDA+BgNVBAoMN0UtVHXEn3JhIEVCRyBC"
			+ "aWxpxZ9pbSBUZWtub2xvamlsZXJpIHZlIEhpem1ldGxlcmkgQS7Fni4xJjAkBgNV"
			+ "BAsMHUUtVHVncmEgU2VydGlmaWthc3lvbiBNZXJrZXppMSgwJgYDVQQDDB9FLVR1"
			+ "Z3JhIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTEzMDMwNTEyMDk0OFoXDTIz"
			+ "MDMwMzEyMDk0OFowgbIxCzAJBgNVBAYTAlRSMQ8wDQYDVQQHDAZBbmthcmExQDA+"
			+ "BgNVBAoMN0UtVHXEn3JhIEVCRyBCaWxpxZ9pbSBUZWtub2xvamlsZXJpIHZlIEhp"
			+ "em1ldGx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skZ1FSNqb4VjMIDw1Z4fKRzC"
			+ "bLBQWV2QWzuoDTDPv31/zvGdg73JRm4gpvlhUbohL3u+pRVjodSVh/GeufOJ8z2F"
			+ "uLjbvrW5KfnaNwUASZQDhETnv0Mxz3WLJdH0pmT1kvarBes96aULNmLazAZfNou2"
			+ "XjG4Kvte9nHfRCaexOYNkbQudZWAUWpLMKawYqGT8ZvYzsRjdT9ZR7E=";

	private static final String[] CERTS = new String[] { CERT_1, CERT_2 };

	public static void main(String[] args) throws CertificateException, IOException {
		for (String base64Cert : CERTS) {
			byte[] pemCert = Base64.getDecoder().decode(base64Cert);

			CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
			Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(pemCert));
			X509Certificate x509Cert = (X509Certificate) cert;

			X500Principal subject = x509Cert.getSubjectX500Principal();
			byte[] encoded = subject.getEncoded();

			System.out.println("Processing: " + subject.getName(X500Principal.RFC2253));

			Asn1Sequence asn1seq = (Asn1Sequence) Asn1.decode(encoded);
			byte[] recoded = new byte[asn1seq.getContainer().getBodyLength()];
			int offset = 0;
			for (Asn1Type asn1set : asn1seq.getValue()) {
				byte[] term = asn1set.encode();
				System.arraycopy(term, 0, recoded, offset, term.length);
				offset += term.length;
			}

			System.out.println("Unpacked RDNs: " + Base64.getEncoder().encodeToString(recoded));
		}
	}

}
{code}

The ouput is:

{noformat}
Processing: CN=E-Tugra Certification Authority,OU=E-Tugra Sertifikasyon Merkezi,O=E-Tuğra
EBG Bilişim Teknolojileri ve Hizmetleri A.Ş.,L=Ankara,C=TR
Exception in thread "main" java.nio.BufferOverflowException
	at java.base/java.nio.HeapByteBuffer.put(HeapByteBuffer.java:225)
	at java.base/java.nio.ByteBuffer.put(ByteBuffer.java:1031)
	at org.apache.kerby.asn1.type.Asn1Simple.encodeBody(Asn1Simple.java:79)
	at org.apache.kerby.asn1.type.Asn1Encodeable.encode(Asn1Encodeable.java:146)
	at org.apache.kerby.asn1.type.Asn1Constructed.encodeBody(Asn1Constructed.java:93)
	at org.apache.kerby.asn1.type.Asn1Encodeable.encode(Asn1Encodeable.java:146)
	at org.apache.kerby.asn1.type.Asn1Constructed.encodeBody(Asn1Constructed.java:93)
	at org.apache.kerby.asn1.type.Asn1Encodeable.encode(Asn1Encodeable.java:146)
	at org.apache.kerby.asn1.type.Asn1Encodeable.encode(Asn1Encodeable.java:136)
	at com.siemens.dynamowerk.certdownloader.KerbyTester.main(KerbyTester.java:96)
{noformat}

The cert is perfectly valid obtained from Mozilla's NSS bundle.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@directory.apache.org
For additional commands, e-mail: dev-help@directory.apache.org


Mime
View raw message