Home >Backend Development >Golang >Go: Get value of 'subject/unstructuredName' from ssl certificate

Go: Get value of 'subject/unstructuredName' from ssl certificate

PHPz
PHPzforward
2024-02-09 13:03:201114browse

Go:从 ssl 证书获取“subject/unstructeredName”的值

In this article, php editor Xinyi will introduce how to obtain the value of "subject/unstructuredName" from the SSL certificate through the Go language. The SSL certificate is the basis for website security, and the "subject" field contains relevant information about the website. The "unstructuredName" field provides further details. Knowing how to extract these values ​​can help us better understand and manage SSL certificates and improve the security and credibility of the website. In what follows, we'll walk through this process in detail with example code and explanations.

Question content

I am trying to migrate some python code to golang. The code needs to get the value of unstructuredname from the ssl certificate subject.

This is how it works in python:

from cryptography import x509
from cryptography.x509.oid import nameoid
from cryptography.hazmat.backends import default_backend

pem = b"""\
-----begin certificate-----
miidctccalmgawibagiunjbltje6i/s9pjn8kgixyi2iw58wdqyjkozihvcnaqel
bqawsteumbiga1ueawwlzxhhbxbszs5jb20xfjaubgnvbaomduv4yw1wbgusielu
yy4xgtaxbgnvbasmeev4yw1wbgugrgl2axnpb24whhcnmjixmti5mtu1ndmxwhcn
mzcxmti2mtu1ndmxwjcbmzelmakga1uebhmcvvmxezarbgnvbagmcknhbglmb3ju
awexfjaubgnvbacmdvnhbibgcmfuy2lzy28xgjaybgnvbaomevnvbwugt3jnyw5p
emf0aw9umr4whaydvqqddbvzb21llm9yz2fuaxphdglvbi5jb20xizahbgkqhkig
9w0bcqimfhnvbwugywrkaxrpb25hbcbpbmzvmiibijanbgkqhkig9w0baqefaaoc
aq8amiibcgkcaqeayqprugdiaerkahbx06wobpekkz8cfyir6dv8d49eqho1xmcp
avotfjsj5cktuh64885lftcghudpcw+rih0fb8tolebua9yimuqbyfnrglrz7u+g
bkgyqo7w0psb7tguki7eseecvwsoygwyzksiuzbsqoauecjxog7gmr6qiutmilaq
dy0wwlir7iakx0qlq8ihfy6ynf2aghet6ic9tgbkifo6hwlep4gbwwtivcd94l6l
6rnpgtyxhsmlael0jn/4qpadcixdrbnahffzbrqyo7synqjuienxevq+7esiqibv
rkvt9hpaj+o/ij4z+z6xys8se3re4fsnsdxphqidaqabma0gcsqgsib3dqebcwua
a4ibaqch6vxtrqubfvgahnmjpshci3jcaz/x9hl2qgxntzas3rljbd6orc+3itli
vzjlwryl1hztxfecj3etvg1acknohx0rcfggnbsqsoz6oaazls6bakchbruqmhfg
icdk8a1pv1cr/spbe/ujskbg548jpqcsweul1jndsnzzmjuxy+ftm2pdyisrbgxy
y0spkxahukltblzghh1kimu7uuteuxdgmlccyey9/ibwtka9j/wnjc0cqc13jle8
pqvliexiovsktsgbzwyxw2p+m6qbjrc83oorwm8gnej1k2yx6jmnzlifidfit/h6
m2y3gjyqfgexupmk9toeejzpj1rb
-----end certificate-----
"""

cert = x509.load_pem_x509_certificate(pem, default_backend())
unstructured_name = cert.subject.get_attributes_for_oid(nameoid.unstructured_name)[0].value
print(unstructured_name)

This code prints the value of unstructuredname (in this case "some additional information").

My attempt in go looks like this:

package main

import (
    "crypto/x509"
    "encoding/pem"
    "fmt"

    "log"
)

const certRaw = `-----BEGIN CERTIFICATE-----
MIIDcTCCAlmgAwIBAgIUNjbltje6I/S9PJN8KgIxyi2Iw58wDQYJKoZIhvcNAQEL
BQAwSTEUMBIGA1UEAwwLZXhhbXBsZS5jb20xFjAUBgNVBAoMDUV4YW1wbGUsIElu
Yy4xGTAXBgNVBAsMEEV4YW1wbGUgRGl2aXNpb24wHhcNMjIxMTI5MTU1NDMxWhcN
MzcxMTI2MTU1NDMxWjCBmzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3Ju
aWExFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xGjAYBgNVBAoMEVNvbWUgT3JnYW5p
emF0aW9uMR4wHAYDVQQDDBVzb21lLm9yZ2FuaXphdGlvbi5jb20xIzAhBgkqhkiG
9w0BCQIMFHNvbWUgYWRkaXRpb25hbCBpbmZvMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAyqprugdiaERkAHbx06WobPekkZ8CFyiR6dv8D49eqHO1xmCp
aVotFJSj5cktUH64885LfTCghUdPcw+rIh0fB8tOleBUA9yIMUqbYFnRgLrz7U+g
bkgyQO7w0PsB7tgUKI7esEEcvWSoygWyZKSiUZBSQOAuEcJxog7GMR6QiUtmiLaq
DY0Wwlir7iakX0Qlq8iHFY6yNf2aghET6IC9tGbkifO6HWLEP4gBWwtIvCd94l6L
6rNpGtYXHSmlaEL0jn/4QPADciXDrBnAhFfzBrqyO7SYNqJUiENXEvq+7ESIqibV
RKvt9HpAj+O/Ij4z+Z6Xys8sE3RE4FSNSdXphQIDAQABMA0GCSqGSIb3DQEBCwUA
A4IBAQCH6vxtrqUBFVgahnmjPSHci3JCaz/X9hL2QgXNTzAS3rLJBd6OrC+3ITli
vzJlWryl1hzTxFEcj3EtVG1AckNOHX0RCFggNBSQSOz6oaaZlS6BAkChbRUqmHfG
iCDK8A1pV1cr/SPBe/UjSkbG548JpqCSWEUL1jNDsNzZmjuXy+FTM2PDYiSRbGxY
Y0SpkxAhuKltblzghh1KIMU7UutEUXDGMlCCYEY9/ibWTKa9J/WNjC0CQC13jLe8
PqVLIeXIoVSkTsgbzwYxW2P+M6QbjRC83OORwm8GnEj1k2yX6JmnzLifidFiT/h6
M2Y3gjYqfGexupmK9toEejZPj1RB
-----END CERTIFICATE-----
`

