java.io.IOException: DER 长度的短读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45303597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:36:52  来源:igfitidea点击:

java.io.IOException: Short read of DER length

javajavafxcertificatejavafx-8x509certificate

提问by Markus Weninger

I'm trying to create a Java application that can read x509 certificates informations and display them in a TableView.

我正在尝试创建一个 Java 应用程序,它可以读取 x509 证书信息并将它们显示在TableView.

But i've a problem when i'm trying to display those informations with the code below

但是当我尝试使用下面的代码显示这些信息时遇到了问题

@FXML
private void handleHDD() throws CertificateException, IOException{
    String userDir = System.getProperty("user.home");
    File folder = new File(userDir +"\Desktop\Certificate_Folder");
    File[] certificates = folder.listFiles();
    if (certificates!=null){

        String columnHeader[] = {"Nom","Version","N°série","Algorithme de signature","Emetteur","Valide à partir de","Valide jusqu'au","Objet","Clé publique"};

        int sizeColumns = 9;

        for (File file : certificates){
            if(file.isFile()){
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            InputStream input = new FileInputStream(file);

            X509Certificate cert = (X509Certificate) cf.generateCertificate(input);
            input.close();
            for (int j = 0; j < sizeColumns; j++) {
                data.add(new CertificateModel(file.getName().replace(".der", ""),cert.getVersion(),cert.getSerialNumber().toString(),cert.getSigAlgName(),cert.getIssuerDN().toString(),cert.getNotBefore(),cert.getNotAfter(),cert.getSubjectDN().toString(),cert.getPublicKey().toString()));
                TableColumn col = new TableColumn();
                col.setText(columnHeader[j]);
                col.setMinWidth(200);
                col.setCellValueFactory(new PropertyValueFactory<CertificateModel, String>(columnHeader[j]));
                table.getColumns().addAll(col);
            }               
            table.setItems(data);

            }


            }

        }
    }

When i try to execute it, i hav the following exception: java.security.cert.CertificateException

当我尝试执行它时,出现以下异常: java.security.cert.CertificateException



This is the full exception stack:

这是完整的异常堆栈:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 33 more
Caused by: java.security.cert.CertificateException: Unable to initialize, java.io.IOException: Short read of DER length
    at sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:198)
    at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:102)
    at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339)
    at cm.camgovca.view.CertificateOverviewController.handleHDD(CertificateOverviewController.java:98)
    ... 43 more
Caused by: java.io.IOException: Short read of DER length
    at sun.security.util.DerInputStream.getLength(DerInputStream.java:582)
    at sun.security.util.DerValue.<init>(DerValue.java:252)
    at sun.security.util.DerInputStream.getDerValue(DerInputStream.java:451)
    at sun.security.x509.X509CertImpl.parse(X509CertImpl.java:1784)
    at sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:195)
    ... 46 more

The line 98 of CertificateOverview.java is

CertificateOverview.java 的第 98 行是

X509Certificate cert = (X509Certificate) cf.generateCertificate(input);

And my certificate is DER Encoded (.der)

我的证书是 DER 编码 (.der)

回答by Markus Weninger

If you look at the documentation of CertificateFactory#generateCertificateit says:

如果您查看CertificateFactory#generateCertificate它的文档,则说:

Throws: CertificateException - on parsing errors.

抛出: CertificateException - 解析错误。

So, as suggested in the comments, most probably your certificate file is not in the expected format.

因此,正如评论中所建议的,您的证书文件很可能不是预期的格式。

Especially the following line in the documentation may be of interest:

特别是文档中的以下行可能会引起人们的兴趣:

In the case of a certificate factory for X.509 certificates, the certificate provided in inStream must be DER-encoded and may be supplied in binary or printable (Base64) encoding. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by -----END CERTIFICATE-----.

对于 X.509 证书的证书工厂,inStream 中提供的证书必须是 DER 编码的,并且可以以二进制或可打印 (Base64) 编码提供。如果证书以Base64编码提供,则必须以-----BEGIN CERTIFICATE-----开头为界,以-----END CERTIFICATE-----结尾为界.

If you post an exception stack, with detailed exception messages, we probably can help more.

如果您发布异常堆栈以及详细的异常消息,我们可能会提供更多帮助。

回答by ISparkes

If you have a keystore defined, make sure that it is where you have defined and that the application can read it.

如果您定义了密钥库,请确保它是您定义的位置并且应用程序可以读取它。

In my case the hint was a bit lower down in the stack trace ("java.security.KeyStore.load"):

在我的情况下,提示在堆栈跟踪(“java.security.KeyStore.load”)中有点低:

Caused by: java.io.IOException: Short read of DER length
at sun.security.util.DerInputStream.getLength(DerInputStream.java:582) ~[na:1.8.0_212]
at sun.security.util.DerValue.init(DerValue.java:391) ~[na:1.8.0_212]
at sun.security.util.DerValue.<init>(DerValue.java:332) ~[na:1.8.0_212]
at sun.security.util.DerValue.<init>(DerValue.java:345) ~[na:1.8.0_212]
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1938) ~[na:1.8.0_212]
at java.security.KeyStore.load(KeyStore.java:1445) ~[na:1.8.0_212]
at org.apache.tomcat.util.security.KeyStoreUtil.load(KeyStoreUtil.java:67) ~[tomcat-embed-core-9.0.27.jar!/:9.0.27]