diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Database.go | 2 | ||||
-rw-r--r-- | databaseschema.psql | 8 | ||||
-rw-r--r-- | main.go | 19 | ||||
-rw-r--r-- | verification.go | 68 |
5 files changed, 95 insertions, 5 deletions
@@ -2,4 +2,5 @@ #* public/ config -clientkey
\ No newline at end of file +clientkey +pem/ diff --git a/Database.go b/Database.go index a83c37d..f10ae12 100644 --- a/Database.go +++ b/Database.go @@ -120,6 +120,8 @@ func CreateNewBoardDB(db *sql.DB, actor Actor) Actor{ SetActorFollowingDB(db, response) MakeActivityRequest(db, nActivity) } + + CreatePem(db, actor) } return actor diff --git a/databaseschema.psql b/databaseschema.psql index eac2c34..686e682 100644 --- a/databaseschema.psql +++ b/databaseschema.psql @@ -207,6 +207,12 @@ id varchar(100), type varchar(25) ); - ALTER TABLE activitystream ADD COLUMN IF NOT EXISTS tripcode varchar(50) default ''; ALTER TABLE cacheactivitystream ADD COLUMN IF NOT EXISTS tripcode varchar(50) default ''; + +CREATE TABLE IF NOT EXISTS publicKeyPem( +id varchar(100) UNIQUE, +file varchar(100) +); + +ALTER TABLE actor ADD COLUMN IF NOT EXISTS publicKeyPem varchar(100) default '';
\ No newline at end of file @@ -38,9 +38,7 @@ var activitystreams = "application/ld+json; profile=\"https://www.w3.org/ns/acti func main() { - if _, err := os.Stat("./public"); os.IsNotExist(err) { - os.Mkdir("./public", 0755) - } + CreatedNeededDirectories() InitCache() @@ -49,6 +47,11 @@ func main() { defer db.Close() RunDatabaseSchema(db) + + CreatePem(db, GetActorByNameFromDB(db, "main")) + + f, _ := os.ReadFile("./pem/board/main-public.pem") + fmt.Println(strings.ReplaceAll(string(f), "\n", `\n`)) go MakeCaptchas(db, 100) @@ -2106,3 +2109,13 @@ func RunDatabaseSchema(db *sql.DB) { CheckError(err, "could not exec databaseschema.psql") } } + +func CreatedNeededDirectories() { + if _, err := os.Stat("./public"); os.IsNotExist(err) { + os.Mkdir("./public", 0755) + } + + if _, err := os.Stat("./pem/board"); os.IsNotExist(err) { + os.MkdirAll("./pem/board", 0700) + } +} diff --git a/verification.go b/verification.go index 8dc5a6b..ec64f63 100644 --- a/verification.go +++ b/verification.go @@ -8,6 +8,10 @@ import "time" import "os/exec" import "os" import "math/rand" +import "crypto/rsa" +import "crypto/x509" +import "encoding/pem" +import crand "crypto/rand" type Verify struct { Type string @@ -471,4 +475,68 @@ func Captcha() string { return newID } +func CreatePem(db *sql.DB, actor Actor) { + privatekey, err := rsa.GenerateKey(crand.Reader, 2048) + CheckError(err, "error creating private pem key") + privateKeyBytes := x509.MarshalPKCS1PrivateKey(privatekey) + + privateKeyBlock := &pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: privateKeyBytes, + } + + privatePem, err := os.Create("./pem/board/" + actor.Name + "-private.pem") + CheckError(err, "error creating private pem file for " + actor.Name) + + err = pem.Encode(privatePem, privateKeyBlock) + CheckError(err, "error encoding private pem") + + publickey := &privatekey.PublicKey + publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey) + CheckError(err, "error Marshaling public key to X509") + + publicKeyBlock := &pem.Block{ + Type: "PUBLIC KEY", + Bytes: publicKeyBytes, + } + + publicPem, err := os.Create("./pem/board/" + actor.Name + "-public.pem") + CheckError(err, "error creating public pem file for " + actor.Name) + + err = pem.Encode(publicPem, publicKeyBlock) + CheckError(err, "error encoding public pem") + + _, err = os.Stat("./pem/board/" + actor.Name + "-public.pem") + if os.IsNotExist(err) { + CheckError(err, "public pem file for actor does not exist") + } else { + StorePemToDB(db, actor) + } +} + +func StorePemToDB(db *sql.DB, actor Actor) { + query := "select publicKeyPem from actor where id=$1" + rows, err := db.Query(query, actor.Id) + + CheckError(err, "error selecting publicKeyPem id from actor") + + var result string + defer rows.Close() + rows.Next() + rows.Scan(&result) + + if(result != "") { + return + } + + publicKeyPem := actor.Id + "#main-key" + query = "update actor set publicKeyPem=$1 where id=$2" + _, err = db.Exec(query, publicKeyPem, actor.Id) + CheckError(err, "error updating publicKeyPem id to actor") + + file := "./pem/board/" + actor.Name + "-public.pem" + query = "insert into publicKeyPem (id, file) values($1, $2)" + _, err = db.Exec(query, publicKeyPem, file) + CheckError(err, "error creating publicKeyPem for actor ") +} |