func main() {
    block, rest := pem.Decode([]byte(certRaw))
    if block == nil {
        log.Fatalf("Decode CA PEM, %v", rest)
    }
    cert, err := x509.ParseCertificate(block.Bytes)
    if err != nil {
        log.Fatalf("parse certificate")
    }
    fmt.Println(cert.Subject.String())
    // Unmarshall and print value of unstructuredName (oid: 1.2.840.113549.1.9.2)
    // ???
}

Go to the playground: https://go.dev/play/p/vwkpdbnpq78

I can't find a function to get attributes via oid. When I print out the entire topic, I see that the value is still (der?) encoded.

How to decode or unmarshal the value of unstructuredname?

Thank you so much.

Solution

I think the go standard library does not have a function to obtain attributes through oid. However, writing one yourself doesn't look too difficult. The code below is based on this related answer (and the code in your question).

This is the updated go playground - https://www.php.cn/link/f0eb6568ea114ba6e293f903c34d7488一个>.

package main

import (
    "crypto/x509"
    "encoding/pem"
    "fmt"

    "log"
)

const certRaw = `-----BEGIN CERTIFICATE-----
MIIDcTCCAlmgAwIBAgIUNjbltje6I/S9PJN8KgIxyi2Iw58wDQYJKoZIhvcNAQEL
BQAwSTEUMBIGA1UEAwwLZXhhbXBsZS5jb20xFjAUBgNVBAoMDUV4YW1wbGUsIElu
Yy4xGTAXBgNVBAsMEEV4YW1wbGUgRGl2aXNpb24wHhcNMjIxMTI5MTU1NDMxWhcN
MzcxMTI2MTU1NDMxWjCBmzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3Ju
aWExFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xGjAYBgNVBAoMEVNvbWUgT3JnYW5p
emF0aW9uMR4wHAYDVQQDDBVzb21lLm9yZ2FuaXphdGlvbi5jb20xIzAhBgkqhkiG
9w0BCQIMFHNvbWUgYWRkaXRpb25hbCBpbmZvMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAyqprugdiaERkAHbx06WobPekkZ8CFyiR6dv8D49eqHO1xmCp
aVotFJSj5cktUH64885LfTCghUdPcw+rIh0fB8tOleBUA9yIMUqbYFnRgLrz7U+g
bkgyQO7w0PsB7tgUKI7esEEcvWSoygWyZKSiUZBSQOAuEcJxog7GMR6QiUtmiLaq
DY0Wwlir7iakX0Qlq8iHFY6yNf2aghET6IC9tGbkifO6HWLEP4gBWwtIvCd94l6L
6rNpGtYXHSmlaEL0jn/4QPADciXDrBnAhFfzBrqyO7SYNqJUiENXEvq+7ESIqibV
RKvt9HpAj+O/Ij4z+Z6Xys8sE3RE4FSNSdXphQIDAQABMA0GCSqGSIb3DQEBCwUA
A4IBAQCH6vxtrqUBFVgahnmjPSHci3JCaz/X9hL2QgXNTzAS3rLJBd6OrC+3ITli
vzJlWryl1hzTxFEcj3EtVG1AckNOHX0RCFggNBSQSOz6oaaZlS6BAkChbRUqmHfG
iCDK8A1pV1cr/SPBe/UjSkbG548JpqCSWEUL1jNDsNzZmjuXy+FTM2PDYiSRbGxY
Y0SpkxAhuKltblzghh1KIMU7UutEUXDGMlCCYEY9/ibWTKa9J/WNjC0CQC13jLe8
PqVLIeXIoVSkTsgbzwYxW2P+M6QbjRC83OORwm8GnEj1k2yX6JmnzLifidFiT/h6
M2Y3gjYqfGexupmK9toEejZPj1RB
-----END CERTIFICATE-----
`

func main() {
    block, rest := pem.Decode([]byte(certRaw))
    if block == nil {
        log.Fatalf("Decode CA PEM, %v", rest)
    }
    cert, err := x509.ParseCertificate(block.Bytes)
    if err != nil {
        log.Fatalf("parse certificate")
    }

    // 1.2.840.113549.1.9.2
    var oidUnstructuredName = []int{1, 2, 840, 113549, 1, 9, 2}
    var unstructuredName string

    for _, n := range cert.Subject.Names {
        if n.Type.Equal(oidUnstructuredName) {
            if v, ok := n.Value.(string); ok {
                unstructuredName = v
            }
        }
    }

    fmt.Println(unstructuredName)
}

The above is the detailed content of Go: Get value of 'subject/unstructuredName' from ssl certificate. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete