2016-12-08 08:12:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-02-02 19:29:16 +00:00
|
|
|
"crypto/rand"
|
2019-12-16 12:04:52 +00:00
|
|
|
"crypto/sha1"
|
2019-11-25 13:49:19 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2019-01-17 09:51:44 +00:00
|
|
|
"encoding/base32"
|
2019-12-16 12:04:52 +00:00
|
|
|
"encoding/base64"
|
2018-01-21 13:18:26 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-04-13 18:59:03 +00:00
|
|
|
"log"
|
2020-01-21 12:14:19 +00:00
|
|
|
"math"
|
2018-02-02 19:29:16 +00:00
|
|
|
"math/big"
|
2022-05-16 09:38:46 +00:00
|
|
|
"net/http"
|
2018-01-21 13:18:26 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2018-04-13 18:59:03 +00:00
|
|
|
"strconv"
|
2019-01-17 09:51:44 +00:00
|
|
|
"strings"
|
2019-11-25 13:49:19 +00:00
|
|
|
"time"
|
2018-01-21 13:18:26 +00:00
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/admin/pki"
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-12-08 08:12:18 +00:00
|
|
|
)
|
|
|
|
|
2018-12-06 02:46:14 +00:00
|
|
|
var TeamsDir string
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func declareCertificateRoutes(router *gin.RouterGroup) {
|
|
|
|
router.GET("/htpasswd", func(c *gin.Context) {
|
|
|
|
ret, err := genHtpasswd(true)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.String(http.StatusOK, ret)
|
|
|
|
})
|
|
|
|
router.POST("/htpasswd", func(c *gin.Context) {
|
|
|
|
if htpasswd, err := genHtpasswd(true); err != nil {
|
|
|
|
log.Println("Unable to generate htpasswd:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
} else if err := ioutil.WriteFile(path.Join(pki.PKIDir, "shared", "ficpasswd"), []byte(htpasswd), 0644); err != nil {
|
|
|
|
log.Println("Unable to write htpasswd:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
})
|
|
|
|
router.DELETE("/htpasswd", func(c *gin.Context) {
|
|
|
|
if err := os.Remove(path.Join(pki.PKIDir, "shared", "ficpasswd")); err != nil {
|
|
|
|
log.Println("Unable to remove htpasswd:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
})
|
|
|
|
router.GET("/htpasswd.apr1", func(c *gin.Context) {
|
|
|
|
ret, err := genHtpasswd(false)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.String(http.StatusOK, ret)
|
|
|
|
})
|
|
|
|
router.GET("/ca", infoCA)
|
|
|
|
router.GET("/ca.pem", getCAPEM)
|
|
|
|
router.POST("/ca/new", func(c *gin.Context) {
|
|
|
|
var upki PKISettings
|
|
|
|
err := c.ShouldBindJSON(&upki)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2018-01-21 13:18:26 +00:00
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
if err := pki.GenerateCA(upki.NotBefore, upki.NotAfter); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusCreated, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
router.GET("/certs", getCertificates)
|
|
|
|
router.POST("/certs", generateClientCert)
|
|
|
|
router.DELETE("/certs", func(c *gin.Context) {
|
|
|
|
v, err := fic.ClearCertificates()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to ClearCertificates:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, v)
|
|
|
|
})
|
|
|
|
|
|
|
|
apiCertificatesRoutes := router.Group("/certs/:certid")
|
|
|
|
apiCertificatesRoutes.Use(CertificateHandler)
|
|
|
|
apiCertificatesRoutes.HEAD("", getTeamP12File)
|
|
|
|
apiCertificatesRoutes.GET("", getTeamP12File)
|
|
|
|
apiCertificatesRoutes.PUT("", updateCertificateAssociation)
|
|
|
|
apiCertificatesRoutes.DELETE("", func(c *gin.Context) {
|
|
|
|
cert := c.MustGet("cert").(*fic.Certificate)
|
|
|
|
|
|
|
|
v, err := cert.Revoke()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to Revoke:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func declareTeamCertificateRoutes(router *gin.RouterGroup) {
|
|
|
|
router.GET("/certificates", func(c *gin.Context) {
|
|
|
|
team := c.MustGet("team").(*fic.Team)
|
|
|
|
|
|
|
|
if serials, err := pki.GetTeamSerials(TeamsDir, team.Id); err != nil {
|
|
|
|
log.Println("Unable to GetTeamSerials:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
var certs []CertExported
|
|
|
|
for _, serial := range serials {
|
|
|
|
if cert, err := fic.GetCertificate(serial); err == nil {
|
|
|
|
certs = append(certs, CertExported{fmt.Sprintf("%0[2]*[1]X", cert.Id, int(math.Ceil(math.Log2(float64(cert.Id))/8)*2)), cert.Creation, cert.Password, &team.Id, cert.Revoked})
|
|
|
|
} else {
|
|
|
|
log.Println("Unable to get back certificate, whereas an association exists on disk: ", err)
|
2018-04-13 18:59:03 +00:00
|
|
|
}
|
2019-02-04 18:59:29 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
c.JSON(http.StatusOK, certs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.GET("/associations", func(c *gin.Context) {
|
|
|
|
team := c.MustGet("team").(*fic.Team)
|
|
|
|
|
|
|
|
assocs, err := pki.GetTeamAssociations(TeamsDir, team.Id)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to GetTeamAssociations:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, assocs)
|
|
|
|
})
|
|
|
|
|
|
|
|
apiTeamAssociationsRoutes := router.Group("/associations/:assoc")
|
|
|
|
apiTeamAssociationsRoutes.POST("", func(c *gin.Context) {
|
|
|
|
team := c.MustGet("team").(*fic.Team)
|
|
|
|
|
|
|
|
if err := os.Symlink(fmt.Sprintf("%d", team.Id), path.Join(TeamsDir, c.Params.ByName("assoc"))); err != nil {
|
|
|
|
log.Println("Unable to create association symlink:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to create association symlink: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, c.Params.ByName("assoc"))
|
|
|
|
})
|
|
|
|
apiTeamAssociationsRoutes.DELETE("", func(c *gin.Context) {
|
|
|
|
err := pki.DeleteTeamAssociation(TeamsDir, c.Params.ByName("assoc"))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to DeleteTeamAssociation(%s): %s", c.Params.ByName("assoc"), err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to delete association symlink: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func CertificateHandler(c *gin.Context) {
|
|
|
|
var certid uint64
|
|
|
|
var err error
|
|
|
|
|
|
|
|
cid := strings.TrimSuffix(string(c.Params.ByName("certid")), ".p12")
|
|
|
|
if certid, err = strconv.ParseUint(cid, 10, 64); err != nil {
|
|
|
|
if certid, err = strconv.ParseUint(cid, 16, 64); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Invalid certficate identifier"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := fic.GetCertificate(certid)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Certificate not found"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Set("cert", cert)
|
|
|
|
|
|
|
|
c.Next()
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 12:04:52 +00:00
|
|
|
func genHtpasswd(ssha bool) (ret string, err error) {
|
2021-11-22 14:35:07 +00:00
|
|
|
var teams []*fic.Team
|
2019-01-17 09:51:44 +00:00
|
|
|
teams, err = fic.GetTeams()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, team := range teams {
|
|
|
|
var serials []uint64
|
|
|
|
serials, err = pki.GetTeamSerials(TeamsDir, team.Id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(serials) == 0 {
|
|
|
|
// Don't include teams that don't have associated certificates
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, serial := range serials {
|
2021-11-22 14:35:07 +00:00
|
|
|
var cert *fic.Certificate
|
2019-01-17 09:51:44 +00:00
|
|
|
cert, err = fic.GetCertificate(serial)
|
|
|
|
if err != nil {
|
2019-12-16 12:04:52 +00:00
|
|
|
// Ignore invalid/incorrect/non-existant certificates
|
|
|
|
continue
|
2019-01-17 09:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cert.Revoked != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:04:52 +00:00
|
|
|
salt := make([]byte, 5)
|
|
|
|
if _, err = rand.Read(salt); err != nil {
|
2019-01-17 09:51:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:04:52 +00:00
|
|
|
if ssha {
|
|
|
|
hash := sha1.New()
|
|
|
|
hash.Write([]byte(cert.Password))
|
|
|
|
hash.Write([]byte(salt))
|
|
|
|
|
2021-01-30 04:13:56 +00:00
|
|
|
passwdline := fmt.Sprintf(":{SSHA}%s\n", base64.StdEncoding.EncodeToString(append(hash.Sum(nil), salt...)))
|
2020-01-15 09:52:39 +00:00
|
|
|
|
|
|
|
ret += strings.ToLower(team.Name) + passwdline
|
|
|
|
ret += fmt.Sprintf("%0[2]*[1]x", cert.Id, int(math.Ceil(math.Log2(float64(cert.Id))/8)*2)) + passwdline
|
|
|
|
ret += fmt.Sprintf("%0[2]*[1]X", cert.Id, int(math.Ceil(math.Log2(float64(cert.Id))/8)*2)) + passwdline
|
2021-01-30 04:13:56 +00:00
|
|
|
teamAssociations, _ := pki.GetTeamAssociations(TeamsDir, team.Id)
|
|
|
|
log.Println(path.Join(TeamsDir, fmt.Sprintf("%d", team.Id)), teamAssociations)
|
|
|
|
for _, ta := range teamAssociations {
|
|
|
|
ret += strings.Replace(ta, ":", "", -1) + passwdline
|
|
|
|
}
|
2019-12-16 12:04:52 +00:00
|
|
|
} else {
|
|
|
|
salt32 := base32.StdEncoding.EncodeToString(salt)
|
|
|
|
ret += fmt.Sprintf(
|
|
|
|
"%s:$apr1$%s$%s\n",
|
|
|
|
strings.ToLower(team.Name),
|
|
|
|
salt32,
|
|
|
|
fic.Apr1Md5(cert.Password, salt32),
|
|
|
|
)
|
|
|
|
}
|
2019-01-17 09:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:49:19 +00:00
|
|
|
type PKISettings struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
SerialNumber *big.Int `json:"serialnumber"`
|
|
|
|
Issuer pkix.Name `json:"issuer"`
|
|
|
|
Subject pkix.Name `json:"subject"`
|
|
|
|
NotBefore time.Time `json:"notbefore"`
|
|
|
|
NotAfter time.Time `json:"notafter"`
|
|
|
|
SignatureAlgorithm x509.SignatureAlgorithm `json:"signatureAlgorithm,"`
|
|
|
|
PublicKeyAlgorithm x509.PublicKeyAlgorithm `json:"publicKeyAlgorithm"`
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func infoCA(c *gin.Context) {
|
2018-01-21 13:18:26 +00:00
|
|
|
_, cacert, err := pki.LoadCA()
|
|
|
|
if err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "CA not found"})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
c.JSON(http.StatusOK, PKISettings{
|
2019-11-25 13:49:19 +00:00
|
|
|
Version: cacert.Version,
|
|
|
|
SerialNumber: cacert.SerialNumber,
|
|
|
|
Issuer: cacert.Issuer,
|
|
|
|
Subject: cacert.Subject,
|
|
|
|
NotBefore: cacert.NotBefore,
|
|
|
|
NotAfter: cacert.NotAfter,
|
|
|
|
SignatureAlgorithm: cacert.SignatureAlgorithm,
|
|
|
|
PublicKeyAlgorithm: cacert.PublicKeyAlgorithm,
|
2022-05-16 09:38:46 +00:00
|
|
|
})
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func getCAPEM(c *gin.Context) {
|
2018-01-21 13:18:26 +00:00
|
|
|
if _, err := os.Stat(pki.CACertPath()); os.IsNotExist(err) {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Unable to locate the CA root certificate. Have you generated it?"})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
} else if fd, err := os.Open(pki.CACertPath()); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to open CA root certificate:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
} else {
|
|
|
|
defer fd.Close()
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
cnt, err := ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to read CA root certificate:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.String(http.StatusOK, string(cnt))
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func getTeamP12File(c *gin.Context) {
|
|
|
|
cert := c.MustGet("cert").(*fic.Certificate)
|
|
|
|
|
2018-01-21 13:18:26 +00:00
|
|
|
// Create p12 if necessary
|
|
|
|
if _, err := os.Stat(pki.ClientP12Path(cert.Id)); os.IsNotExist(err) {
|
|
|
|
if err := pki.WriteP12(cert.Id, cert.Password); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to WriteP12:", err.Error())
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(pki.ClientP12Path(cert.Id)); os.IsNotExist(err) {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to compute ClientP12Path:", err.Error())
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, errors.New("Unable to locate the p12. Have you generated it?"))
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
} else if fd, err := os.Open(pki.ClientP12Path(cert.Id)); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to open ClientP12Path:", err.Error())
|
2022-07-08 21:11:11 +00:00
|
|
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("Unable to open the p12: %w", err))
|
2022-05-16 09:38:46 +00:00
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
} else {
|
|
|
|
defer fd.Close()
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to open ClientP12Path:", err.Error())
|
2022-07-08 21:11:11 +00:00
|
|
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("Unable to open the p12: %w", err))
|
2022-05-16 09:38:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, "application/x-pkcs12", data)
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func generateClientCert(c *gin.Context) {
|
2018-01-21 13:18:26 +00:00
|
|
|
// First, generate a new, unique, serial
|
2018-02-02 19:29:16 +00:00
|
|
|
var serial_gen [8]byte
|
|
|
|
if _, err := rand.Read(serial_gen[:]); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to read enough entropy to generate client certificate:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to read enough entropy"})
|
|
|
|
return
|
2018-02-02 19:29:16 +00:00
|
|
|
}
|
|
|
|
for fic.ExistingCertSerial(serial_gen) {
|
|
|
|
if _, err := rand.Read(serial_gen[:]); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to read enough entropy to generate client certificate:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to read enough entropy"})
|
|
|
|
return
|
2018-02-02 19:29:16 +00:00
|
|
|
}
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:29:16 +00:00
|
|
|
var serial_b big.Int
|
|
|
|
serial_b.SetBytes(serial_gen[:])
|
|
|
|
serial := serial_b.Uint64()
|
|
|
|
|
2018-01-21 13:18:26 +00:00
|
|
|
// Let's pick a random password
|
2021-09-09 09:20:45 +00:00
|
|
|
password, err := fic.GeneratePassword()
|
2018-01-21 13:18:26 +00:00
|
|
|
if err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to generate password:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to generate password: " + err.Error()})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, now load CA
|
|
|
|
capriv, cacert, err := pki.LoadCA()
|
|
|
|
if err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to load the CA:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to load the CA"})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate our privkey
|
|
|
|
if err := pki.GenerateClient(serial, cacert.NotBefore, cacert.NotAfter, &cacert, &capriv); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to generate private key:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to generate private key: " + err.Error()})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save in DB
|
2020-01-21 12:14:19 +00:00
|
|
|
cert, err := fic.RegisterCertificate(serial, password)
|
2022-05-16 09:38:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to register certificate:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to register certificate."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, CertExported{fmt.Sprintf("%0[2]*[1]X", cert.Id, int(math.Ceil(math.Log2(float64(cert.Id))/8)*2)), cert.Creation, cert.Password, nil, cert.Revoked})
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CertExported struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Creation time.Time `json:"creation"`
|
2020-01-21 12:14:19 +00:00
|
|
|
Password string `json:"password,omitempty"`
|
2021-01-30 04:13:56 +00:00
|
|
|
IdTeam *int64 `json:"id_team"`
|
2018-01-21 13:18:26 +00:00
|
|
|
Revoked *time.Time `json:"revoked"`
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func getCertificates(c *gin.Context) {
|
|
|
|
certificates, err := fic.GetCertificates()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to retrieve certificates list:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during certificates retrieval."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret := make([]CertExported, 0)
|
|
|
|
for _, cert := range certificates {
|
|
|
|
dstLinkPath := path.Join(TeamsDir, pki.GetCertificateAssociation(cert.Id))
|
|
|
|
|
|
|
|
var idTeam *int64 = nil
|
|
|
|
if lnk, err := os.Readlink(dstLinkPath); err == nil {
|
|
|
|
if tid, err := strconv.ParseInt(lnk, 10, 64); err == nil {
|
|
|
|
idTeam = &tid
|
2018-04-13 18:59:03 +00:00
|
|
|
}
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
ret = append(ret, CertExported{fmt.Sprintf("%0[2]*[1]X", cert.Id, int(math.Ceil(math.Log2(float64(cert.Id))/8)*2)), cert.Creation, "", idTeam, cert.Revoked})
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, ret)
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CertUploaded struct {
|
|
|
|
Team *int64 `json:"id_team"`
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func updateCertificateAssociation(c *gin.Context) {
|
|
|
|
cert := c.MustGet("cert").(*fic.Certificate)
|
|
|
|
|
2018-01-21 13:18:26 +00:00
|
|
|
var uc CertUploaded
|
2022-05-16 09:38:46 +00:00
|
|
|
err := c.ShouldBindJSON(&uc)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 18:59:03 +00:00
|
|
|
dstLinkPath := path.Join(TeamsDir, pki.GetCertificateAssociation(cert.Id))
|
2018-01-21 13:18:26 +00:00
|
|
|
|
|
|
|
if uc.Team != nil {
|
|
|
|
srcLinkPath := fmt.Sprintf("%d", *uc.Team)
|
|
|
|
if err := os.Symlink(srcLinkPath, dstLinkPath); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to create certificate symlink:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to create certificate symlink: %s", err.Error())})
|
|
|
|
return
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
2019-01-21 01:54:27 +00:00
|
|
|
|
|
|
|
// Mark team as active to ensure it'll be generated
|
|
|
|
if ut, err := fic.GetTeam(*uc.Team); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to GetTeam:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during team retrieval."})
|
|
|
|
return
|
2019-01-21 01:54:27 +00:00
|
|
|
} else if !ut.Active {
|
|
|
|
ut.Active = true
|
2022-05-16 09:38:46 +00:00
|
|
|
_, err := ut.Update()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to UpdateTeam after updateCertificateAssociation:", err.Error())
|
|
|
|
}
|
2019-01-21 01:54:27 +00:00
|
|
|
}
|
2018-01-21 13:18:26 +00:00
|
|
|
} else {
|
|
|
|
os.Remove(dstLinkPath)
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
c.JSON(http.StatusOK, cert)
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|