]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/node.go
Configure sendmail command per node
[nncp.git] / src / cypherpunks.ru / nncp / node.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package nncp
20
21 import (
22         "crypto/rand"
23         "errors"
24
25         "github.com/flynn/noise"
26         "golang.org/x/crypto/blake2b"
27         "golang.org/x/crypto/ed25519"
28         "golang.org/x/crypto/nacl/box"
29 )
30
31 type NodeId [blake2b.Size256]byte
32
33 func (id NodeId) String() string {
34         return ToBase32(id[:])
35 }
36
37 type Node struct {
38         Name     string
39         Id       *NodeId
40         ExchPub  *[32]byte
41         SignPub  ed25519.PublicKey
42         NoisePub *[32]byte
43         Sendmail []string
44         Incoming *string
45         Freq     *string
46         Via      []*NodeId
47         Addrs    map[string]string
48 }
49
50 type NodeOur struct {
51         Id       *NodeId
52         ExchPub  *[32]byte
53         ExchPrv  *[32]byte
54         SignPub  ed25519.PublicKey
55         SignPrv  ed25519.PrivateKey
56         NoisePub *[32]byte
57         NoisePrv *[32]byte
58 }
59
60 func NewNodeGenerate() (*NodeOur, error) {
61         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
62         if err != nil {
63                 return nil, err
64         }
65         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
66         if err != nil {
67                 return nil, err
68         }
69         noiseKey := noise.DH25519.GenerateKeypair(rand.Reader)
70         noisePub := new([32]byte)
71         noisePrv := new([32]byte)
72         copy(noisePrv[:], noiseKey.Private)
73         copy(noisePub[:], noiseKey.Public)
74
75         id := NodeId(blake2b.Sum256([]byte(signPub)))
76         node := NodeOur{
77                 Id:       &id,
78                 ExchPub:  exchPub,
79                 ExchPrv:  exchPrv,
80                 SignPub:  signPub,
81                 SignPrv:  signPrv,
82                 NoisePub: noisePub,
83                 NoisePrv: noisePrv,
84         }
85         return &node, nil
86 }
87
88 func (nodeOur *NodeOur) Their() *Node {
89         return &Node{
90                 Name:    "self",
91                 Id:      nodeOur.Id,
92                 ExchPub: nodeOur.ExchPub,
93                 SignPub: nodeOur.SignPub,
94         }
95 }
96
97 func NodeIdFromString(raw string) (*NodeId, error) {
98         decoded, err := FromBase32(raw)
99         if err != nil {
100                 return nil, err
101         }
102         if len(decoded) != blake2b.Size256 {
103                 return nil, errors.New("Invalid node id size")
104         }
105         buf := new([blake2b.Size256]byte)
106         copy(buf[:], decoded)
107         nodeId := NodeId(*buf)
108         return &nodeId, nil
109 }