c35b93766d
this change removes the cryptopasta dependency. a couple possible sources of problem with this change: * the encoding used for ECDSA signatures on SignedMessage has changed. the encoding employed by cryptopasta was workable, but not the same as the encoding used for such signatures in the rest of the world (most particularly, on ECDSA signatures in X.509 certificates). I think we'll be best served by using one ECDSA signature encoding from here on, but if we need to use the old encoding for backwards compatibility with existing nodes, that can be arranged. * since there's already a breaking change in SignedMessage, I changed it to send and receive public keys in raw PKIX format, instead of PEM. PEM just adds unhelpful overhead for this case.
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package satellitedb
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
|
"storj.io/storj/pkg/storj"
|
|
"storj.io/storj/pkg/utils"
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
)
|
|
|
|
type certDB struct {
|
|
db *dbx.DB
|
|
}
|
|
|
|
func (b *certDB) SavePublicKey(ctx context.Context, nodeID storj.NodeID, publicKey crypto.PublicKey) error {
|
|
tx, err := b.db.Open(ctx)
|
|
if err != nil {
|
|
return Error.Wrap(err)
|
|
}
|
|
|
|
_, err = tx.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
|
|
if err != nil {
|
|
// no rows err, so create/insert an entry
|
|
pubbytes, err := pkcrypto.PublicKeyToPKIX(publicKey)
|
|
if err != nil {
|
|
return Error.Wrap(utils.CombineErrors(err, tx.Rollback()))
|
|
}
|
|
|
|
if err != nil {
|
|
return Error.Wrap(utils.CombineErrors(err, tx.Rollback()))
|
|
}
|
|
_, err = tx.Create_CertRecord(ctx,
|
|
dbx.CertRecord_Publickey(pubbytes),
|
|
dbx.CertRecord_Id(nodeID.Bytes()),
|
|
)
|
|
if err != nil {
|
|
return Error.Wrap(utils.CombineErrors(err, tx.Rollback()))
|
|
}
|
|
} else {
|
|
// nodeID entry already exists, just return
|
|
return Error.Wrap(tx.Rollback())
|
|
}
|
|
|
|
return Error.Wrap(tx.Commit())
|
|
}
|
|
|
|
func (b *certDB) GetPublicKey(ctx context.Context, nodeID storj.NodeID) (crypto.PublicKey, error) {
|
|
dbxInfo, err := b.db.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pubkey, err := pkcrypto.PublicKeyFromPKIX(dbxInfo.Publickey)
|
|
if err != nil {
|
|
return nil, Error.New("Failed to extract Public Key from RenterBandwidthAllocation: %+v", err)
|
|
}
|
|
return pubkey, nil
|
|
}
|