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
use std::time;
use std::convert::TryInto;
use std::convert::TryFrom;

use crate::{
    Cert,
    cert::KeyBinding,
    packet::key,
    packet::Key,
    packet::Signature,
    Result,
    RevocationStatus,
};

/// A variant of `KeyAmalgamation` for primary keys.
#[derive(Debug)]
struct PrimaryKeyAmalgamation<'a, P: key::KeyParts> {
    cert: &'a Cert,
    binding: &'a KeyBinding<P, key::PrimaryRole>,
}

/// A variant of `KeyAmalgamation` for subkeys.
#[derive(Debug)]
struct SubordinateKeyAmalgamation<'a, P: key::KeyParts> {
    cert: &'a Cert,
    binding: &'a KeyBinding<P, key::SubordinateRole>,
}

/// The underlying `KeyAmalgamation` type.
///
/// We don't make this type public, because an enum's variant types
/// must also all be public, and we don't want that here.  Wrapping
/// this in a struct means that we can hide that.
#[derive(Debug)]
enum KeyAmalgamation0<'a, P: key::KeyParts> {
    Primary(PrimaryKeyAmalgamation<'a, P>),
    Subordinate(SubordinateKeyAmalgamation<'a, P>),
}

/// A `Key` and its associated data.
#[derive(Debug)]
pub struct KeyAmalgamation<'a, P: key::KeyParts>(KeyAmalgamation0<'a, P>);

impl<'a, P> From<(&'a Cert, &'a KeyBinding<P, key::PrimaryRole>)>
    for KeyAmalgamation<'a, P>
    where P: key::KeyParts
{
    fn from(x: (&'a Cert, &'a KeyBinding<P, key::PrimaryRole>)) -> Self {
        KeyAmalgamation(KeyAmalgamation0::Primary(PrimaryKeyAmalgamation {
            cert: x.0,
            binding: x.1,
        }))
    }
}

impl<'a, P> From<(&'a Cert, &'a KeyBinding<P, key::SubordinateRole>)>
    for KeyAmalgamation<'a, P>
    where P: key::KeyParts
{
    fn from(x: (&'a Cert, &'a KeyBinding<P, key::SubordinateRole>)) -> Self {
        KeyAmalgamation(KeyAmalgamation0::Subordinate(SubordinateKeyAmalgamation {
            cert: x.0,
            binding: x.1,
        }))
    }
}

// We can't make the key parts generic, because then the impl would
// conflict with 'impl<T> std::convert::From<T> for T'.
impl<'a> From<KeyAmalgamation<'a, key::PublicParts>>
    for KeyAmalgamation<'a, key::UnspecifiedParts>
{
    fn from(ka: KeyAmalgamation<'a, key::PublicParts>) -> Self {
        match ka {
            KeyAmalgamation(KeyAmalgamation0::Primary(ka)) => {
                KeyAmalgamation(KeyAmalgamation0::Primary(
                    PrimaryKeyAmalgamation {
                        cert: ka.cert,
                        binding: ka.binding.into(),
                    })
                )
            }
            KeyAmalgamation(KeyAmalgamation0::Subordinate(ka)) => {
                KeyAmalgamation(KeyAmalgamation0::Subordinate(
                    SubordinateKeyAmalgamation {
                        cert: ka.cert,
                        binding: ka.binding.into(),
                    })
                )
            }
        }
    }
}

impl<'a> From<KeyAmalgamation<'a, key::SecretParts>>
    for KeyAmalgamation<'a, key::PublicParts>
{
    fn from(ka: KeyAmalgamation<'a, key::SecretParts>) -> Self {
        match ka {
            KeyAmalgamation(KeyAmalgamation0::Primary(ka)) => {
                KeyAmalgamation(KeyAmalgamation0::Primary(
                    PrimaryKeyAmalgamation {
                        cert: ka.cert,
                        binding: ka.binding.into(),
                    })
                )
            }
            KeyAmalgamation(KeyAmalgamation0::Subordinate(ka)) => {
                KeyAmalgamation(KeyAmalgamation0::Subordinate(
                    SubordinateKeyAmalgamation {
                        cert: ka.cert,
                        binding: ka.binding.into(),
                    })
                )
            }
        }
    }
}

impl<'a> TryFrom<KeyAmalgamation<'a, key::PublicParts>>
    for KeyAmalgamation<'a, key::SecretParts>
{
    type Error = failure::Error;

    fn try_from(ka: KeyAmalgamation<'a, key::PublicParts>) -> Result<Self> {
        Ok(match ka {
            KeyAmalgamation(KeyAmalgamation0::Primary(ka)) => {
                KeyAmalgamation(KeyAmalgamation0::Primary(
                    PrimaryKeyAmalgamation {
                        cert: ka.cert,
                        binding: ka.binding.try_into()?,
                    })
                )
            }
            KeyAmalgamation(KeyAmalgamation0::Subordinate(ka)) => {
                KeyAmalgamation(KeyAmalgamation0::Subordinate(
                    SubordinateKeyAmalgamation {
                        cert: ka.cert,
                        binding: ka.binding.try_into()?,
                    })
                )
            }
        })
    }
}

impl<'a, P: 'a + key::KeyParts> KeyAmalgamation<'a, P> {
    /// Returns the key.
    pub fn key(&self) -> &'a Key<P, key::UnspecifiedRole>
        where &'a Key<P, key::UnspecifiedRole>: From<&'a key::PublicKey>
    {
        match self {
            KeyAmalgamation(KeyAmalgamation0::Primary(ref h)) =>
                h.cert.primary.key().into(),
            KeyAmalgamation(KeyAmalgamation0::Subordinate(ref h)) =>
                h.binding.key().into(),
        }
    }

    /// Returns the key's binding signature at time `t`, if any.
    pub fn binding_signature<T>(&self, t: T) -> Option<&'a Signature>
        where T: Into<Option<time::SystemTime>>
    {
        match self {
            KeyAmalgamation(KeyAmalgamation0::Primary(ref h)) =>
                h.cert.primary_key_signature(t),
            KeyAmalgamation(KeyAmalgamation0::Subordinate(ref h)) =>
                h.binding.binding_signature(t),
        }
    }

    /// Returns the key's revocation status at time `t`.
    pub fn revoked<T>(&self, t: T) -> RevocationStatus<'a>
        where T: Into<Option<time::SystemTime>>
    {
        match self {
            KeyAmalgamation(KeyAmalgamation0::Primary(ref h)) =>
                h.cert.revoked(t),
            KeyAmalgamation(KeyAmalgamation0::Subordinate(ref h)) =>
                h.binding.revoked(t),
        }
    }
}