Thursday, May 16, 2024
HomeGolangParsing non-public key with x509 bundle - Getting Assist

Parsing non-public key with x509 bundle – Getting Assist


Hey everybody.

I’m utilizing the x509 bundle and I’m making an attempt to parse the non-public key saved in my .env file utilizing the x509.ParsePKCS8PrivateKey methodology.

The tactic throws the next error: “asn1: construction error: tags don’t match (2 vs {class:0 tag:16 size:95 isCompound:true}) {non-obligatory:false specific:false utility:false non-public:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} int @2”.

I asume that the error is brought on because of the format of the non-public key within the .env file and the truth that a passphrase was used to generated. (I used the openssl command).
The format of the non-public key the .env file is the next
PRIVATE_KEY=”—–BEGIN ENCRYPTED PRIVATE KEY—–
blablablabla
—–END ENCRYPTED PRIVATE KEY—–

Right here’s is my code

privateKeyPEM := os.Getenv("PRIVATE_KEY")

	// get non-public key from .env file
	block, _ := pem.Decode([]byte(privateKeyPEM))
	if block == nil {
		http.Error(w, "{"error": "Error decoding non-public key"}", http.StatusBadRequest)
		return
	}

	privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		fmt.Println(err.Error())
		http.Error(w, "{"error": "Error parsing non-public key"}", http.StatusBadRequest)
		return
	}

The purpose of this code is to make use of the non-public key inside an http handler with a view to decrypt some information coming from the consumer. After I obtain the info I name the rsa.DecryptPKCS1v15(rand.Reader, privateKey, ecryptedData) to decrypt the info, however the code by no means reaches this level.

Any concepts can be appreciated. My Go model is 1.21

Thanks upfront

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments