Retrieve Subject alternative names of X.509 certificate in java

前端 未结 2 1463
既然无缘
既然无缘 2021-01-18 02:35

I have tried using the solution provided in this link.

I am getting following error when i tried reading subject alternative names of X.509 Certificate

2条回答
  •  北海茫月
    2021-01-18 03:17

    I tried with your code for me it is working, I tested with a certificate exported from internet explorer

    Internet Explorer -> Tools -> Internet Options -> Content -> Certificates -> Untrusted Publishers -> www.google.com
    

    I exported this as ".cer", I made few changes to your code

    public static List getSubjectAlternativeNames(X509Certificate certificate) {
            List identities = new ArrayList();
            try {
                Collection> altNames = certificate.getSubjectAlternativeNames();
                if (altNames == null)
                    return Collections.emptyList();
                for (List item : altNames) {
                    Integer type = (Integer) item.get(0);
                    if (type == 0 || type == 2){
                        try {
                            ASN1InputStream decoder=null;
                            if(item.toArray()[1] instanceof byte[])
                                decoder = new ASN1InputStream((byte[]) item.toArray()[1]);
                            else if(item.toArray()[1] instanceof String)
                                identities.add( (String) item.toArray()[1] );
                            if(decoder==null) continue;
                            DEREncodable encoded = decoder.readObject();
                            encoded = ((DERSequence) encoded).getObjectAt(1);
                            encoded = ((DERTaggedObject) encoded).getObject();
                            encoded = ((DERTaggedObject) encoded).getObject();
                            String identity = ((DERUTF8String) encoded).getString();
                            identities.add(identity);
                        }
                        catch (UnsupportedEncodingException e) {
                            log.error("Error decoding subjectAltName" + e.getLocalizedMessage(),e);
                        }
                        catch (Exception e) {
                            log.error("Error decoding subjectAltName" + e.getLocalizedMessage(),e);
                        }
                    }else{
                        log.warn("SubjectAltName of invalid type found: " + certificate);
                    }
                }
            }
            catch (CertificateParsingException e) {
                log.error("Error parsing SubjectAltName in certificate: " + certificate + "\r\nerror:" + e.getLocalizedMessage(),e);
            }
            return identities;
        }
    

    I saved the file to c:\aa1.cer

    X509Certificate cert=null;
            FileInputStream fis = new FileInputStream("c:\\aa1.cer");
            BufferedInputStream bis = new BufferedInputStream(fis);
    
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            if (bis.available() > 0)
                try{
                   cert = (X509Certificate)cf.generateCertificate(bis);
                }
                catch (CertificateException e) {
                    e.printStackTrace();
                }
            System.out.println(CertificateInfo.getSubjectAlternativeNames(cert));
    

    I got the output as [www.google.com, google.com]

    Please check your certificate, I think the problem is your certificate

提交回复
热议问题