Store hashes pk

This commit is contained in:
Andrea Maria Piana 2020-07-02 12:49:04 +02:00
parent a15919527b
commit 2eee956dc6
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
1 changed files with 7 additions and 4 deletions

View File

@ -29,7 +29,7 @@ func NewSQLitePersistence(db *sql.DB) Persistence {
func (p *SQLitePersistence) GetPushNotificationRegistration(publicKey *ecdsa.PublicKey, installationID string) (*protobuf.PushNotificationRegistration, error) { func (p *SQLitePersistence) GetPushNotificationRegistration(publicKey *ecdsa.PublicKey, installationID string) (*protobuf.PushNotificationRegistration, error) {
var marshaledRegistration []byte var marshaledRegistration []byte
err := p.db.QueryRow(`SELECT registration FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, crypto.CompressPubkey(publicKey), installationID).Scan(&marshaledRegistration) err := p.db.QueryRow(`SELECT registration FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, p.hashPublicKey(publicKey), installationID).Scan(&marshaledRegistration)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, nil return nil, nil
@ -46,17 +46,20 @@ func (p *SQLitePersistence) GetPushNotificationRegistration(publicKey *ecdsa.Pub
} }
func (p *SQLitePersistence) SavePushNotificationRegistration(publicKey *ecdsa.PublicKey, registration *protobuf.PushNotificationRegistration) error { func (p *SQLitePersistence) SavePushNotificationRegistration(publicKey *ecdsa.PublicKey, registration *protobuf.PushNotificationRegistration) error {
compressedPublicKey := crypto.CompressPubkey(publicKey)
marshaledRegistration, err := proto.Marshal(registration) marshaledRegistration, err := proto.Marshal(registration)
if err != nil { if err != nil {
return err return err
} }
_, err = p.db.Exec(`INSERT INTO push_notification_server_registrations (public_key, installation_id, version, registration) VALUES (?, ?, ?, ?)`, compressedPublicKey, registration.InstallationId, registration.Version, marshaledRegistration) _, err = p.db.Exec(`INSERT INTO push_notification_server_registrations (public_key, installation_id, version, registration) VALUES (?, ?, ?, ?)`, p.hashPublicKey(publicKey), registration.InstallationId, registration.Version, marshaledRegistration)
return err return err
} }
func (p *SQLitePersistence) DeletePushNotificationRegistration(publicKey *ecdsa.PublicKey, installationID string) error { func (p *SQLitePersistence) DeletePushNotificationRegistration(publicKey *ecdsa.PublicKey, installationID string) error {
_, err := p.db.Exec(`DELETE FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, crypto.CompressPubkey(publicKey), installationID) _, err := p.db.Exec(`DELETE FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, p.hashPublicKey(publicKey), installationID)
return err return err
} }
func (p *SQLitePersistence) hashPublicKey(pk *ecdsa.PublicKey) []byte {
return shake256(crypto.CompressPubkey(pk))
}