2020-08-01 23:59:20 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.coolaj86.com/coolaj86/go-mockid/xkeypairs"
|
|
|
|
"git.rootprojects.org/root/keypairs"
|
|
|
|
)
|
|
|
|
|
2020-08-02 09:39:56 +00:00
|
|
|
// GeneratePublicJWK will create a new private key in JWK format
|
|
|
|
func GeneratePublicJWK(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if "POST" != r.Method {
|
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := getOpts(r)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
privkey, err := getPrivKey(opts)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jwk := keypairs.MarshalJWKPublicKey(keypairs.NewPublicKey(privkey.Public()))
|
|
|
|
w.Write(append(jwk, '\n'))
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:59:20 +00:00
|
|
|
// GeneratePrivateJWK will create a new private key in JWK format
|
|
|
|
func GeneratePrivateJWK(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if "POST" != r.Method {
|
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := getOpts(r)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-16 22:32:46 +00:00
|
|
|
privkey := xkeypairs.GenPrivKey(opts)
|
2020-08-01 23:59:20 +00:00
|
|
|
|
2022-05-05 23:38:25 +00:00
|
|
|
jwk := keypairs.MarshalJWKPrivateKey(privkey)
|
2020-08-01 23:59:20 +00:00
|
|
|
w.Write(append(jwk, '\n'))
|
|
|
|
}
|
|
|
|
|
2020-08-02 09:39:56 +00:00
|
|
|
// GeneratePublicDER will create a new private key in JWK format
|
|
|
|
func GeneratePublicDER(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if "POST" != r.Method {
|
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := getOpts(r)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
privkey, err := getPrivKey(opts)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-05 23:38:25 +00:00
|
|
|
b, _ := keypairs.MarshalDERPublicKey(privkey.Public())
|
2020-08-02 09:39:56 +00:00
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:59:20 +00:00
|
|
|
// GeneratePrivateDER will create a new private key in a valid DER encoding
|
|
|
|
func GeneratePrivateDER(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if "POST" != r.Method {
|
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := getOpts(r)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-16 22:32:46 +00:00
|
|
|
privkey := xkeypairs.GenPrivKey(opts)
|
2020-08-01 23:59:20 +00:00
|
|
|
|
2022-05-05 23:38:25 +00:00
|
|
|
der, _ := keypairs.MarshalDERPrivateKey(privkey)
|
2020-08-01 23:59:20 +00:00
|
|
|
w.Write(der)
|
|
|
|
}
|
|
|
|
|
2020-08-02 09:39:56 +00:00
|
|
|
// GeneratePublicPEM will create a new private key in JWK format
|
|
|
|
func GeneratePublicPEM(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if "POST" != r.Method {
|
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := getOpts(r)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
privkey, err := getPrivKey(opts)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-05 23:38:25 +00:00
|
|
|
b, _ := keypairs.MarshalPEMPublicKey(privkey.Public())
|
2020-08-02 09:39:56 +00:00
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:59:20 +00:00
|
|
|
// GeneratePrivatePEM will create a new private key in a valid PEM encoding
|
|
|
|
func GeneratePrivatePEM(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if "POST" != r.Method {
|
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := getOpts(r)
|
|
|
|
if nil != err {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-16 22:32:46 +00:00
|
|
|
privkey := xkeypairs.GenPrivKey(opts)
|
2020-08-01 23:59:20 +00:00
|
|
|
|
2022-05-05 23:38:25 +00:00
|
|
|
privpem, _ := keypairs.MarshalPEMPrivateKey(privkey)
|
2020-08-01 23:59:20 +00:00
|
|
|
w.Write(privpem)
|
|
|
|
}
|
|
|
|
|
2020-08-02 09:39:56 +00:00
|
|
|
const maxRetry = 16
|
|
|
|
|
2020-09-16 22:32:46 +00:00
|
|
|
func getPrivKey(opts *xkeypairs.KeyOptions) (keypairs.PrivateKey, error) {
|
2020-08-02 09:39:56 +00:00
|
|
|
if "" != opts.Key {
|
|
|
|
return keypairs.ParsePrivateKey([]byte(opts.Key))
|
|
|
|
}
|
2020-09-16 22:32:46 +00:00
|
|
|
return xkeypairs.GenPrivKey(opts), nil
|
2020-08-01 23:59:20 +00:00
|
|
|
}
|