Skip to content

Commit

Permalink
fix: decode pem file to public key using asn1 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mehditeymorian committed Sep 2, 2022
1 parent 0e60500 commit 854c071
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/config/key.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package config

import (
"crypto/rsa"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"regexp"

"github.com/golang-jwt/jwt/v4"
Expand All @@ -24,7 +27,18 @@ func (c *Config) DecodeKey(algorithm string) any {
temp, _ = keyGenerator.GenerateRsaKeys(2048)
}

key, err = jwt.ParseRSAPublicKeyFromPEM([]byte(temp))
block, _ := pem.Decode([]byte(temp))
if block == nil || block.Type != "PUBLIC KEY" {
pterm.Fatal.Println("failed to decode pem value containing public key")
}

var publicKey rsa.PublicKey
if _, err := asn1.Unmarshal(block.Bytes, &publicKey); err != nil {
pterm.Fatal.Printf("failed to unmarshal asn1 data to public key: %v\n", err)
}

key = &publicKey
err = nil
case matchAlgorithm("HS.*", algorithm):
pterm.Info.Println("Using HMAC key for decoding")

Expand Down

0 comments on commit 854c071

Please sign in to comment.