diff --git a/cmd/main.go b/cmd/main.go index 84a5cef..773787d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,6 +11,7 @@ func main() { router.GET("/", routes.GetCertificateInfo) router.GET("/tls", routes.GetCertificateInfo) router.POST("/parse", routes.GetCertificateInformationEncoded) + router.POST("/pgp", routes.GetOpenPGPInformationEncoded) router.Run() } diff --git a/go.mod b/go.mod index ea46623..168375b 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/ugorji/go v1.2.1 // indirect - golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9 // indirect + golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9 golang.org/x/sys v0.0.0-20201211002650-1f0c578a6b29 // indirect google.golang.org/protobuf v1.25.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/routes/get.go b/routes/get.go index f932ebe..28f9be4 100644 --- a/routes/get.go +++ b/routes/get.go @@ -1,6 +1,9 @@ package routes import ( + "golang.org/x/crypto/openpgp" + "golang.org/x/crypto/openpgp/armor" + "golang.org/x/crypto/openpgp/packet" "crypto/ecdsa" "crypto/ed25519" "crypto/rsa" @@ -11,10 +14,74 @@ import ( "encoding/pem" "io/ioutil" "net/http" + "time" "github.com/gin-gonic/gin" ) +type PGPKey struct { + FullName, Name, Comment, Email string + CreationTime time.Time + PublicKeyAlgorithm packet.PublicKeyAlgorithm + Fingerprint [20]byte + KeyID uint16 +} + +func GetOpenPGPInformationEncoded(c *gin.Context) { + query := c.Copy().Request.Body + + block, err := armor.Decode(query) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": false, + "message": "Unable to parse body.", + }) + return + } + pkt := packet.NewReader(block.Body) + entity, err := openpgp.ReadEntity(pkt) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": false, + "message": "Unable to parse body.", + }) + return + } + if len(entity.Identities) > 1 { + c.JSON(http.StatusBadRequest, gin.H{ + "status": false, + "message": "No identities found in PGP key.", + }) + return + } + var key *PGPKey + for name, identity := range entity.Identities { + key = &PGPKey{ + FullName: name, + Name: identity.UserId.Name, + Comment: identity.UserId.Comment, + Email: identity.UserId.Email, + CreationTime: entity.PrimaryKey.CreationTime, + PublicKeyAlgorithm: entity.PrimaryKey.PubKeyAlgo, + Fingerprint: entity.PrimaryKey.Fingerprint, + KeyID: uint16(entity.PrimaryKey.KeyId), + } + break + } + + c.JSON(http.StatusOK, gin.H{ + "status": true, + "fullName": key.FullName, + "name": key.Name, + "comment": key.Comment, + "email": key.Email, + "creationTime": key.CreationTime, + "publicKeyAlgorithm": key.PublicKeyAlgorithm, + "fingerprint": hex.EncodeToString(key.Fingerprint[:]), + "keyID": key.KeyID, + }) +} + // GetCertificateInformationEncoded handler function for providing raw data to be parsed func GetCertificateInformationEncoded(c *gin.Context) { query := c.Copy().Request.Body