2014-10-27 21:55:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2015-02-15 21:10:44 +01:00
|
|
|
"github.com/gibheer/pki"
|
2014-10-27 21:55:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-02-15 01:34:25 +01:00
|
|
|
EmptyByteArray = make([]byte, 0)
|
2014-10-27 21:55:43 +01:00
|
|
|
)
|
|
|
|
|
2015-02-15 01:34:25 +01:00
|
|
|
//const (
|
|
|
|
// RsaLowerLength = 2048
|
|
|
|
// RsaUpperLength = 4096
|
|
|
|
// TypeLabelRSA = "RSA PRIVATE KEY"
|
|
|
|
// TypeLabelECDSA = "EC PRIVATE KEY"
|
|
|
|
// TypeLabelCSR = "CERTIFICATE REQUEST"
|
|
|
|
// TypeLabelPubKey = "PUBLIC KEY"
|
|
|
|
//)
|
|
|
|
//
|
|
|
|
//var (
|
|
|
|
// EcdsaLength = []int{224, 256, 384, 521}
|
|
|
|
//)
|
|
|
|
//
|
2014-10-27 21:55:43 +01:00
|
|
|
func main() {
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
crash_with_help(1, "No module selected!")
|
|
|
|
}
|
|
|
|
switch os.Args[1] {
|
2015-01-14 21:42:37 +01:00
|
|
|
case "create-private": create_private_key()
|
|
|
|
case "create-public": create_public_key()
|
2015-02-17 22:33:54 +01:00
|
|
|
case "help": print_modules()
|
2015-02-15 01:34:25 +01:00
|
|
|
// case "info": info_on_file()
|
|
|
|
// case "sign-input": sign_input()
|
|
|
|
// case "verify-signature": verify_signature()
|
2015-02-17 22:33:54 +01:00
|
|
|
// case "create-cert-sign": create_sign_request()
|
|
|
|
// case "sign-request": sign_request()
|
2014-12-24 11:43:18 +01:00
|
|
|
default: crash_with_help(1, "Command not supported!")
|
2014-10-27 21:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 01:34:25 +01:00
|
|
|
// create a private key
|
|
|
|
func create_private_key() {
|
|
|
|
fs := NewFlags("create-private")
|
|
|
|
fs.AddOutput()
|
|
|
|
fs.AddPrivateKeyGenerationFlags()
|
|
|
|
err := fs.Parse(program_args())
|
2015-02-17 22:15:21 +01:00
|
|
|
if err != nil { os.Exit(2) }
|
2014-10-27 21:55:43 +01:00
|
|
|
|
2015-02-17 21:44:10 +01:00
|
|
|
var pk pki.Pemmer
|
2015-02-15 01:34:25 +01:00
|
|
|
switch fs.Flags.PrivateKeyGenerationFlags.Type {
|
2015-02-17 21:44:10 +01:00
|
|
|
case "ecdsa": pk, err = pki.NewPrivateKeyEcdsa(fs.Flags.PrivateKeyGenerationFlags.Curve)
|
|
|
|
case "rsa": pk, err = pki.NewPrivateKeyRsa(fs.Flags.PrivateKeyGenerationFlags.Size)
|
2014-12-24 10:45:08 +01:00
|
|
|
}
|
2015-02-17 22:15:21 +01:00
|
|
|
if err != nil { os.Exit(2) }
|
2015-02-15 01:34:25 +01:00
|
|
|
marsh_pem, err := pk.MarshalPem()
|
2015-02-17 22:15:21 +01:00
|
|
|
if err != nil { os.Exit(2) }
|
2015-02-15 01:34:25 +01:00
|
|
|
_, err = marsh_pem.WriteTo(fs.Flags.Output)
|
2015-02-17 22:15:21 +01:00
|
|
|
if err != nil { os.Exit(2) }
|
2014-12-24 10:45:08 +01:00
|
|
|
}
|
|
|
|
|
2015-02-15 01:34:25 +01:00
|
|
|
// create a public key derived from a private key
|
|
|
|
func create_public_key() {
|
|
|
|
fs := NewFlags("create-public")
|
|
|
|
fs.AddPrivateKey()
|
2015-02-17 21:44:10 +01:00
|
|
|
fs.AddOutput()
|
2015-02-15 01:34:25 +01:00
|
|
|
err := fs.Parse(program_args())
|
2015-02-17 22:15:21 +01:00
|
|
|
if err != nil { os.Exit(2) }
|
2015-01-19 12:13:13 +01:00
|
|
|
|
2015-02-17 21:44:10 +01:00
|
|
|
var pub_key pki.Pemmer
|
|
|
|
pub_key = fs.Flags.PrivateKey.Public()
|
|
|
|
marsh_pem, err := pub_key.MarshalPem()
|
2015-02-17 22:15:21 +01:00
|
|
|
if err != nil { os.Exit(2) }
|
2015-02-17 21:44:10 +01:00
|
|
|
_, err = marsh_pem.WriteTo(fs.Flags.Output)
|
2015-02-17 22:15:21 +01:00
|
|
|
if err != nil { os.Exit(2) }
|
2015-01-19 12:13:13 +01:00
|
|
|
}
|
|
|
|
|
2014-10-27 21:55:43 +01:00
|
|
|
// print the module help
|
|
|
|
func print_modules() {
|
|
|
|
fmt.Printf(`Usage: %s command args
|
|
|
|
where 'command' is one of:
|
|
|
|
create-private create a new private key
|
2015-01-14 21:42:37 +01:00
|
|
|
create-public create a public key from a private one
|
2014-10-27 21:55:43 +01:00
|
|
|
create-cert-sign create a new certificate sign request
|
|
|
|
help show this help
|
|
|
|
info get info on a file
|
2015-02-15 01:34:25 +01:00
|
|
|
sign-request sign a certificate request
|
2015-01-16 11:23:14 +01:00
|
|
|
sign-input sign a message with a private key
|
|
|
|
verify-signature verify a signature
|
2014-10-27 21:55:43 +01:00
|
|
|
`, filepath.Base(os.Args[0]))
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
2015-02-15 01:34:25 +01:00
|
|
|
// crash and provide a helpful message
|
2014-10-27 21:55:43 +01:00
|
|
|
func crash_with_help(code int, message string) {
|
|
|
|
fmt.Fprintln(os.Stderr, message)
|
|
|
|
print_modules()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
2015-02-15 01:34:25 +01:00
|
|
|
|
|
|
|
// return the arguments to the program
|
|
|
|
func program_args() []string {
|
|
|
|
return os.Args[2:]
|
|
|
|
}
|