...

Source file src/github.com/sassoftware/relic/cmdline/token/x509cmd.go

Documentation: github.com/sassoftware/relic/cmdline/token

     1  //
     2  // Copyright (c) SAS Institute Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package token
    18  
    19  import (
    20  	"crypto/rand"
    21  	"errors"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  
    26  	"github.com/sassoftware/relic/cmdline/shared"
    27  	"github.com/sassoftware/relic/lib/certloader"
    28  	"github.com/sassoftware/relic/lib/x509tools"
    29  	"github.com/spf13/cobra"
    30  )
    31  
    32  var (
    33  	argCopyExtensions bool
    34  	argCrossSign      bool
    35  )
    36  
    37  var ReqCmd = &cobra.Command{
    38  	Use:   "x509-request",
    39  	Short: "Generate PKCS#10 certificate signing request",
    40  }
    41  
    42  var SelfSignCmd = &cobra.Command{
    43  	Use:   "x509-self-sign",
    44  	Short: "Generate self-signed X509 certificate",
    45  }
    46  
    47  var SignCsrCmd = &cobra.Command{
    48  	Use:   "x509-sign",
    49  	Short: "Create a X509 certificate from a certificate signing request",
    50  	RunE:  signCsrCmd,
    51  }
    52  
    53  func init() {
    54  	ReqCmd.RunE = x509Cmd
    55  	shared.RootCmd.AddCommand(ReqCmd)
    56  	addSelectOrGenerateFlags(ReqCmd)
    57  	x509tools.AddRequestFlags(ReqCmd)
    58  
    59  	SelfSignCmd.RunE = x509Cmd
    60  	shared.RootCmd.AddCommand(SelfSignCmd)
    61  	addSelectOrGenerateFlags(SelfSignCmd)
    62  	x509tools.AddCertFlags(SelfSignCmd)
    63  
    64  	shared.RootCmd.AddCommand(SignCsrCmd)
    65  	addKeyFlags(SignCsrCmd)
    66  	x509tools.AddCertFlags(SignCsrCmd)
    67  	SignCsrCmd.Flags().BoolVar(&argCopyExtensions, "copy-extensions", false, "Copy extensions verbabim from CSR")
    68  	SignCsrCmd.Flags().BoolVar(&argCrossSign, "cross-sign", false, "Input is an existing certificate (implies --copy-extensions)")
    69  }
    70  
    71  func x509Cmd(cmd *cobra.Command, args []string) error {
    72  	if x509tools.ArgCommonName == "" {
    73  		return errors.New("--commonName is required")
    74  	}
    75  	key, err := selectOrGenerate()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	var result string
    80  	if cmd == ReqCmd {
    81  		result, err = x509tools.MakeRequest(rand.Reader, key)
    82  	} else {
    83  		result, err = x509tools.MakeCertificate(rand.Reader, key)
    84  	}
    85  	if err != nil {
    86  		return err
    87  	}
    88  	os.Stdout.WriteString(result)
    89  	fmt.Println("CKA_ID:", formatKeyID(key.GetID()))
    90  	return nil
    91  }
    92  
    93  func signCsrCmd(cmd *cobra.Command, args []string) error {
    94  	if len(args) != 1 {
    95  		return errors.New("expected a CSR file as input")
    96  	}
    97  	csr, err := ioutil.ReadFile(args[0])
    98  	if err != nil {
    99  		return err
   100  	}
   101  	key, err := openKey(argKeyName)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	certPath := key.Config().X509Certificate
   106  	if certPath == "" {
   107  		return errors.New("token key has no x509 certificate")
   108  	}
   109  	cert, err := certloader.LoadTokenCertificates(key, certPath, "")
   110  	if err != nil {
   111  		return err
   112  	}
   113  	if argCrossSign {
   114  		result, err := x509tools.CrossSign(csr, rand.Reader, key, cert.Leaf)
   115  		if err != nil {
   116  			return err
   117  		}
   118  		os.Stdout.WriteString(result)
   119  	} else {
   120  		result, err := x509tools.SignCSR(csr, rand.Reader, key, cert.Leaf, argCopyExtensions)
   121  		if err != nil {
   122  			return err
   123  		}
   124  		os.Stdout.WriteString(result)
   125  	}
   126  	return nil
   127  }
   128  

View as plain text