| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package id |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
// OlmMsgType is an Olm message type |
| 15 |
type OlmMsgType int |
| 16 |
|
| 17 |
const ( |
| 18 |
OlmMsgTypePreKey OlmMsgType = 0 |
| 19 |
OlmMsgTypeMsg OlmMsgType = 1 |
| 20 |
) |
| 21 |
|
| 22 |
// Algorithm is a Matrix message encryption algorithm. |
| 23 |
// https://spec.matrix.org/v1.2/client-server-api/#messaging-algorithm-names |
| 24 |
type Algorithm string |
| 25 |
|
| 26 |
const ( |
| 27 |
AlgorithmOlmV1 Algorithm = "m.olm.v1.curve25519-aes-sha2" |
| 28 |
AlgorithmMegolmV1 Algorithm = "m.megolm.v1.aes-sha2" |
| 29 |
) |
| 30 |
|
| 31 |
type KeyAlgorithm string |
| 32 |
|
| 33 |
const ( |
| 34 |
KeyAlgorithmCurve25519 KeyAlgorithm = "curve25519" |
| 35 |
KeyAlgorithmEd25519 KeyAlgorithm = "ed25519" |
| 36 |
KeyAlgorithmSignedCurve25519 KeyAlgorithm = "signed_curve25519" |
| 37 |
) |
| 38 |
|
| 39 |
type CrossSigningUsage string |
| 40 |
|
| 41 |
const ( |
| 42 |
XSUsageMaster CrossSigningUsage = "master" |
| 43 |
XSUsageSelfSigning CrossSigningUsage = "self_signing" |
| 44 |
XSUsageUserSigning CrossSigningUsage = "user_signing" |
| 45 |
) |
| 46 |
|
| 47 |
// A SessionID is an arbitrary string that identifies an Olm or Megolm session. |
| 48 |
type SessionID string |
| 49 |
|
| 50 |
func (sessionID SessionID) String() string { |
| 51 |
return string(sessionID) |
| 52 |
} |
| 53 |
|
| 54 |
// Ed25519 is the base64 representation of an Ed25519 public key |
| 55 |
type Ed25519 string |
| 56 |
type SigningKey = Ed25519 |
| 57 |
|
| 58 |
func (ed25519 Ed25519) String() string { |
| 59 |
return string(ed25519) |
| 60 |
} |
| 61 |
|
| 62 |
// Curve25519 is the base64 representation of an Curve25519 public key |
| 63 |
type Curve25519 string |
| 64 |
type SenderKey = Curve25519 |
| 65 |
type IdentityKey = Curve25519 |
| 66 |
|
| 67 |
func (curve25519 Curve25519) String() string { |
| 68 |
return string(curve25519) |
| 69 |
} |
| 70 |
|
| 71 |
// A DeviceID is an arbitrary string that references a specific device. |
| 72 |
type DeviceID string |
| 73 |
|
| 74 |
func (deviceID DeviceID) String() string { |
| 75 |
return string(deviceID) |
| 76 |
} |
| 77 |
|
| 78 |
// A DeviceKeyID is a string formatted as <algorithm>:<device_id> that is used as the key in deviceid-key mappings. |
| 79 |
type DeviceKeyID string |
| 80 |
|
| 81 |
func NewDeviceKeyID(algorithm KeyAlgorithm, deviceID DeviceID) DeviceKeyID { |
| 82 |
return DeviceKeyID(fmt.Sprintf("%s:%s", algorithm, deviceID)) |
| 83 |
} |
| 84 |
|
| 85 |
func (deviceKeyID DeviceKeyID) String() string { |
| 86 |
return string(deviceKeyID) |
| 87 |
} |
| 88 |
|
| 89 |
func (deviceKeyID DeviceKeyID) Parse() (Algorithm, DeviceID) { |
| 90 |
index := strings.IndexRune(string(deviceKeyID), ':') |
| 91 |
if index < 0 || len(deviceKeyID) <= index+1 { |
| 92 |
return "", "" |
| 93 |
} |
| 94 |
return Algorithm(deviceKeyID[:index]), DeviceID(deviceKeyID[index+1:]) |
| 95 |
} |
| 96 |
|
| 97 |
// A KeyID a string formatted as <keyalgorithm>:<key_id> that is used as the key in one-time-key mappings. |
| 98 |
type KeyID string |
| 99 |
|
| 100 |
func NewKeyID(algorithm KeyAlgorithm, keyID string) KeyID { |
| 101 |
return KeyID(fmt.Sprintf("%s:%s", algorithm, keyID)) |
| 102 |
} |
| 103 |
|
| 104 |
func (keyID KeyID) String() string { |
| 105 |
return string(keyID) |
| 106 |
} |
| 107 |
|
| 108 |
func (keyID KeyID) Parse() (KeyAlgorithm, string) { |
| 109 |
index := strings.IndexRune(string(keyID), ':') |
| 110 |
if index < 0 || len(keyID) <= index+1 { |
| 111 |
return "", "" |
| 112 |
} |
| 113 |
return KeyAlgorithm(keyID[:index]), string(keyID[index+1:]) |
| 114 |
} |