jwk.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package template
  14. import (
  15. "crypto/x509"
  16. "github.com/lestrrat-go/jwx/v2/jwk"
  17. )
  18. func jwkPublicKeyPem(jwkjson string) (string, error) {
  19. k, err := jwk.ParseKey([]byte(jwkjson))
  20. if err != nil {
  21. return "", err
  22. }
  23. var rawkey any
  24. err = k.Raw(&rawkey)
  25. if err != nil {
  26. return "", err
  27. }
  28. mpk, err := x509.MarshalPKIXPublicKey(rawkey)
  29. if err != nil {
  30. return "", err
  31. }
  32. return pemEncode(mpk, "PUBLIC KEY")
  33. }
  34. func jwkPrivateKeyPem(jwkjson string) (string, error) {
  35. k, err := jwk.ParseKey([]byte(jwkjson))
  36. if err != nil {
  37. return "", err
  38. }
  39. var mpk []byte
  40. var pk any
  41. err = k.Raw(&pk)
  42. if err != nil {
  43. return "", err
  44. }
  45. mpk, err = x509.MarshalPKCS8PrivateKey(pk)
  46. if err != nil {
  47. return "", err
  48. }
  49. return pemEncode(mpk, "PRIVATE KEY")
  50. }