From 9ba6a88449e6297ea925e0c59e923c56e742983f Mon Sep 17 00:00:00 2001 From: Gibheer Date: Mon, 30 Mar 2015 21:34:05 +0200 Subject: split out all private key related functions This moves all private key related functions out into a separate file to get flags.go smaller. --- flags.go | 97 ---------------------------------------------------------------- 1 file changed, 97 deletions(-) (limited to 'flags.go') diff --git a/flags.go b/flags.go index a78cd56..b93c156 100644 --- a/flags.go +++ b/flags.go @@ -21,14 +21,7 @@ import ( "github.com/gibheer/pki" ) -const ( - RsaLowerLength = 2048 - RsaUpperLength = 16384 -) - var ( - // the possible ecdsa curves allowed to be used - EcdsaCurves = []int{224, 256, 384, 521} // the possible valid key usages to check against the commandline ValidKeyUsages = map[string]x509.KeyUsage{ "digitalsignature": x509.KeyUsageDigitalSignature, @@ -94,12 +87,6 @@ type ( certificatePath string // path to a certificate } - privateKeyGenerationFlags struct { - Type string // type of the private key (rsa, ecdsa) - Curve elliptic.Curve // curve for ecdsa - Size int // bitsize for rsa - } - certGenerationRaw struct { serial int64 notBefore string @@ -121,14 +108,6 @@ var ( certificate requests and certificates and sign/verify messages.`, } - CmdCreatePrivateKey = &Command{ - Use: "create-private", - Short: "create a private key", - Long: "Create an ecdsa or rsa key with this command", - Example: "create-private -type=ecdsa -length=521", - Run: create_private_key, - } - CmdCreatePublicKey = &Command{ Use: "create-public", Short: "create a public key from a private key", @@ -182,8 +161,6 @@ certificate requests and certificates and sign/verify messages.`, FlagOutput io.WriteCloser // signature from the args FlagSignature []byte - // private key specific stuff - FlagPrivateKeyGeneration privateKeyGenerationFlags // a certificate filled with the parameters FlagCertificateRequestData *pki.CertificateData // the certificate sign request @@ -238,39 +215,6 @@ func checkFlags(checks ...flagCheck) error { return nil } -//// print a message with the usage part -//func (f *Flags) Usagef(message string, args ...interface{}) { -// fmt.Fprintf(os.Stderr, "error: " + message + "\n", args...) -// f.flagset.Flags().Usage() -//} - -// add the private key option to the requested flags -func InitFlagPrivateKey(cmd *Command) { - cmd.Flags().StringVar(&flagContainer.privateKeyPath, "private-key", "", "path to the private key (required)") -} - -// check the private key flag and load the private key -func checkPrivateKey() error { - if flagContainer.privateKeyPath == "" { - return fmt.Errorf("No private key given!") - } - // check permissions of private key file - info, err := os.Stat(flagContainer.privateKeyPath) - if err != nil { - return fmt.Errorf("Error reading private key: %s", err) - } - if info.Mode().Perm().String()[4:] != "------" { - return fmt.Errorf("private key file modifyable by others!") - } - - pk, err := ReadPrivateKeyFile(flagContainer.privateKeyPath) - if err != nil { - return fmt.Errorf("Error reading private key: %s", err) - } - FlagPrivateKey = pk - return nil -} - // add the public key flag func InitFlagPublicKey(cmd *Command) { cmd.Flags().StringVar(&flagContainer.publicKeyPath, "public-key", "", "path to the public key (required)") @@ -454,47 +398,6 @@ func checkInput() error { return nil } -// This function adds the private key generation flags. -func InitFlagPrivateKeyGeneration(cmd *Command) { - cmd.Flags().StringVar(&flagContainer.cryptType, "type", "ecdsa", "the type of the private key (ecdsa, rsa)") - cmd.Flags().IntVar( - &flagContainer.length, - "length", 521, - fmt.Sprintf("%d - %d for rsa; one of %v for ecdsa", RsaLowerLength, RsaUpperLength, EcdsaCurves), - ) -} - -// check the private key generation variables and move them to the work space -func checkPrivateKeyGeneration() error { - pk_type := flagContainer.cryptType - FlagPrivateKeyGeneration.Type = pk_type - switch pk_type { - case "ecdsa": - switch flagContainer.length { - case 224: - FlagPrivateKeyGeneration.Curve = elliptic.P224() - case 256: - FlagPrivateKeyGeneration.Curve = elliptic.P256() - case 384: - FlagPrivateKeyGeneration.Curve = elliptic.P384() - case 521: - FlagPrivateKeyGeneration.Curve = elliptic.P521() - default: - return fmt.Errorf("Curve %d unknown!", flagContainer.length) - } - case "rsa": - size := flagContainer.length - if RsaLowerLength <= size && size <= RsaUpperLength { - FlagPrivateKeyGeneration.Size = size - } else { - return fmt.Errorf("Length of %d is not allowed for rsa!", size) - } - default: - return fmt.Errorf("Type %s is unknown!", pk_type) - } - return nil -} - // add the signature flag to load a signature from a signing process func InitFlagSignature(cmd *Command) { cmd.Flags().StringVar(&flagContainer.signature, "signature", "", "the base64 encoded signature to use for verification") -- cgit v1.2.3-70-g09d2