|
@@ -48,20 +48,27 @@ func Cert(basePath string, authTokenGenerator func() string, args []string, json
|
|
|
Help()
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
uniqueId, uniqueIdPrivate, err := zerotier.NewCertificateSubjectUniqueId(zerotier.CertificateUniqueIdTypeNistP384)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: unable to create unique ID and private key: %s\n", err.Error())
|
|
|
+ pErr("unable to create unique ID and private key: %s", err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
- sec, err := json.MarshalIndent(&zerotier.CertificateSubjectUniqueIDSecret{UniqueID: uniqueId, UniqueIDSecret: uniqueIdPrivate}, "", " ")
|
|
|
+
|
|
|
+ sec, err := json.MarshalIndent(&zerotier.CertificateSubjectUniqueIDSecret{
|
|
|
+ UniqueID: uniqueId,
|
|
|
+ UniqueIDSecret: uniqueIdPrivate,
|
|
|
+ }, "", " ")
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: unable to create unique ID and private key: %s\n", err.Error())
|
|
|
+ pErr("unable to create unique ID and private key: %s", err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
if len(args) == 1 {
|
|
|
fmt.Println(string(sec))
|
|
|
} else {
|
|
|
_ = ioutil.WriteFile(args[1], sec, 0600)
|
|
|
+ pResult("%s", args[1])
|
|
|
}
|
|
|
|
|
|
case "newcsr":
|
|
@@ -69,28 +76,32 @@ func Cert(basePath string, authTokenGenerator func() string, args []string, json
|
|
|
Help()
|
|
|
return 1
|
|
|
}
|
|
|
- var cs zerotier.CertificateSubject
|
|
|
- err := readJSONFile(args[1], &cs)
|
|
|
+
|
|
|
+ var subject zerotier.CertificateSubject
|
|
|
+ err := readJSONFile(args[1], &subject)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: unable to read subject from %s: %s\n", args[1], err.Error())
|
|
|
+ pErr("unable to read subject from %s: %s", args[1], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
- var subj zerotier.CertificateSubjectUniqueIDSecret
|
|
|
- err = readJSONFile(args[2], &subj)
|
|
|
+
|
|
|
+ var uniqueIdSecret zerotier.CertificateSubjectUniqueIDSecret
|
|
|
+ err = readJSONFile(args[2], &uniqueIdSecret)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: unable to read unique ID secret from %s: %s\n", args[2], err.Error())
|
|
|
+ pErr("unable to read unique ID secret from %s: %s", args[2], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
- csr, err := zerotier.NewCertificateCSR(&cs, subj.UniqueID, subj.UniqueIDSecret)
|
|
|
+
|
|
|
+ csr, err := zerotier.NewCertificateCSR(&subject, uniqueIdSecret.UniqueID, uniqueIdSecret.UniqueIDSecret)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: problem creating CSR: %s\n", err.Error())
|
|
|
+ pErr("problem creating CSR: %s", err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
err = ioutil.WriteFile(args[3], csr, 0644)
|
|
|
if err == nil {
|
|
|
- fmt.Printf("Wrote CSR to %s\n", args[3])
|
|
|
+ pResult("%s", args[3])
|
|
|
} else {
|
|
|
- fmt.Printf("ERROR: unable to write CSR to %s: %s\n", args[3], err.Error())
|
|
|
+ pErr("unable to write CSR to %s: %s", args[3], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
|
|
@@ -102,40 +113,42 @@ func Cert(basePath string, authTokenGenerator func() string, args []string, json
|
|
|
|
|
|
csrBytes, err := ioutil.ReadFile(args[1])
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: unable to read CSR from %s: %s\n", args[1], err.Error())
|
|
|
+ pErr("unable to read CSR from %s: %s", args[1], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
csr, err := zerotier.NewCertificateFromBytes(csrBytes, false)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: CSR in %s is invalid: %s\n", args[1], err.Error())
|
|
|
+ pErr("CSR in %s is invalid: %s", args[1], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
|
|
|
- id := readIdentity(args[2])
|
|
|
- if id == nil {
|
|
|
- fmt.Printf("ERROR: unable to read identity from %s\n", args[2])
|
|
|
+ signingIdentity := readIdentity(args[2])
|
|
|
+ if signingIdentity == nil {
|
|
|
+ pErr("unable to read identity from %s", args[2])
|
|
|
return 1
|
|
|
}
|
|
|
- if !id.HasPrivate() {
|
|
|
- fmt.Printf("ERROR: signing identity in %s lacks private key\n", args[2])
|
|
|
+ if !signingIdentity.HasPrivate() {
|
|
|
+ pErr("signing identity in %s lacks private key", args[2])
|
|
|
return 1
|
|
|
}
|
|
|
|
|
|
- cert, err := csr.Sign(id)
|
|
|
+ cert, err := csr.Sign(signingIdentity)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: error signing CSR or generating certificate: %s\n", err.Error())
|
|
|
+ pErr("error signing CSR or generating certificate: %s", err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
cb, err := cert.Marshal()
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: error marshaling signed certificate: %s\n", err.Error())
|
|
|
+ pErr("error marshaling signed certificate: %s", err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
err = ioutil.WriteFile(args[3], cb, 0644)
|
|
|
if err == nil {
|
|
|
- fmt.Printf("Wrote signed certificate to %s\n", args[3])
|
|
|
+ pResult("%s", args[3])
|
|
|
} else {
|
|
|
- fmt.Printf("ERROR: unable to write signed certificate to %s: %s\n", args[3], err.Error())
|
|
|
+ pErr("unable to write signed certificate to %s: %s", args[3], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
|
|
@@ -144,16 +157,19 @@ func Cert(basePath string, authTokenGenerator func() string, args []string, json
|
|
|
Help()
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
certBytes, err := ioutil.ReadFile(args[1])
|
|
|
if err != nil {
|
|
|
- fmt.Printf("ERROR: unable to read certificate from %s: %s\n", args[1], err.Error())
|
|
|
+ pErr("unable to read certificate from %s: %s", args[1], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
cert, err := zerotier.NewCertificateFromBytes(certBytes, true)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("FAILED: certificate in %s invalid: %s\n", args[1], err.Error())
|
|
|
+ pErr("certificate in %s invalid: %s", args[1], err.Error())
|
|
|
return 1
|
|
|
}
|
|
|
+
|
|
|
if args[0] == "dump" {
|
|
|
fmt.Println(cert.JSON())
|
|
|
} else {
|