search
HomeBackend DevelopmentGolangtls.X509KeyPair pops up 'Unable to find any PEM data in certificate input' error

tls.X509KeyPair 弹出“无法在证书输入中找到任何 PEM 数据”错误

Question content

I am trying to make a tls certificate for https server through golang x509 package

I got this error

tls: failed to find any pem data in certificate input

After some research, I created my certificate like this

func generatecert()  {
    ca := &x509.certificate{
        serialnumber: big.newint(2023),
        subject: pkix.name{
            organization:       []string{"company"},
            organizationalunit: []string{"lol"},
            country:            []string{"us"},
            province:           []string{""},
            locality:           []string{"ny"},
            streetaddress:      []string{"no street"},
            postalcode:         []string{"77777"},
        },
        notbefore:             time.now(),
        notafter:              time.now().adddate(10, 0, 0),
        subjectkeyid:          []byte{1, 2, 3, 4, 5},
        basicconstraintsvalid: true,
        isca:                  true,
        extkeyusage:           []x509.extkeyusage{x509.extkeyusageclientauth, x509.extkeyusageserverauth},
        keyusage:              x509.keyusagedigitalsignature | x509.keyusagecertsign,
    }

    certpubl, certpriv, err := ed25519.generatekey(rand.reader)
    if err != nil {
        log.println("key generate failed", err)
        return
    }

    certcert, err := x509.createcertificate(rand.reader, ca, ca, certpubl, certpriv)
    if err != nil {
        log.println("create cert failed", err)
        return
    }

    out := &bytes.buffer{}

    //encoding cert
    certtestpem := &pem.block{type: "certificate", bytes: certcert}
    pem.encode(out, certtestpem)
    publiccert := out.bytes()
    certderblock, publiccert := pem.decode(publiccert)

    //check decoded cert
    print(certderblock.type, "\n")
    if publiccert != nil {
        print("publiccert nil\n")
    }

    //encoding private key
    out.reset()
    privatepem, _ := x509.marshalpkcs8privatekey(certpriv)
    pem.encode(out, &pem.block{type: "private key", bytes: privatepem})
    privitkey := out.bytes()

    //check keypair
    _, err = tls.x509keypair(publiccert, privitkey)
    if err != nil {
        print(err.error())
    }
}

It shows error as below

CERTIFICATE
publicCert nil
tls: failed to find any PEM data in certificate input

I try to decode after pem.encodetomemory

pem.type is correct but the variable "publiccert" is zero, I tried adding \n to the beginning of the certificate and it did nothing but the certificate itself is not zero, can anyone help me p>

What can I do to make tls work? ? ?


Correct answer


There are several problems with this code

publiccert := out.bytes()

Checking the contents of publiccert at this stage shows the expected value. But the following statement will simply override publiccert:

certderblock, publiccert := pem.decode(publiccert)

You can see this by examining the publiccert after this statement. As documented publiccert The data will now be displayed after the actual certificate.

This should be

certderblock, _ := pem.decode(publiccert)

Checking the publiccert content after this corrected statement again shows the expected value.

out.reset()
privatepem, _ := x509.marshalpkcs8privatekey(certpriv)
pem.encode(out, &pem.block{type: "private key", bytes: privatepem})
privitkey := out.bytes()

This will get the expected value into the privitkey. However, it will change publiccert because it is only part of out and out has changed the action. Therefore, out will now contain privitkey at the beginning instead of the beginning of the certificate - this is reflected in the value of publiccert.

See also the documentation for bytes.buffer.bytes

Slicing is only valid until the next buffer modification (that is, only valid before the next read, write, reset or truncate method is called)

So, not just resetting existing buffers

out.reset()

It is better to create a new buffer for privitkey and keep the existing buffer for publiccert

out = &bytes.Buffer{}

The above is the detailed content of tls.X509KeyPair pops up 'Unable to find any PEM data in certificate input' error. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete
如何解决win11无法初始化LOL游戏图形设备的问题如何解决win11无法初始化LOL游戏图形设备的问题Dec 26, 2023 pm 07:49 PM

我们在使用win11系统玩lol时,可能会出现提示无法初始化图形设备的问题,这可能是因为我们的电脑兼容性问题,也可能是因为显卡驱动问题,大家可以通过下面的方法依次尝试解决,下面就一起来看看吧。win11玩lol无法初始化图形设备怎么办:一、兼容性1、首先按下键盘“win+r”快捷键调出运行,然后输入“gpedit.msc”回车确定。2、打开组策略编辑器,进入“管理模板”再进入“Windows组件”3、接着找到“应用程序兼容性”,双击打开其中的“关闭程序兼容性助理”4、打开之后勾选其中的“已启用”

笔记本cpu玩游戏90度正常吗 详细讲解:笔记本温度标准详情笔记本cpu玩游戏90度正常吗 详细讲解:笔记本温度标准详情Feb 06, 2024 pm 05:48 PM

很多用户在使用笔记本电脑玩游戏时,发现CPU的温度达到了90℃以上,担心会对电脑造成什么损伤,本期文章以华硕、飞行堡垒6为例,说说关于“玩游戏CPU温度达到或超过90℃”的原因和解决办法。使用笔记本电脑玩游戏时,CPU温度达到90℃,是正常现象一般来说,笔记本电脑的CPU温度应该保持在40~75℃之间。然而,如果系统运行的软件较多,或者运行一些比较耗费系统资源的游戏或软件,CPU的工作量会增加,导致温度在短时间内升高。不过,当用户关闭这些软件时,温度会恢复到正常水平。因此,用户应该在使用电脑时注

