1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// SPDX-FileCopyrightText: Heiko Schaefer <heiko@schaefer.name>
// SPDX-License-Identifier: MIT OR Apache-2.0

mod cmd;
mod util;

use std::io;

use pgp::Signature;
use rpgpie::key::{Certificate, Tsk};

#[derive(Clone, Copy, Default)]
pub struct RPGSOP {}

// SOP singleton
const SOP: RPGSOP = RPGSOP {};

pub struct Certs {
    certs: Vec<Certificate>,
}

pub struct Keys {
    keys: Vec<Tsk>,
}

pub struct Sigs {
    sigs: Vec<Signature>,
}

impl sop::SOP<'_> for RPGSOP {
    type Keys = Keys;
    type Certs = Certs;
    type Sigs = Sigs;

    fn version(&'_ self) -> sop::Result<Box<dyn sop::ops::Version>> {
        Ok(Box::new(cmd::version::Version::new()))
    }

    fn sopv_version(&self) -> sop::Result<&'static str> {
        Ok("1.0")
    }

    fn generate_key(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::GenerateKey<Self, Self::Keys> + '_>> {
        Ok(Box::new(cmd::generate::GenerateKey::new()))
    }

    fn change_key_password(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::ChangeKeyPassword<Self, Self::Keys>>> {
        todo!()
    }

    fn revoke_key(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::RevokeKey<Self, Self::Certs, Self::Keys>>> {
        todo!()
    }

    fn extract_cert(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::ExtractCert<Self, Self::Certs, Self::Keys> + '_>> {
        Ok(Box::new(cmd::extract_cert::ExtractCert::new()))
    }

    fn sign(&'_ self) -> sop::Result<Box<dyn sop::ops::Sign<Self, Self::Keys, Self::Sigs> + '_>> {
        Ok(Box::new(cmd::sign::Sign::new()))
    }

    fn verify(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::Verify<Self, Self::Certs, Self::Sigs> + '_>> {
        Ok(Box::new(cmd::verify::Verify::new()))
    }

    fn encrypt(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::Encrypt<Self, Self::Certs, Self::Keys> + '_>> {
        Ok(Box::new(cmd::encrypt::Encrypt::new()))
    }

    fn decrypt(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::Decrypt<Self, Self::Certs, Self::Keys> + '_>> {
        Ok(Box::new(cmd::decrypt::Decrypt::new()))
    }

    fn armor(&'_ self) -> sop::Result<Box<dyn sop::ops::Armor>> {
        todo!()
    }

    fn dearmor(&'_ self) -> sop::Result<Box<dyn sop::ops::Dearmor>> {
        todo!()
    }

    fn inline_detach(&'_ self) -> sop::Result<Box<dyn sop::ops::InlineDetach<Self::Sigs>>> {
        todo!()
    }

    fn inline_verify(
        &'_ self,
    ) -> sop::Result<Box<dyn sop::ops::InlineVerify<Self, Self::Certs> + '_>> {
        Ok(Box::new(cmd::inline_verify::InlineVerify::new()))
    }

    fn inline_sign(&'_ self) -> sop::Result<Box<dyn sop::ops::InlineSign<Self, Self::Keys> + '_>> {
        Ok(Box::new(cmd::inline_sign::InlineSign::new()))
    }
}

impl sop::Load<'_, RPGSOP> for Certs {
    fn from_reader(
        _sop: &RPGSOP,
        mut source: &mut (dyn io::Read + Send + Sync),
    ) -> sop::Result<Self> {
        let certs = Certificate::load(&mut source).expect("FIXME");

        Ok(Certs { certs })
    }
}

impl sop::Save for Certs {
    fn to_writer(
        &self,
        armored: bool,
        sink: &mut (dyn io::Write + Send + Sync),
    ) -> sop::Result<()> {
        Certificate::save(&self.certs, armored, sink).expect("FIXME");

        Ok(())
    }
}

impl sop::Load<'_, RPGSOP> for Keys {
    fn from_reader(
        _sop: &'_ RPGSOP,
        mut source: &mut (dyn io::Read + Send + Sync),
    ) -> sop::Result<Self> {
        let keys = Tsk::load(&mut source).expect("FIXME");

        Ok(Keys { keys })
    }
}

impl sop::Save for Keys {
    fn to_writer(
        &self,
        armored: bool,
        sink: &mut (dyn io::Write + Send + Sync),
    ) -> sop::Result<()> {
        Tsk::save(&self.keys, armored, sink).expect("FIXME");

        Ok(())
    }
}

impl sop::Load<'_, RPGSOP> for Sigs {
    fn from_reader(
        _sop: &'_ RPGSOP,
        mut source: &mut (dyn io::Read + Send + Sync),
    ) -> sop::Result<Self> {
        let sigs = rpgpie::sig::load(&mut source).expect("FIXME");

        Ok(Sigs { sigs })
    }
}

impl sop::Save for Sigs {
    fn to_writer(
        &self,
        armored: bool,
        mut sink: &mut (dyn io::Write + Send + Sync),
    ) -> sop::Result<()> {
        rpgpie::sig::save(&self.sigs, armored, &mut sink).expect("FIXME");

        Ok(())
    }
}

impl<'s> sop::plumbing::SopRef<'s, RPGSOP> for Certs {
    fn sop(&self) -> &'s RPGSOP {
        &SOP
    }
}

impl<'s> sop::plumbing::SopRef<'s, RPGSOP> for Keys {
    fn sop(&self) -> &'s RPGSOP {
        &SOP
    }
}

impl<'s> sop::plumbing::SopRef<'s, RPGSOP> for Sigs {
    fn sop(&self) -> &'s RPGSOP {
        &SOP
    }
}