move private key loading to private_key file
This commit is contained in:
parent
d6088d5f99
commit
dac4b27f74
46
main.go
46
main.go
|
@ -1,11 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
@ -41,49 +38,6 @@ func info_on_file() {}
|
|||
// sign a certificate request to create a new certificate
|
||||
func sign_request() {}
|
||||
|
||||
// load the private key stored at `path`
|
||||
func load_private_key(path string) PrivateKey {
|
||||
if path == "" {
|
||||
crash_with_help(2, "No path to private key supplied!")
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error when opening private key: %s", err))
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error when reading private key: %s", err))
|
||||
}
|
||||
|
||||
block, _ := pem.Decode(data)
|
||||
if block.Type == TypeLabelRSA {
|
||||
return load_private_key_rsa(block)
|
||||
} else if block.Type == TypeLabelECDSA {
|
||||
return load_private_key_ecdsa(block)
|
||||
} else {
|
||||
crash_with_help(2, "No valid private key file! Only RSA and ECDSA keys are allowed!")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func load_private_key_rsa(block *pem.Block) PrivateKey {
|
||||
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error parsing private key: %s", err))
|
||||
}
|
||||
return key
|
||||
}
|
||||
func load_private_key_ecdsa(block *pem.Block) PrivateKey {
|
||||
key, err := x509.ParseECPrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error parsing private key: %s", err))
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
// open stream for given path
|
||||
func open_output_stream(path string) (io.WriteCloser, error) {
|
||||
if path == "STDOUT" {
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
@ -96,4 +97,48 @@ func parse_create_flags() CreateFlags {
|
|||
return flags
|
||||
}
|
||||
|
||||
// load the private key stored at `path`
|
||||
func load_private_key(path string) PrivateKey {
|
||||
if path == "" {
|
||||
crash_with_help(2, "No path to private key supplied!")
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error when opening private key: %s", err))
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error when reading private key: %s", err))
|
||||
}
|
||||
|
||||
block, _ := pem.Decode(data)
|
||||
if block.Type == TypeLabelRSA {
|
||||
return load_private_key_rsa(block)
|
||||
} else if block.Type == TypeLabelECDSA {
|
||||
return load_private_key_ecdsa(block)
|
||||
} else {
|
||||
crash_with_help(2, "No valid private key file! Only RSA and ECDSA keys are allowed!")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// parse rsa private key
|
||||
func load_private_key_rsa(block *pem.Block) PrivateKey {
|
||||
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error parsing private key: %s", err))
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
// parse ecdsa private key
|
||||
func load_private_key_ecdsa(block *pem.Block) PrivateKey {
|
||||
key, err := x509.ParseECPrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
crash_with_help(3, fmt.Sprintf("Error parsing private key: %s", err))
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue