package fic import ( "time" "golang.org/x/crypto/blake2b" ) // Key represents a flag's challenge, stored as hash. type Key struct { Id int64 `json:"id"` // IdExercice is the identifier of the underlying challenge IdExercice int64 `json:"idExercice"` // Label is the title of the flag as displayed to players Label string `json:"label"` // Checksum is the expected hashed flag Checksum []byte `json:"value"` } // GetKeys returns a list of flags comming with the challenge. func (e Exercice) GetKeys() ([]Key, error) { if rows, err := DBQuery("SELECT id_key, id_exercice, type, cksum FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil { return nil, err } else { defer rows.Close() var keys = make([]Key, 0) for rows.Next() { var k Key k.IdExercice = e.Id if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Checksum); err != nil { return nil, err } keys = append(keys, k) } if err := rows.Err(); err != nil { return nil, err } return keys, nil } } // getHashedKey calculates the expected checksum for the given raw_value. func getHashedKey(raw_value string) [blake2b.Size]byte { hash := blake2b.Sum512([]byte(raw_value)) return hash } // AddRawKey creates and fills a new struct Key, from a non-hashed flag, and registers it into the database. func (e Exercice) AddRawKey(name string, raw_value string) (Key, error) { hash := getHashedKey(raw_value) return e.AddKey(name, hash[:]) } // AddKey creates and fills a new struct Key, from a hashed flag, and registers it into the database. func (e Exercice) AddKey(name string, checksum []byte) (Key, error) { if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, cksum) VALUES (?, ?, ?)", e.Id, name, checksum); err != nil { return Key{}, err } else if kid, err := res.LastInsertId(); err != nil { return Key{}, err } else { return Key{kid, e.Id, name, checksum}, nil } } // Update applies modifications back to the database. func (k Key) Update() (int64, error) { if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Label, k.Checksum, k.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err } else { return nb, err } } // Delete the flag from the database. func (k Key) Delete() (int64, error) { if res, err := DBExec("DELETE FROM exercice_keys WHERE id_key = ?", k.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err } else { return nb, err } } // WipeKeys deletes keys coming with the challenge. func (e Exercice) WipeKeys() (int64, error) { if res, err := DBExec("DELETE FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err } else { return nb, err } } // Check if the given val is the expected one for this flag. func (k Key) Check(val string) bool { hash := getHashedKey(val) if len(k.Checksum) != len(hash) { return false } for i := range hash { if k.Checksum[i] != hash[i] { return false } } return true } // FoundBy registers in the database that the given Team solved the flag. func (k Key) FoundBy(t Team) { DBExec("INSERT INTO key_found (id_key, id_team, time) VALUES (?, ?, ?)", k.Id, t.Id, time.Now()) }