Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting openssl private key from certbot private_key json file #19

Open
endorama opened this issue Nov 23, 2018 · 0 comments
Open

Extracting openssl private key from certbot private_key json file #19

endorama opened this issue Nov 23, 2018 · 0 comments

Comments

@endorama
Copy link
Owner

endorama commented Nov 23, 2018

source: https://community.letsencrypt.org/t/how-to-get-openssl-rsa-private-key-out-of-private-key-json/4658/5

solution

Using a simple go program:

  $ go get -u gopkg.in/square/go-jose.v2/...
  $ go run certbot-to-pem.go [path to private_key.json]
  -----BEGIN RSA PRIVATE KEY-----
  <snip>

Here is the program (certbot-to-pem.go):

package main

import (
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"io/ioutil"
	"os"
	"reflect"

	"gopkg.in/square/go-jose.v2"
)

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Usage: certbot-to-pem path-to-certbot-private_key.json")
		os.Exit(1)
	}

	pkBuf, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		panic(err)
	}

	var k jose.JSONWebKey
	if err := k.UnmarshalJSON(pkBuf); err != nil {
		panic(err)
	}

	switch p := k.Key.(type) {
	case *rsa.PrivateKey:
		fmt.Println(string(pem.EncodeToMemory(&pem.Block{
			Type:  "RSA PRIVATE KEY",
			Bytes: x509.MarshalPKCS1PrivateKey(p),
		})))
	default:
		panic("Don't know how to deal with " + reflect.TypeOf(p).String())
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant