ca.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package main
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/elliptic"
  5. "crypto/rand"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "math"
  10. "net/netip"
  11. "os"
  12. "strings"
  13. "time"
  14. "github.com/skip2/go-qrcode"
  15. "github.com/slackhq/nebula/cert"
  16. "github.com/slackhq/nebula/pkclient"
  17. "golang.org/x/crypto/ed25519"
  18. )
  19. type caFlags struct {
  20. set *flag.FlagSet
  21. name *string
  22. duration *time.Duration
  23. outKeyPath *string
  24. outCertPath *string
  25. outQRPath *string
  26. groups *string
  27. ips *string
  28. subnets *string
  29. argonMemory *uint
  30. argonIterations *uint
  31. argonParallelism *uint
  32. encryption *bool
  33. curve *string
  34. p11url *string
  35. }
  36. func newCaFlags() *caFlags {
  37. cf := caFlags{set: flag.NewFlagSet("ca", flag.ContinueOnError)}
  38. cf.set.Usage = func() {}
  39. cf.name = cf.set.String("name", "", "Required: name of the certificate authority")
  40. cf.duration = cf.set.Duration("duration", time.Duration(time.Hour*8760), "Optional: amount of time the certificate should be valid for. Valid time units are seconds: \"s\", minutes: \"m\", hours: \"h\"")
  41. cf.outKeyPath = cf.set.String("out-key", "ca.key", "Optional: path to write the private key to")
  42. cf.outCertPath = cf.set.String("out-crt", "ca.crt", "Optional: path to write the certificate to")
  43. cf.outQRPath = cf.set.String("out-qr", "", "Optional: output a qr code image (png) of the certificate")
  44. cf.groups = cf.set.String("groups", "", "Optional: comma separated list of groups. This will limit which groups subordinate certs can use")
  45. cf.ips = cf.set.String("ips", "", "Optional: comma separated list of ipv4 address and network in CIDR notation. This will limit which ipv4 addresses and networks subordinate certs can use for ip addresses")
  46. cf.subnets = cf.set.String("subnets", "", "Optional: comma separated list of ipv4 address and network in CIDR notation. This will limit which ipv4 addresses and networks subordinate certs can use in subnets")
  47. cf.argonMemory = cf.set.Uint("argon-memory", 2*1024*1024, "Optional: Argon2 memory parameter (in KiB) used for encrypted private key passphrase")
  48. cf.argonParallelism = cf.set.Uint("argon-parallelism", 4, "Optional: Argon2 parallelism parameter used for encrypted private key passphrase")
  49. cf.argonIterations = cf.set.Uint("argon-iterations", 1, "Optional: Argon2 iterations parameter used for encrypted private key passphrase")
  50. cf.encryption = cf.set.Bool("encrypt", false, "Optional: prompt for passphrase and write out-key in an encrypted format")
  51. cf.curve = cf.set.String("curve", "25519", "EdDSA/ECDSA Curve (25519, P256)")
  52. cf.p11url = p11Flag(cf.set)
  53. return &cf
  54. }
  55. func parseArgonParameters(memory uint, parallelism uint, iterations uint) (*cert.Argon2Parameters, error) {
  56. if memory <= 0 || memory > math.MaxUint32 {
  57. return nil, newHelpErrorf("-argon-memory must be be greater than 0 and no more than %d KiB", uint32(math.MaxUint32))
  58. }
  59. if parallelism <= 0 || parallelism > math.MaxUint8 {
  60. return nil, newHelpErrorf("-argon-parallelism must be be greater than 0 and no more than %d", math.MaxUint8)
  61. }
  62. if iterations <= 0 || iterations > math.MaxUint32 {
  63. return nil, newHelpErrorf("-argon-iterations must be be greater than 0 and no more than %d", uint32(math.MaxUint32))
  64. }
  65. return cert.NewArgon2Parameters(uint32(memory), uint8(parallelism), uint32(iterations)), nil
  66. }
  67. func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error {
  68. cf := newCaFlags()
  69. err := cf.set.Parse(args)
  70. if err != nil {
  71. return err
  72. }
  73. isP11 := len(*cf.p11url) > 0
  74. if err := mustFlagString("name", cf.name); err != nil {
  75. return err
  76. }
  77. if !isP11 {
  78. if err = mustFlagString("out-key", cf.outKeyPath); err != nil {
  79. return err
  80. }
  81. }
  82. if err := mustFlagString("out-crt", cf.outCertPath); err != nil {
  83. return err
  84. }
  85. var kdfParams *cert.Argon2Parameters
  86. if !isP11 && *cf.encryption {
  87. if kdfParams, err = parseArgonParameters(*cf.argonMemory, *cf.argonParallelism, *cf.argonIterations); err != nil {
  88. return err
  89. }
  90. }
  91. if *cf.duration <= 0 {
  92. return &helpError{"-duration must be greater than 0"}
  93. }
  94. var groups []string
  95. if *cf.groups != "" {
  96. for _, rg := range strings.Split(*cf.groups, ",") {
  97. g := strings.TrimSpace(rg)
  98. if g != "" {
  99. groups = append(groups, g)
  100. }
  101. }
  102. }
  103. var ips []netip.Prefix
  104. if *cf.ips != "" {
  105. for _, rs := range strings.Split(*cf.ips, ",") {
  106. rs := strings.Trim(rs, " ")
  107. if rs != "" {
  108. n, err := netip.ParsePrefix(rs)
  109. if err != nil {
  110. return newHelpErrorf("invalid ip definition: %s", err)
  111. }
  112. if !n.Addr().Is4() {
  113. return newHelpErrorf("invalid ip definition: can only be ipv4, have %s", rs)
  114. }
  115. ips = append(ips, n)
  116. }
  117. }
  118. }
  119. var subnets []netip.Prefix
  120. if *cf.subnets != "" {
  121. for _, rs := range strings.Split(*cf.subnets, ",") {
  122. rs := strings.Trim(rs, " ")
  123. if rs != "" {
  124. n, err := netip.ParsePrefix(rs)
  125. if err != nil {
  126. return newHelpErrorf("invalid subnet definition: %s", err)
  127. }
  128. if !n.Addr().Is4() {
  129. return newHelpErrorf("invalid subnet definition: can only be ipv4, have %s", rs)
  130. }
  131. subnets = append(subnets, n)
  132. }
  133. }
  134. }
  135. var passphrase []byte
  136. if !isP11 && *cf.encryption {
  137. for i := 0; i < 5; i++ {
  138. out.Write([]byte("Enter passphrase: "))
  139. passphrase, err = pr.ReadPassword()
  140. if err == ErrNoTerminal {
  141. return fmt.Errorf("out-key must be encrypted interactively")
  142. } else if err != nil {
  143. return fmt.Errorf("error reading passphrase: %s", err)
  144. }
  145. if len(passphrase) > 0 {
  146. break
  147. }
  148. }
  149. if len(passphrase) == 0 {
  150. return fmt.Errorf("no passphrase specified, remove -encrypt flag to write out-key in plaintext")
  151. }
  152. }
  153. var curve cert.Curve
  154. var pub, rawPriv []byte
  155. var p11Client *pkclient.PKClient
  156. if isP11 {
  157. switch *cf.curve {
  158. case "P256":
  159. curve = cert.Curve_P256
  160. default:
  161. return fmt.Errorf("invalid curve for PKCS#11: %s", *cf.curve)
  162. }
  163. p11Client, err = pkclient.FromUrl(*cf.p11url)
  164. if err != nil {
  165. return fmt.Errorf("error while creating PKCS#11 client: %w", err)
  166. }
  167. defer func(client *pkclient.PKClient) {
  168. _ = client.Close()
  169. }(p11Client)
  170. pub, err = p11Client.GetPubKey()
  171. if err != nil {
  172. return fmt.Errorf("error while getting public key with PKCS#11: %w", err)
  173. }
  174. } else {
  175. switch *cf.curve {
  176. case "25519", "X25519", "Curve25519", "CURVE25519":
  177. curve = cert.Curve_CURVE25519
  178. pub, rawPriv, err = ed25519.GenerateKey(rand.Reader)
  179. if err != nil {
  180. return fmt.Errorf("error while generating ed25519 keys: %s", err)
  181. }
  182. case "P256":
  183. var key *ecdsa.PrivateKey
  184. curve = cert.Curve_P256
  185. key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  186. if err != nil {
  187. return fmt.Errorf("error while generating ecdsa keys: %s", err)
  188. }
  189. // ecdh.PrivateKey lets us get at the encoded bytes, even though
  190. // we aren't using ECDH here.
  191. eKey, err := key.ECDH()
  192. if err != nil {
  193. return fmt.Errorf("error while converting ecdsa key: %s", err)
  194. }
  195. rawPriv = eKey.Bytes()
  196. pub = eKey.PublicKey().Bytes()
  197. default:
  198. return fmt.Errorf("invalid curve: %s", *cf.curve)
  199. }
  200. }
  201. t := &cert.TBSCertificate{
  202. Version: cert.Version1,
  203. Name: *cf.name,
  204. Groups: groups,
  205. Networks: ips,
  206. UnsafeNetworks: subnets,
  207. NotBefore: time.Now(),
  208. NotAfter: time.Now().Add(*cf.duration),
  209. PublicKey: pub,
  210. IsCA: true,
  211. Curve: curve,
  212. }
  213. if !isP11 {
  214. if _, err := os.Stat(*cf.outKeyPath); err == nil {
  215. return fmt.Errorf("refusing to overwrite existing CA key: %s", *cf.outKeyPath)
  216. }
  217. }
  218. if _, err := os.Stat(*cf.outCertPath); err == nil {
  219. return fmt.Errorf("refusing to overwrite existing CA cert: %s", *cf.outCertPath)
  220. }
  221. var c cert.Certificate
  222. var b []byte
  223. if isP11 {
  224. c, err = t.SignPkcs11(nil, curve, p11Client)
  225. if err != nil {
  226. return fmt.Errorf("error while signing with PKCS#11: %w", err)
  227. }
  228. } else {
  229. c, err = t.Sign(nil, curve, rawPriv)
  230. if err != nil {
  231. return fmt.Errorf("error while signing: %s", err)
  232. }
  233. if *cf.encryption {
  234. b, err = cert.EncryptAndMarshalSigningPrivateKey(curve, rawPriv, passphrase, kdfParams)
  235. if err != nil {
  236. return fmt.Errorf("error while encrypting out-key: %s", err)
  237. }
  238. } else {
  239. b = cert.MarshalSigningPrivateKeyToPEM(curve, rawPriv)
  240. }
  241. err = os.WriteFile(*cf.outKeyPath, b, 0600)
  242. if err != nil {
  243. return fmt.Errorf("error while writing out-key: %s", err)
  244. }
  245. }
  246. b, err = c.MarshalPEM()
  247. if err != nil {
  248. return fmt.Errorf("error while marshalling certificate: %s", err)
  249. }
  250. err = os.WriteFile(*cf.outCertPath, b, 0600)
  251. if err != nil {
  252. return fmt.Errorf("error while writing out-crt: %s", err)
  253. }
  254. if *cf.outQRPath != "" {
  255. b, err = qrcode.Encode(string(b), qrcode.Medium, -5)
  256. if err != nil {
  257. return fmt.Errorf("error while generating qr code: %s", err)
  258. }
  259. err = os.WriteFile(*cf.outQRPath, b, 0600)
  260. if err != nil {
  261. return fmt.Errorf("error while writing out-qr: %s", err)
  262. }
  263. }
  264. return nil
  265. }
  266. func caSummary() string {
  267. return "ca <flags>: create a self signed certificate authority"
  268. }
  269. func caHelp(out io.Writer) {
  270. cf := newCaFlags()
  271. out.Write([]byte("Usage of " + os.Args[0] + " " + caSummary() + "\n"))
  272. cf.set.SetOutput(out)
  273. cf.set.PrintDefaults()
  274. }