| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
|
| 6 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 7 |
) |
| 8 |
|
| 9 |
type PublicKey struct { |
| 10 |
Flags KeyState |
| 11 |
PublicKey string |
| 12 |
} |
| 13 |
|
| 14 |
type PublicKeys []PublicKey |
| 15 |
|
| 16 |
func (keys PublicKeys) GetKeyRing() (*crypto.KeyRing, error) { |
| 17 |
kr, err := crypto.NewKeyRing(nil) |
| 18 |
if err != nil { |
| 19 |
return nil, err |
| 20 |
} |
| 21 |
|
| 22 |
for _, key := range keys { |
| 23 |
pubKey, err := crypto.NewKeyFromArmored(key.PublicKey) |
| 24 |
if err != nil { |
| 25 |
return nil, err |
| 26 |
} |
| 27 |
|
| 28 |
if err := kr.AddKey(pubKey); err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
return kr, nil |
| 34 |
} |
| 35 |
|
| 36 |
type KeyList struct { |
| 37 |
Data string |
| 38 |
Signature string |
| 39 |
} |
| 40 |
|
| 41 |
func NewKeyList(signer *crypto.KeyRing, entries []KeyListEntry) (KeyList, error) { |
| 42 |
data, err := json.Marshal(entries) |
| 43 |
if err != nil { |
| 44 |
return KeyList{}, err |
| 45 |
} |
| 46 |
|
| 47 |
sig, err := signer.SignDetached(crypto.NewPlainMessage(data)) |
| 48 |
if err != nil { |
| 49 |
return KeyList{}, err |
| 50 |
} |
| 51 |
|
| 52 |
arm, err := sig.GetArmored() |
| 53 |
if err != nil { |
| 54 |
return KeyList{}, err |
| 55 |
} |
| 56 |
|
| 57 |
return KeyList{ |
| 58 |
Data: string(data), |
| 59 |
Signature: arm, |
| 60 |
}, nil |
| 61 |
} |
| 62 |
|
| 63 |
type KeyListEntry struct { |
| 64 |
Fingerprint string |
| 65 |
SHA256Fingerprints []string |
| 66 |
Flags KeyState |
| 67 |
Primary Bool |
| 68 |
} |
| 69 |
|
| 70 |
type KeyState int |
| 71 |
|
| 72 |
const ( |
| 73 |
KeyStateTrusted KeyState = 1 << iota // 2^0 = 1 means the key is not compromised (i.e. if we can trust signatures coming from it) |
| 74 |
KeyStateActive // 2^1 = 2 means the key is still in use (i.e. not obsolete, we can encrypt messages to it) |
| 75 |
) |
| 76 |
|
| 77 |
type CreateAddressKeyReq struct { |
| 78 |
AddressID string |
| 79 |
PrivateKey string |
| 80 |
Primary Bool |
| 81 |
SignedKeyList KeyList |
| 82 |
|
| 83 |
// The following are only used in "migrated accounts" |
| 84 |
Token string `json:",omitempty"` |
| 85 |
Signature string `json:",omitempty"` |
| 86 |
} |
| 87 |
|
| 88 |
type MakeAddressKeyPrimaryReq struct { |
| 89 |
SignedKeyList KeyList |
| 90 |
} |