91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package config
|
||
|
|
||
|
import (
|
||
|
"crypto/x509"
|
||
|
"crypto/x509/pkix"
|
||
|
"crypto/rsa"
|
||
|
"crypto/rand"
|
||
|
"encoding/pem"
|
||
|
"encoding/json"
|
||
|
"github.com/ProtonMail/go-crypto/openpgp"
|
||
|
"github.com/ProtonMail/go-crypto/openpgp/packet"
|
||
|
)
|
||
|
|
||
|
type OpenPGP struct {
|
||
|
Armored string
|
||
|
entities openpgp.EntityList
|
||
|
}
|
||
|
|
||
|
func (o *OpenPGP) Read() (yamlData []byte, err error) {
|
||
|
pemReader := io.NopCloser(strings.NewReader(o.Armored))
|
||
|
o.entities, err = openpgp.ReadArmoredKeyRing(pemReader)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
|
||
|
func (o *OpenPGP) UnmarshalJSON(data []byte) error {
|
||
|
if unmarshalErr := json.Unmarshal(data, o); unmarshalErr != nil {
|
||
|
return unmarshalErr
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *OpenPGP) UnmarshalYAML(value *yaml.Node) error {
|
||
|
type decodeOpenPGP OpenPGP
|
||
|
if unmarshalErr := value.Decode((*decodeOpenPGP)(o)); unmarshalErr != nil {
|
||
|
return unmarshalErr
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *OpenPGP) Clone() data.Configuration {
|
||
|
jsonGeneric, _ := json.Marshal(c)
|
||
|
clone := NewOpenPGP()
|
||
|
if unmarshalErr := json.Unmarshal(jsonGeneric, &clone); unmarshalErr != nil {
|
||
|
panic(unmarshalErr)
|
||
|
}
|
||
|
return clone
|
||
|
}
|
||
|
|
||
|
func (o *OpenPGP) Type() string {
|
||
|
return "openpgp"
|
||
|
}
|
||
|
|
||
|
func (o *OpenPGP) GetEntityIndex(key string) (index int, field string, err error) {
|
||
|
values := strings.SplitN(key, ".", 2)
|
||
|
if len(values) == 2 {
|
||
|
if index, err = strconv.Atoi(values[0]); err == nil {
|
||
|
field = values[1]
|
||
|
}
|
||
|
} else {
|
||
|
err = data.ErrUnknownConfigurationKey
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (o *OpenPGP) GetValue(name string) (result any, err error) {
|
||
|
var ok bool
|
||
|
if result, ok = (*c)[name]; !ok {
|
||
|
err = data.ErrUnknownConfigurationKey
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
|
||
|
// Expected key: 0.PrivateKey
|
||
|
func (o *OpenPGP) Has(key string) (ok bool) {
|
||
|
index, field, err := o.GetEntityIndex(key)
|
||
|
if len(o.entities) > index && err == nil {
|
||
|
switch key {
|
||
|
case PublicKey:
|
||
|
ok = o.entities[index].PrimaryKey != nil
|
||
|
case PrivateKey:
|
||
|
ok = o.entities[index].PrimaryKey != nil
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|