deno.land / x / deno@v1.28.2 / ext / crypto / shared.rs

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
use std::borrow::Cow;
use deno_core::error::custom_error;use deno_core::error::type_error;use deno_core::error::AnyError;use deno_core::ZeroCopyBuf;use elliptic_curve::sec1::ToEncodedPoint;use rsa::pkcs1::DecodeRsaPrivateKey;use rsa::pkcs1::EncodeRsaPublicKey;use rsa::pkcs8::DecodePrivateKey;use rsa::RsaPrivateKey;use serde::Deserialize;use serde::Serialize;
pub const RSA_ENCRYPTION_OID: const_oid::ObjectIdentifier = const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1");
pub const ID_SECP256R1_OID: const_oid::ObjectIdentifier = const_oid::ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7");pub const ID_SECP384R1_OID: const_oid::ObjectIdentifier = const_oid::ObjectIdentifier::new_unwrap("1.3.132.0.34");pub const ID_SECP521R1_OID: const_oid::ObjectIdentifier = const_oid::ObjectIdentifier::new_unwrap("1.3.132.0.35");
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]pub enum ShaHash { #[serde(rename = "SHA-1")] Sha1, #[serde(rename = "SHA-256")] Sha256, #[serde(rename = "SHA-384")] Sha384, #[serde(rename = "SHA-512")] Sha512,}
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]pub enum EcNamedCurve { #[serde(rename = "P-256")] P256, #[serde(rename = "P-384")] P384, #[serde(rename = "P-521")] P521,}
#[derive(Serialize, Deserialize)]#[serde(rename_all = "lowercase", tag = "type", content = "data")]pub enum RawKeyData { Secret(ZeroCopyBuf), Private(ZeroCopyBuf), Public(ZeroCopyBuf),}
impl RawKeyData { pub fn as_rsa_public_key(&self) -> Result<Cow<'_, [u8]>, AnyError> { match self { RawKeyData::Public(data) => Ok(Cow::Borrowed(data)), RawKeyData::Private(data) => { let private_key = RsaPrivateKey::from_pkcs1_der(data) .map_err(|_| type_error("expected valid private key"))?;
let public_key_doc = private_key .to_public_key() .to_pkcs1_der() .map_err(|_| type_error("expected valid public key"))?;
Ok(Cow::Owned(public_key_doc.as_bytes().into())) } _ => Err(type_error("expected public key")), } }
pub fn as_rsa_private_key(&self) -> Result<&[u8], AnyError> { match self { RawKeyData::Private(data) => Ok(data), _ => Err(type_error("expected private key")), } }
pub fn as_secret_key(&self) -> Result<&[u8], AnyError> { match self { RawKeyData::Secret(data) => Ok(data), _ => Err(type_error("expected secret key")), } }
pub fn as_ec_public_key_p256(&self) -> Result<p256::EncodedPoint, AnyError> { match self { RawKeyData::Public(data) => { // public_key is a serialized EncodedPoint p256::EncodedPoint::from_bytes(data) .map_err(|_| type_error("expected valid public EC key")) } RawKeyData::Private(data) => { let signing_key = p256::SecretKey::from_pkcs8_der(data) .map_err(|_| type_error("expected valid private EC key"))?; Ok(signing_key.public_key().to_encoded_point(false)) } // Should never reach here. RawKeyData::Secret(_) => unreachable!(), } }
pub fn as_ec_public_key_p384(&self) -> Result<p384::EncodedPoint, AnyError> { match self { RawKeyData::Public(data) => { // public_key is a serialized EncodedPoint p384::EncodedPoint::from_bytes(data) .map_err(|_| type_error("expected valid public EC key")) } RawKeyData::Private(data) => { let signing_key = p384::SecretKey::from_pkcs8_der(data) .map_err(|_| type_error("expected valid private EC key"))?; Ok(signing_key.public_key().to_encoded_point(false)) } // Should never reach here. RawKeyData::Secret(_) => unreachable!(), } }
pub fn as_ec_private_key(&self) -> Result<&[u8], AnyError> { match self { RawKeyData::Private(data) => Ok(data), _ => Err(type_error("expected private key")), } }}
pub fn data_error(msg: impl Into<Cow<'static, str>>) -> AnyError { custom_error("DOMExceptionDataError", msg)}
pub fn not_supported_error(msg: impl Into<Cow<'static, str>>) -> AnyError { custom_error("DOMExceptionNotSupportedError", msg)}
pub fn operation_error(msg: impl Into<Cow<'static, str>>) -> AnyError { custom_error("DOMExceptionOperationError", msg)}
pub fn unsupported_format() -> AnyError { not_supported_error("unsupported format")}
deno

Version Info

Tagged at
a year ago