Go: Get value of 'subject/unstructuredName' from ssl certificate
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/S9PJN8KgIxyi2Iw58wDQYJKo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ORwm8GnEj1k2yX6JmnzLifidFiT/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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

There is no function named "sum" in the C language standard library. "sum" is usually defined by programmers or provided in specific libraries, and its functionality depends on the specific implementation. Common scenarios are summing for arrays, and can also be used in other data structures, such as linked lists. In addition, "sum" is also used in fields such as image processing and statistical analysis. An excellent "sum" function should have good readability, robustness and efficiency.

The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

Why does map iteration in Go cause all values to become the last element? In Go language, when faced with some interview questions, you often encounter maps...
