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::{DecodeWithMemTracking, U256};
33use subspace_runtime_primitives::Moment;
34
35/// Signature verification request.
36#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
37pub struct SignatureVerificationRequest {
38    /// Der encoded public key info.
39    pub public_key_info: DerVec,
40    /// Der encoded signature algorithm.
41    pub signature_algorithm: DerVec,
42    /// Data that is being signed.
43    pub data: Vec<u8>,
44    /// Signature.
45    pub signature: Vec<u8>,
46}
47
48/// Validity of a given certificate.
49#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
50pub struct Validity {
51    /// Not valid before the time since UNIX_EPOCH
52    pub not_before: Moment,
53    /// Not valid after the time since UNIX_EPOCH
54    pub not_after: Moment,
55}
56
57impl Validity {
58    /// Checks if the certificate is valid at this time.
59    pub fn is_valid_at(&self, time: Moment) -> bool {
60        time >= self.not_before && time <= self.not_after
61    }
62}
63
64/// Validity conversion error.
65#[cfg(feature = "std")]
66#[derive(TypeInfo, Encode, Decode, Debug, PartialEq)]
67pub enum ValidityError {
68    /// Overflow during conversion to `Validity`.
69    Overflow,
70}
71
72#[cfg(feature = "std")]
73impl TryFrom<x509_parser::prelude::Validity> for Validity {
74    type Error = ValidityError;
75
76    fn try_from(value: x509_parser::certificate::Validity) -> Result<Self, Self::Error> {
77        Ok(Validity {
78            not_before: (value.not_before.timestamp() as u64)
79                .checked_mul(1000)
80                .and_then(|secs| {
81                    secs.checked_add(value.not_before.to_datetime().millisecond() as u64)
82                })
83                .ok_or(Self::Error::Overflow)?,
84            not_after: (value.not_after.timestamp() as u64)
85                .checked_mul(1000)
86                .and_then(|secs| {
87                    secs.checked_add(value.not_after.to_datetime().millisecond() as u64)
88                })
89                .ok_or(Self::Error::Overflow)?,
90        })
91    }
92}
93
94/// Decoded Tbs certificate.
95#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
96pub struct TbsCertificate {
97    /// Certificate serial number.
98    pub serial: U256,
99    /// Certificate subject common name.
100    pub subject_common_name: Vec<u8>,
101    /// Certificate subject public key info, der encoded.
102    pub subject_public_key_info: DerVec,
103    /// Certificate validity.
104    pub validity: Validity,
105}
106
107/// DER encoded bytes
108#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone, DecodeWithMemTracking)]
109pub struct DerVec(pub Vec<u8>);
110
111impl AsRef<[u8]> for DerVec {
112    fn as_ref(&self) -> &[u8] {
113        &self.0
114    }
115}
116
117impl From<Vec<u8>> for DerVec {
118    fn from(value: Vec<u8>) -> Self {
119        Self(value)
120    }
121}