解决LOL黑屏问题的Win11方法解决LOL黑屏问题的Win11方法Dec 25, 2023 pm 10:07 PM

win11系统已经正式发布了很久,不少玩家都选择了更新。但是在更新后,出现了win11玩LOL黑屏的问题,这其实是系统bug导致的,目前微软还没有修复,我们只能尝试手动修改注册表的方法试图解决。win11玩LOL黑屏:方法一:1、使用键盘组合键“Win+R”打开运行。2、打开后输入“regedit”,点击“确定”,打开注册表。3、将以下路径复制到路径栏:“计算机\HKEY_CURRENT_USER\ControlPanel\Desktop”4、找到并打开其中的“ForegroundLockTim

解决win11lol游戏无法启动的方法解决win11lol游戏无法启动的方法Dec 26, 2023 am 10:25 AM

根据网友的测试,win11在游玩英雄联盟时的流畅性和性能都非常不错,因此很多用户都更新到win11来玩这款游戏,不过一些朋友在更新后遇到了win11打不开lol的情况,这可能是因为驱动的问题,下面就一起来解决一下吧。win11lol进不去游戏怎么办方法一:1、首先我们打开自己的英伟达显卡面板,然后在右上角点击齿轮状“设置”按钮。2、接着在其中找到“游戏内覆盖”选项,将右侧的开关给关闭即可。方法二:1、右键选中此电脑,在下拉菜单中选择“管理”2、在计算机管理中找到“设备管理器”,接着展开右侧的“显

解决win11中lol游戏没有声音的方法解决win11中lol游戏没有声音的方法Dec 23, 2023 pm 10:53 PM

玩游戏的时候声音是非常重要的,没了声音会大大降低游戏体验。最近就有玩家用win11玩lol没有声音,这可能是由于我们关闭或禁用了游戏中的音效,也可能是没有开启游戏声音,下面就一起来看看怎么解决吧。win11玩lol没有声音:方法一:1、首先,我们需要确认一下自己的电脑是否有声音。2、如果电脑也没有声音,那么可能是静音了,点击右下角喇叭打开即可。3、也有可能是没有安装声卡或音响设备的驱动,在“设备管理器”中可以更新驱动。4、还可能是你的声音播放设备坏了,建议检查一下耳机或音响是否可以使用。方法二:

win10玩lol间断性卡顿卡屏该怎么办win10玩lol间断性卡顿卡屏解决方案win10玩lol间断性卡顿卡屏该怎么办win10玩lol间断性卡顿卡屏解决方案Jul 14, 2023 pm 12:45 PM

Lol是LOL,许多win10客户们都喜歡玩这一moba游戏,虽然此手游的配备规定不高,但是有的客户却经常会产生自身玩的情况下间断性卡顿的问题,那麼win10玩lol间断性卡顿卡屏怎么办呢,一般情形下客户遇到此问题不用担忧,根据这儿的win10玩lol间断性卡顿卡屏解决方案,就能处理该问题了。win10玩lol间断性卡顿卡屏该怎么办win10玩lol间断性卡顿卡屏是由于电脑虚拟内存不够,解决方案如下所示:1、最先点击开启“我的电脑”中的“属性”,如下图所示:2、点击左边第四个菜单栏“高端系统软件

amd a8处理器怎么样「详细介绍:AMD A8-7680处理器评测」amd a8处理器怎么样「详细介绍:AMD A8-7680处理器评测」Feb 06, 2024 pm 07:20 PM

一、前言:你没看错!古老FM2+平台又迎来了新成员在2018年10月,AMD发布了一款属于Carrizo家族的A8-7680APU处理器。这款处理器采用了第六代挖掘机构架的APU,并且处理器接口为FM2+,而非最新的AM4。笔者刚刚收到这块处理器时感到惊讶,没想到AMD在锐龙处理器推出两年后仍能推出这样一款上代APU。不过,冷静下来分析,其实A8-7680也有一定的存在意义。FM2+平台支持AMD专用DDR3内存条,其性能与普通DDR3内存相当,但价格却不到一半。因此,对性能要求不高的用户可以选

解决LOL游戏中没有声音的方法教程解决LOL游戏中没有声音的方法教程Jan 15, 2024 am 10:03 AM

电脑正常,一直到选择英雄游戏都正常有声音,但是一进入游戏以后,就没声音了,英雄也不说话,系统全军出击那是啥啥的也没有,咋回事啊!游戏也重装过了!这是来自一位LOL用户的提问,那么该如何解决呢?不懂的用户一起来看看玩LOL时没有声音的解决措施吧。《英雄联盟》是由美国RiotGames开发,中国大陆地区由腾讯游戏运营的网络游戏,目前是玩的最多的游戏之一,但是玩的多了,也会遇到一些问题,例如玩LOL时没有声音,这该怎么办呢?下面,小编就来跟大家来说说解决玩LOL时没有声音如何恢复。玩LOL时没有声音怎

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.