| 1 | package torutil |
| 2 | |
| 3 | import ( |
| 4 | "crypto" |
| 5 | "crypto/rsa" |
| 6 | "crypto/sha1" |
| 7 | "crypto/x509" |
| 8 | "encoding/base32" |
| 9 | "fmt" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/cretz/bine/torutil/ed25519" |
| 13 | "golang.org/x/crypto/sha3" |
| 14 | ) |
| 15 | |
| 16 | var serviceIDEncoding = base32.StdEncoding.WithPadding(base32.NoPadding) |
| 17 | |
| 18 | // OnionServiceIDFromPrivateKey generates the onion service ID from the given |
| 19 | // private key. This panics if the private key is not a 1024-bit |
| 20 | // crypto/*rsa.PrivateKey or github.com/cretz/bine/torutil/ed25519.KeyPair. |
| 21 | func OnionServiceIDFromPrivateKey(key crypto.PrivateKey) string { |
| 22 | switch k := key.(type) { |
| 23 | case *rsa.PrivateKey: |
| 24 | return OnionServiceIDFromV2PublicKey(&k.PublicKey) |
| 25 | case ed25519.KeyPair: |
| 26 | return OnionServiceIDFromV3PublicKey(k.PublicKey()) |
| 27 | } |
| 28 | panic(fmt.Sprintf("Unrecognized private key type: %T", key)) |
| 29 | } |
| 30 | |
| 31 | // OnionServiceIDFromPublicKey generates the onion service ID from the given |
| 32 | // public key. This panics if the public key is not a 1024-bit |
| 33 | // crypto/*rsa.PublicKey or github.com/cretz/bine/torutil/ed25519.PublicKey. |
| 34 | func OnionServiceIDFromPublicKey(key crypto.PublicKey) string { |
| 35 | switch k := key.(type) { |
| 36 | case *rsa.PublicKey: |
| 37 | return OnionServiceIDFromV2PublicKey(k) |
| 38 | case ed25519.PublicKey: |
| 39 | return OnionServiceIDFromV3PublicKey(k) |
| 40 | } |
| 41 | panic(fmt.Sprintf("Unrecognized public key type: %T", key)) |
| 42 | } |
| 43 | |
| 44 | // OnionServiceIDFromV2PublicKey generates a V2 service ID for the given |
| 45 | // RSA-1024 public key. Panics if not a 1024-bit key. |
| 46 | func OnionServiceIDFromV2PublicKey(key *rsa.PublicKey) string { |
| 47 | if key.N.BitLen() != 1024 { |
| 48 | panic("RSA key not 1024 bit") |
| 49 | } |
| 50 | h := sha1.New() |
| 51 | h.Write(x509.MarshalPKCS1PublicKey(key)) |
| 52 | return strings.ToLower(serviceIDEncoding.EncodeToString(h.Sum(nil)[:10])) |
| 53 | } |
| 54 | |
| 55 | // OnionServiceIDFromV3PublicKey generates a V3 service ID for the given |
| 56 | // ED25519 public key. |
| 57 | func OnionServiceIDFromV3PublicKey(key ed25519.PublicKey) string { |
| 58 | checkSum := sha3.Sum256(append(append([]byte(".onion checksum"), key...), 0x03)) |
| 59 | var keyBytes [35]byte |
| 60 | copy(keyBytes[:], key) |
| 61 | keyBytes[32] = checkSum[0] |
| 62 | keyBytes[33] = checkSum[1] |
| 63 | keyBytes[34] = 0x03 |
| 64 | return strings.ToLower(serviceIDEncoding.EncodeToString(keyBytes[:])) |
| 65 | } |
| 66 | |
| 67 | // PublicKeyFromV3OnionServiceID returns a public key for the given service ID |
| 68 | // or an error if the service ID is invalid. |
| 69 | func PublicKeyFromV3OnionServiceID(id string) (ed25519.PublicKey, error) { |
| 70 | byts, err := serviceIDEncoding.DecodeString(strings.ToUpper(id)) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } else if len(byts) != 35 { |
| 74 | return nil, fmt.Errorf("Invalid id length") |
| 75 | } else if byts[34] != 0x03 { |
| 76 | return nil, fmt.Errorf("Invalid version") |
| 77 | } |
| 78 | // Do a checksum check |
| 79 | key := ed25519.PublicKey(byts[:32]) |
| 80 | checkSum := sha3.Sum256(append(append([]byte(".onion checksum"), key...), 0x03)) |
| 81 | if byts[32] != checkSum[0] || byts[33] != checkSum[1] { |
| 82 | return nil, fmt.Errorf("Invalid checksum") |
| 83 | } |
| 84 | return key, nil |
| 85 | } |