diff options
Diffstat (limited to 'activitypub/pem.go')
-rw-r--r-- | activitypub/pem.go | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/activitypub/pem.go b/activitypub/pem.go index ab225a9..408be0c 100644 --- a/activitypub/pem.go +++ b/activitypub/pem.go @@ -13,6 +13,7 @@ import ( "strings" "github.com/FChannel0/FChannel-Server/config" + "github.com/FChannel0/FChannel-Server/util" ) type Signature struct { @@ -25,7 +26,7 @@ type Signature struct { func CreatePem(actor Actor) error { privatekey, err := rsa.GenerateKey(crand.Reader, 2048) if err != nil { - return err + return util.MakeError(err, "CreatePem") } privateKeyBytes := x509.MarshalPKCS1PrivateKey(privatekey) @@ -37,17 +38,17 @@ func CreatePem(actor Actor) error { privatePem, err := os.Create("./pem/board/" + actor.Name + "-private.pem") if err != nil { - return err + return util.MakeError(err, "CreatePem") } if err := pem.Encode(privatePem, privateKeyBlock); err != nil { - return err + return util.MakeError(err, "CreatePem") } publickey := &privatekey.PublicKey publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey) if err != nil { - return err + return util.MakeError(err, "CreatePem") } publicKeyBlock := &pem.Block{ @@ -57,16 +58,16 @@ func CreatePem(actor Actor) error { publicPem, err := os.Create("./pem/board/" + actor.Name + "-public.pem") if err != nil { - return err + return util.MakeError(err, "CreatePem") } if err := pem.Encode(publicPem, publicKeyBlock); err != nil { - return err + return util.MakeError(err, "CreatePem") } _, err = os.Stat("./pem/board/" + actor.Name + "-public.pem") if os.IsNotExist(err) { - return err + return util.MakeError(err, "CreatePem") } else { return StorePemToDB(actor) } @@ -81,7 +82,7 @@ so DO NOT LOSE IT!!! If you lose it, YOU WILL LOSE ACCESS TO YOUR BOARD!`) func CreatePublicKeyFromPrivate(actor *Actor, publicKeyPem string) error { publicFilename, err := GetActorPemFileFromDB(publicKeyPem) if err != nil { - return err + return util.MakeError(err, "CreatePublicKeyFromPrivate") } privateFilename := strings.ReplaceAll(publicFilename, "public.pem", "private.pem") @@ -89,7 +90,7 @@ func CreatePublicKeyFromPrivate(actor *Actor, publicKeyPem string) error { // Not a lost cause priv, err := ioutil.ReadFile(privateFilename) if err != nil { - return err + return util.MakeError(err, "CreatePublicKeyFromPrivate") } block, _ := pem.Decode([]byte(priv)) @@ -99,12 +100,12 @@ func CreatePublicKeyFromPrivate(actor *Actor, publicKeyPem string) error { key, err := x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { - return err + return util.MakeError(err, "CreatePublicKeyFromPrivate") } publicKeyDer, err := x509.MarshalPKIXPublicKey(&key.PublicKey) if err != nil { - return err + return util.MakeError(err, "CreatePublicKeyFromPrivate") } pubKeyBlock := pem.Block{ @@ -115,11 +116,11 @@ func CreatePublicKeyFromPrivate(actor *Actor, publicKeyPem string) error { publicFileWriter, err := os.Create(publicFilename) if err != nil { - return err + return util.MakeError(err, "CreatePublicKeyFromPrivate") } if err := pem.Encode(publicFileWriter, &pubKeyBlock); err != nil { - return err + return util.MakeError(err, "CreatePublicKeyFromPrivate") } } else { fmt.Println(`\nUnable to locate private key from public key generation. Now, @@ -140,14 +141,14 @@ func GetActorPemFromDB(pemID string) (PublicKeyPem, error) { query := `select id, owner, file from publickeypem where id=$1` if err := config.DB.QueryRow(query, pemID).Scan(&pem.Id, &pem.Owner, &pem.PublicKeyPem); err != nil { - return pem, err + return pem, util.MakeError(err, "GetActorPemFromDB") } dir, _ := os.Getwd() dir = dir + "" + strings.Replace(pem.PublicKeyPem, ".", "", 1) f, err := os.ReadFile(dir) if err != nil { - return pem, err + return pem, util.MakeError(err, "GetActorPemFromDB") } pem.PublicKeyPem = strings.ReplaceAll(string(f), "\r\n", `\n`) @@ -159,7 +160,7 @@ func GetActorPemFileFromDB(pemID string) (string, error) { query := `select file from publickeypem where id=$1` rows, err := config.DB.Query(query, pemID) if err != nil { - return "", err + return "", util.MakeError(err, "GetActorPemFileFromDB") } defer rows.Close() @@ -175,7 +176,7 @@ func StorePemToDB(actor Actor) error { query := "select publicKeyPem from actor where id=$1" rows, err := config.DB.Query(query, actor.Id) if err != nil { - return err + return util.MakeError(err, "StorePemToDB") } defer rows.Close() @@ -191,13 +192,13 @@ func StorePemToDB(actor Actor) error { publicKeyPem := actor.Id + "#main-key" query = "update actor set publicKeyPem=$1 where id=$2" if _, err := config.DB.Exec(query, publicKeyPem, actor.Id); err != nil { - return err + return util.MakeError(err, "StorePemToDB") } file := "./pem/board/" + actor.Name + "-public.pem" query = "insert into publicKeyPem (id, owner, file) values($1, $2, $3)" _, err = config.DB.Exec(query, publicKeyPem, actor.Id, file) - return err + return util.MakeError(err, "StorePemToDB") } func ParseHeaderSignature(signature string) Signature { |