2021-07-02 23:40:26 -04:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2021-07-04 19:01:09 -04:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rsa"
|
2021-07-02 23:40:26 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"net/http"
|
2021-07-04 19:13:24 -04:00
|
|
|
"strings"
|
2021-07-02 23:40:26 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/openpgp"
|
|
|
|
"golang.org/x/crypto/openpgp/armor"
|
|
|
|
"golang.org/x/crypto/openpgp/packet"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PGPKey struct {
|
|
|
|
FullName, Name, Comment, Email string
|
|
|
|
CreationTime time.Time
|
|
|
|
PublicKeyAlgorithm packet.PublicKeyAlgorithm
|
|
|
|
Fingerprint [20]byte
|
2021-07-04 19:01:09 -04:00
|
|
|
KeyID uint64
|
2021-07-02 23:40:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-07-04 19:01:09 -04:00
|
|
|
KeyID: entity.PrimaryKey.KeyId,
|
2021-07-02 23:40:26 -04:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2021-07-04 19:01:09 -04:00
|
|
|
|
|
|
|
// bitLength, _ := entity.PrimaryKey.BitLength()
|
|
|
|
var bitLength int
|
|
|
|
|
|
|
|
switch entity.PrimaryKey.PubKeyAlgo {
|
|
|
|
case packet.PubKeyAlgoECDSA:
|
2021-07-04 19:13:24 -04:00
|
|
|
if ecdsaKey, ok := entity.PrimaryKey.PublicKey.(*ecdsa.PublicKey); ok {
|
|
|
|
bitLength = ecdsaKey.Params().BitSize
|
|
|
|
} else {
|
|
|
|
panic("expected ecdsa.PublicKey for type packet.PubKeyAlgoECDSA")
|
|
|
|
}
|
|
|
|
case packet.PubKeyAlgoRSA:
|
|
|
|
if rsaKey, ok := entity.PrimaryKey.PublicKey.(*rsa.PublicKey); ok {
|
|
|
|
bitLength = rsaKey.N.BitLen()
|
|
|
|
} else {
|
|
|
|
panic("expected rsa.PublicKey for type packet.PubKeyAlgoRSA")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
val, _ := entity.PrimaryKey.BitLength()
|
|
|
|
bitLength = int(val)
|
2021-07-04 19:01:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-02 23:40:26 -04:00
|
|
|
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,
|
2021-07-04 19:13:24 -04:00
|
|
|
"fingerprint": strings.ToUpper(hex.EncodeToString(key.Fingerprint[:])),
|
2021-07-04 19:01:09 -04:00
|
|
|
"keyID": entity.PrimaryKey.KeyIdString(),
|
2021-07-04 19:13:24 -04:00
|
|
|
"bitLength": bitLength,
|
2021-07-02 23:40:26 -04:00
|
|
|
})
|
|
|
|
}
|