X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=service-exposure%2Futils%2Fpemtojwks%2Fpemtojwks.go;fp=service-exposure%2Futils%2Fpemtojwks%2Fpemtojwks.go;h=d232d781bc13fa07b5a63547eecf5bfb1c88a9ca;hb=df61b02070956cac9ec7429281dc78ba853b46ed;hp=6317843dfe319de90a3bed8bcbedc15c367fecdd;hpb=ff3afde70cdc9519d527fa096c31df1151d84765;p=nonrtric.git diff --git a/service-exposure/utils/pemtojwks/pemtojwks.go b/service-exposure/utils/pemtojwks/pemtojwks.go index 6317843d..d232d781 100644 --- a/service-exposure/utils/pemtojwks/pemtojwks.go +++ b/service-exposure/utils/pemtojwks/pemtojwks.go @@ -2,7 +2,7 @@ // ========================LICENSE_START================================= // O-RAN-SC // %% -// Copyright (C) 2022: Nordix Foundation +// Copyright (C) 2022-2023: Nordix Foundation // %% // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ // limitations under the License. // ========================LICENSE_END=================================== // -package pemtojwks +package pemtojwks import ( "crypto/rsa" @@ -38,6 +38,7 @@ type Jwks struct { type Key struct { Kid string `json:"kid,omitempty"` Kty string `json:"kty"` + Alg string `json:"alg"` Use string `json:"use"` N string `json:"n"` E string `json:"e"` @@ -93,27 +94,35 @@ func getPublicKeyFromCert(cert_bytes []byte) *rsa.PublicKey { cert, _ = x509.ParseCertificate(block.Bytes) rsaPublicKey := cert.PublicKey.(*rsa.PublicKey) - return rsaPublicKey + return rsaPublicKey } -func CreateJWKS(certFile string) string { +func CreateJWKS(certFile string) (string, string, string) { var publicKey *rsa.PublicKey + var kid string = "SIGNING_KEY" cert, err := ioutil.ReadFile(certFile) if err != nil { fmt.Println(err) } publicKey = getPublicKeyFromCert(cert) + publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) + if err != nil { + fmt.Println(err) + } + publicKeyPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: publicKeyBytes}) + block, _ := pem.Decode(publicKeyPem) + publicKeyString := base64.StdEncoding.EncodeToString(block.Bytes) certificate := getCert(cert) // generate fingerprint with sha1 // you can also use md5, sha256, etc. fingerprint := sha1.Sum(certificate.Raw) - jwksKey := Key{ - Kid: "SIGNING_KEY", + Kid: kid, Kty: "RSA", + Alg: "RS256", Use: "sig", N: base64.RawStdEncoding.EncodeToString(publicKey.N.Bytes()), E: base64.RawStdEncoding.EncodeToString(big.NewInt(int64(publicKey.E)).Bytes()), @@ -126,8 +135,7 @@ func CreateJWKS(certFile string) string { jwksJson, err := json.Marshal(jwks) if err != nil { fmt.Println(err) - return err.Error() } - return string(jwksJson) + return string(jwksJson), publicKeyString, kid }