


Issue providing ExtendedKeyUsage information to CSR during build in golang
During CSR generation in golang, a problem arose in providing information to ExtendedKeyUsage. This issue may affect the correctness and validity of the generated CSR. ExtendedKeyUsage is part of the certificate extension and is used to specify the purpose of using the certificate. By providing correct ExtendedKeyUsage information to the CSR, the availability and security of the certificate in specific scenarios can be ensured. However, in golang, sometimes you encounter the problem that ExtendedKeyUsage information cannot be provided correctly when generating CSR. PHP editor Xigua will introduce the cause and solution of this problem in detail in this article to help readers solve similar problems and successfully generate CSR that meets the requirements.
Question content
I stumbled upon a strange problem. I'm writing a small golang tool that generates csr based on some user-supplied input. I mostly succeeded in achieving my goal, but ran into issues with extendedkeyusage. Simply put, it doesn't work.
asn1 marshaling codes for some x509 fields:
var oidemailaddress = asn1.objectidentifier{1, 2, 840, 113549, 1, 9, 1} var oidextensionkeyusage = asn1.objectidentifier{2, 5, 29, 15} var oidextensionextendedkeyusage = asn1.objectidentifier{2, 5, 29, 37} asn1keyusagedigsig, err := asn1.marshal(asn1.bitstring{ bytes: []byte{byte(x509.keyusagedigitalsignature)}, bitlength: 8, }) asn1keyusagedatenc, err := asn1.marshal(asn1.bitstring{ bytes: []byte{byte(x509.keyusagedataencipherment)}, bitlength: 8, }) asn1keyusagecauth, err := asn1.marshal(asn1.bitstring{ bytes: []byte{byte(x509.extkeyusageclientauth)}, bitlength: 8, }) if err != nil { error.fatalf("can't serialize extended key usage %s", err) }
Then I created a template and successfully generated and saved csr, almost:
template := x509.certificaterequest{ rawsubject: asn1subj, emailaddresses: []string{emailaddress}, signaturealgorithm: _sigalg, extraextensions: []pkix.extension{ { id: oidextensionextendedkeyusage, value: asn1keyusagecauth, }, { id: oidextensionkeyusage, critical: true, value: asn1keyusagedatenc, }, { id: oidextensionkeyusage, critical: true, value: asn1keyusagedigsig, }, }, } csrbytes, _ := x509.createcertificaterequest(rand.reader, &template, privatekey)
and here is an openssl req -in my_output.csr -text -noout
****** asn1 oid: prime256v1 nist curve: p-256 attributes: requested extensions: x509v3 subject alternative name: email:[email protected] x509v3 extended key usage: .... x509v3 key usage: critical key agreement x509v3 key usage: critical encipher only signature algorithm: ecdsa-with-sha256 ******
My extendedkeyusage is empty, while it should be clientauthentication. What did i do wrong?
I look forward to seeing:
X509v3 Extended Key Usage: ClientAuthentication
What I see is an empty field. I tried using a different set of bytes from another oid but still no results. It's like the extendedkeyusage field doesn't allow anything to be written (although it should)
If imported:
Version: go1.19.3 darwin/amd64
Solution
I think the problem is when printing the data. The keys/values actually exist in the data.
From the code:
var oidextensionextendedkeyusage = asn1.objectidentifier{2, 5, 29, 37} asn1keyusagecauth, err := asn1.marshal(asn1.bitstring{ bytes: []byte{byte(x509.extkeyusageclientauth)}, bitlength: 8, }) extraextensions: []pkix.extension{ { id: oidextensionextendedkeyusage, //critical: true, value: asn1keyusagecauth, //value: {2, 5, 29, 15}, },
oidextensionextendedkeyusage is asn.1 oid 2.5.29.37, which when encoded using the der encoder will be "55 1d 25"
You can encode it online to see what binary it will generate (e.g. https://www.php.cn/link/8e08227323cd829e449559bb381484b7)
asn1keyusagecauth value is 2 (constant defined in x509.go), which will be "00 02" when encoded to asn.1 bit string using der encoder (the first 00 is the number of padding bits (none), 02 is the number of padding digits) value 2)
Now get the base64 value of the certificate request and decode it using the asn.1 der decoder (eg: https://asn1.io/asn1playground)
miibtzccav0caqawgzwxczajbgnvbaytakfvmq8wdqydvqqiewztewruzxkxdzan bgnvbactbln5zg5letetmbega1uechmkc210aensawvuddelmakga1uecxmcsvqx jtajbgnvbamthhntdggtq2xpzw50lvk4cdg1bk1psvnzmgliz0exijagbgkqhkig 9w0bcqeme3ntdghjbgllbnrac210ac5jb20wwtatbgcqhkjopqibbggqhkjopqmb bwncaar4riguoxsyxdaml9f9e2grjumuk8q0jilotb2kadmbz1rocedszuuxkqcr 0vud2aw3vidph1ar4hkqwkm43hxqof4wxayjkozihvcnaqkomu8wttaebgnvhree fzavgrnzbxroy2xpzw50qhntdgguy29tmasga1udjqqeawiiaajaobgnvhq8baf8e bamcaagwdgydvr0paqh/baqdagabmaogccqgsm49bamca0gameuciqdtbj 0atjy f1gy8am2mv7/x3tsebmmvdszkw8l6rvseqigmih8co9nkp0axdmgp9x4kvjjzk9x rw3roydt89d73oa=
try the full power of oss' asn-1step by downloading a free trial oss nokalva tlv print utility version 8.6.1 copyright (c) 1997-2022 oss nokalva, inc. all rights reserved. 30 8201b7(439) 30 82015d(349) 02 01 00 30 819c(156) 31 0b 30 09 06 03 550406 13 02 4155 31 0f 30 0d 06 03 550408 13 06 5379646e6579 31 0f 30 0d 06 03 550407 13 06 5379646e6579 31 13 30 11 06 03 55040a 13 0a 736d7468436c69656e74 31 0b 30 09 06 03 55040b 13 02 4954 31 25 30 23 06 03 550403 13 1c 736d74682d436c69656e742d59387038356e4d694953733069486741 31 22 30 20 06 09 2a864886f70d010901 0c 13 736d7468636c69656e7440736d74682e636f6d 30 59 30 13 06 07 2a8648ce3d0201 06 08 2a8648ce3d030107 03 42 000478ac88143b14b25dd68c2fd17d7b68118ee3142bc4348e29684dbda401d9... a0 5e 30 5c 06 09 2a864886f70d01090e 31 4f 30 4d 30 1e 06 03 551d11 04 17 30158113736d7468636c69656e7440736d74682e636f6d 30 0b -- here it is! 06 03 551d25 04 04 03020002 30 0e 06 03 551d0f 01 01 ff 04 04 03020008 30 0e 06 03 551d0f 01 01 ff 04 04 03020001 30 0a 06 08 2a8648ce3d040302 03 48 003045022100d3063fb402d8f2175198f0033632feff5f7b6c11b98c55db332b0f25... results to get more details, please provide/compile a schema for your data.
Scroll down the output above until you find here it is!
Your key/value is:
30 0B -- a SEQUENCE of 11 bytes 06 03 551D25 -- an item of 3 bytes (551D25 ... OidExtensionExtendedKeyUsage) 04 04 03020002 -- an item of 4 bytes (03 02 0002 ... an item of 2 bytes 0002 ... asn1KeyUsageCAuth)
I'd love to decode the csr according to the asn.1 spec...but I can't find it :(
The above is the detailed content of Issue providing ExtendedKeyUsage information to CSR during build in golang. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.
