sp_auto_id/
lib.rs

1// Copyright (C) 2021 Subspace Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Primitives for X509 certificate verification
17
18#![cfg_attr(not(feature = "std"), no_std)]
19
20#[cfg(feature = "std")]
21pub mod host_functions;
22mod runtime_interface;
23
24#[cfg(not(feature = "std"))]
25extern crate alloc;
26
27pub use crate::runtime_interface::auto_id_runtime_interface;
28#[cfg(not(feature = "std"))]
29use alloc::vec::Vec;
30use parity_scale_codec::{Decode, Encode};
31use scale_info::TypeInfo;
32use sp_core::U256;
33use sp_runtime_interface::pass_by;
34use sp_runtime_interface::pass_by::PassBy;
35use subspace_runtime_primitives::Moment;
36
37/// Signature verification request.
38#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
39pub struct SignatureVerificationRequest {
40    /// Der encoded public key info.
41    pub public_key_info: DerVec,
42    /// Der encoded signature algorithm.
43    pub signature_algorithm: DerVec,
44    /// Data that is being signed.
45    pub data: Vec<u8>,
46    /// Signature.
47    pub signature: Vec<u8>,
48}
49
50impl PassBy for SignatureVerificationRequest {
51    type PassBy = pass_by::Codec<Self>;
52}
53
54/// Validity of a given certificate.
55#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
56pub struct Validity {
57    /// Not valid before the time since UNIX_EPOCH
58    pub not_before: Moment,
59    /// Not valid after the time since UNIX_EPOCH
60    pub not_after: Moment,
61}
62
63impl Validity {
64    /// Checks if the certificate is valid at this time.
65    pub fn is_valid_at(&self, time: Moment) -> bool {
66        time >= self.not_before && time <= self.not_after
67    }
68}
69
70/// Validity conversion error.
71#[cfg(feature = "std")]
72#[derive(TypeInfo, Encode, Decode, Debug, PartialEq)]
73pub enum ValidityError {
74    /// Overflow during conversion to `Validity`.
75    Overflow,
76}
77
78#[cfg(feature = "std")]
79impl TryFrom<x509_parser::prelude::Validity> for Validity {
80    type Error = ValidityError;
81
82    fn try_from(value: x509_parser::certificate::Validity) -> Result<Self, Self::Error> {
83        Ok(Validity {
84            not_before: (value.not_before.timestamp() as u64)
85                .checked_mul(1000)
86                .and_then(|secs| {
87                    secs.checked_add(value.not_before.to_datetime().millisecond() as u64)
88                })
89                .ok_or(Self::Error::Overflow)?,
90            not_after: (value.not_after.timestamp() as u64)
91                .checked_mul(1000)
92                .and_then(|secs| {
93                    secs.checked_add(value.not_after.to_datetime().millisecond() as u64)
94                })
95                .ok_or(Self::Error::Overflow)?,
96        })
97    }
98}
99
100/// Decoded Tbs certificate.
101#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
102pub struct TbsCertificate {
103    /// Certificate serial number.
104    pub serial: U256,
105    /// Certificate subject common name.
106    pub subject_common_name: Vec<u8>,
107    /// Certificate subject public key info, der encoded.
108    pub subject_public_key_info: DerVec,
109    /// Certificate validity.
110    pub validity: Validity,
111}
112
113/// DER encoded bytes
114#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
115pub struct DerVec(pub Vec<u8>);
116
117impl PassBy for DerVec {
118    type PassBy = pass_by::Codec<Self>;
119}
120
121impl AsRef<[u8]> for DerVec {
122    fn as_ref(&self) -> &[u8] {
123        &self.0
124    }
125}
126
127impl From<Vec<u8>> for DerVec {
128    fn from(value: Vec<u8>) -> Self {
129        Self(value)
130    }
131}