provider.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ecdsa
  14. import (
  15. "bytes"
  16. "fmt"
  17. "strings"
  18. "github.com/ipfs/go-log/v2"
  19. "github.com/mudler/edgevpn/pkg/blockchain"
  20. "github.com/mudler/edgevpn/pkg/hub"
  21. "github.com/mudler/edgevpn/pkg/node"
  22. )
  23. type ECDSA521 struct {
  24. privkey string
  25. logger log.StandardLogger
  26. }
  27. // ECDSA521Provider returns an ECDSA521 auth provider.
  28. // To use it, use the following configuration to provide a
  29. // private key: AuthProviders: map[string]map[string]interface{}{"ecdsa": {"private_key": "<key>"}},
  30. // While running, keys can be added from a TZ node also from the api, for example:
  31. // curl -X PUT 'http://localhost:8081/api/ledger/trustzoneAuth/ecdsa_1/<key>'
  32. // Note: privkey and pubkeys are in the format generated by GenerateKeys() down below
  33. // The provider resolves "ecdsa" keys in the trustzone auth area, and
  34. // uses each one as pubkey to try to auth against
  35. func ECDSA521Provider(ll log.StandardLogger, privkey string) (*ECDSA521, error) {
  36. return &ECDSA521{privkey: privkey, logger: ll}, nil
  37. }
  38. // Authenticate a message against a set of pubkeys.
  39. // It cycles over all the Trusted zone Auth data ( providers options, not where senders ID are stored)
  40. // and detects any key with ecdsa prefix. Values are assumed to be string and parsed as pubkeys.
  41. // The pubkeys are then used to authenticate nodes and verify if any of the pubkeys validates the challenge.
  42. func (e *ECDSA521) Authenticate(m *hub.Message, c chan *hub.Message, tzdata map[string]blockchain.Data) bool {
  43. sigs, ok := m.Annotations["sigs"]
  44. if !ok {
  45. e.logger.Debug("No signature in message", m.Message, m.Annotations)
  46. return false
  47. }
  48. e.logger.Debug("ECDSA auth Received", m)
  49. pubKeys := []string{}
  50. for k, t := range tzdata {
  51. if strings.Contains(k, "ecdsa") {
  52. var s string
  53. t.Unmarshal(&s)
  54. pubKeys = append(pubKeys, s)
  55. }
  56. }
  57. if len(pubKeys) == 0 {
  58. e.logger.Debug("ECDSA auth: No pubkeys to auth against")
  59. // no pubkeys to authenticate present in the ledger
  60. return false
  61. }
  62. for _, pubkey := range pubKeys {
  63. // Try verifying the signature
  64. if err := verify([]byte(pubkey), []byte(fmt.Sprint(sigs)), bytes.NewBufferString(m.Message)); err == nil {
  65. e.logger.Debug("ECDSA auth: Signature verified")
  66. return true
  67. }
  68. e.logger.Debug("ECDSA auth: Signature not verified")
  69. }
  70. return false
  71. }
  72. // Challenger sends ECDSA521 challenges over the public channel if the current node is not in the trusted zone.
  73. // This start a challenge which eventually should get the node into the TZ
  74. func (e *ECDSA521) Challenger(inTrustZone bool, c node.Config, n *node.Node, b *blockchain.Ledger, trustData map[string]blockchain.Data) {
  75. if !inTrustZone {
  76. e.logger.Debug("ECDSA auth: current node not in trustzone, sending challanges")
  77. signature, err := sign([]byte(e.privkey), bytes.NewBufferString("challenge"))
  78. if err != nil {
  79. e.logger.Error("Error signing message: ", err.Error())
  80. return
  81. }
  82. msg := hub.NewMessage("challenge")
  83. msg.Annotations = make(map[string]interface{})
  84. msg.Annotations["sigs"] = string(signature)
  85. n.PublishMessage(msg)
  86. return
  87. }
  88. }