Skip to content

Commit 0464d49

Browse files
committed
error: replace num_enum with minimal macro rule.
The `num_enum` dependency brings with it a large number of transitive dependencies. Some of those transitive deps now have an aggressive MSRV of 1.64+. The existing usage of `num_enum` is very minimal: just one derived trait for the `rustls_result` enum to provide it with a `From` impl for u32 primitive values. This commit replaces the `num_enum` crate with a small `u32_enum_builder!` macro rule loosely based on the Rustls `enum_builder` macro. Since our use case is narrower, I've simplified the macro and tailored it to the rustls-ffi use-case. This approach adds one complication related to `cbindgen`: it now has to be instructed to expand the `rustls-ffi` crate before generating bindings in order to find the `rustls_result` enum. Doing this requires tweaking the `cbindgen.toml` to add a `parse.expand` configuration setting. Notably this also means `cbindgen` now has to be run with a nightly `rustc`. Since this is a developer only workflow it shouldn't be too onerous a requirement. We're now happily building with Rust 1.60 again and can also breathe easy knowing we have a slimmer dependency profile!
1 parent 7aa7453 commit 0464d49

File tree

4 files changed

+178
-132
lines changed

4 files changed

+178
-132
lines changed

.github/workflows/test.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ jobs:
7777
- uses: actions/checkout@v2
7878
with:
7979
persist-credentials: false
80+
- name: Install nightly rust toolchain
81+
uses: actions-rs/toolchain@v1
82+
with:
83+
toolchain: nightly
84+
override: true
85+
default: true
8086
- name: Configure CMake
8187
run: cmake -S . -B build
8288
- name: Build, debug configuration
@@ -93,6 +99,12 @@ jobs:
9399
- uses: actions/checkout@v2
94100
with:
95101
persist-credentials: false
102+
- name: Install nightly rust toolchain
103+
uses: actions-rs/toolchain@v1
104+
with:
105+
toolchain: nightly
106+
override: true
107+
default: true
96108
- name: Configure CMake
97109
run: cmake -S . -B build
98110
- name: Build, release configuration
@@ -106,6 +118,12 @@ jobs:
106118
- uses: actions/checkout@v2
107119
with:
108120
persist-credentials: false
121+
- name: Install nightly rust toolchain
122+
uses: actions-rs/toolchain@v1
123+
with:
124+
toolchain: nightly
125+
override: true
126+
default: true
109127
- run: touch src/lib.rs
110128
- run: cbindgen --version
111129
- run: make src/rustls.h

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ libc = "0.2"
2929
sct = "0.7"
3030
rustls-pemfile = "0.2.1"
3131
log = "0.4.17"
32-
num_enum = "0.5.10"
3332

3433
[lib]
3534
name = "rustls_ffi"

cbindgen.toml

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ include = ["rustls_tls_version"]
1111

1212
[defines]
1313
"feature = read_buf" = "DEFINE_READ_BUF"
14+
15+
[parse.expand]
16+
crates = ["rustls-ffi"]
17+
features = ["read_buf"]

src/error.rs

+156-131
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::sync::Arc;
55

66
use crate::ffi_panic_boundary;
77
use libc::{c_char, c_uint, size_t};
8-
use num_enum::TryFromPrimitive;
98
use rustls::{CertificateError, Error, InvalidMessage};
109

1110
/// A return value for a function that may return either success (0) or a
@@ -16,6 +15,162 @@ use rustls::{CertificateError, Error, InvalidMessage};
1615
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1716
pub struct rustls_io_result(pub libc::c_int);
1817

18+
macro_rules! u32_enum_builder {
19+
(
20+
$(#[$comment:meta])*
21+
EnumName: $enum_name: ident;
22+
EnumDefault: $enum_default: ident;
23+
EnumVal { $( $enum_var: ident => $enum_val: expr ),* }
24+
) => {
25+
$(#[$comment])*
26+
#[allow(dead_code)]
27+
#[repr(u32)]
28+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29+
pub enum $enum_name {
30+
$( $enum_var = $enum_val),*
31+
}
32+
impl From<u32> for $enum_name {
33+
fn from(x: u32) -> Self {
34+
match x {
35+
$($enum_val => $enum_name::$enum_var),*
36+
, _ => $enum_name::$enum_default,
37+
}
38+
}
39+
}
40+
};
41+
}
42+
43+
u32_enum_builder! {
44+
EnumName: rustls_result;
45+
EnumDefault: InvalidParameter;
46+
EnumVal{
47+
Ok => 7000,
48+
Io => 7001,
49+
NullParameter => 7002,
50+
InvalidDnsNameError => 7003,
51+
Panic => 7004,
52+
CertificateParseError => 7005,
53+
PrivateKeyParseError => 7006,
54+
InsufficientSize => 7007,
55+
NotFound => 7008,
56+
InvalidParameter => 7009,
57+
UnexpectedEof => 7010,
58+
PlaintextEmpty => 7011,
59+
AcceptorNotReady => 7012,
60+
AlreadyUsed => 7013,
61+
62+
// From https://docs.rs/rustls/latest/rustls/enum.Error.html
63+
NoCertificatesPresented => 7101,
64+
DecryptError => 7102,
65+
FailedToGetCurrentTime => 7103,
66+
FailedToGetRandomBytes => 7113,
67+
HandshakeNotComplete => 7104,
68+
PeerSentOversizedRecord => 7105,
69+
NoApplicationProtocol => 7106,
70+
BadMaxFragmentSize => 7114,
71+
UnsupportedNameType => 7115,
72+
EncryptError => 7116,
73+
74+
// Reserved from previous use pre rustls-ffi <0.21.0
75+
// CorruptMessage => 7100,
76+
// CorruptMessagePayload => 7111,
77+
// CertInvalidEncoding => 7117,
78+
// CertInvalidSignatureType => 7118,
79+
// CertInvalidSignature => 7119,
80+
// CertInvalidData => 7120,
81+
82+
// From InvalidCertificate, with fields that get flattened.
83+
// https://docs.rs/rustls/0.21.0/rustls/enum.Error.html#variant.InvalidCertificate
84+
CertEncodingBad => 7121,
85+
CertExpired => 7122,
86+
CertNotYetValid => 7123,
87+
CertRevoked => 7124,
88+
CertUnhandledCriticalExtension => 7125,
89+
CertUnknownIssuer => 7126,
90+
CertBadSignature => 7127,
91+
CertNotValidForName => 7128,
92+
CertInvalidPurpose => 7129,
93+
CertApplicationVerificationFailure => 7130,
94+
CertOtherError => 7131,
95+
96+
// From InvalidMessage, with fields that get flattened.
97+
// https://docs.rs/rustls/0.21.0/rustls/enum.Error.html#variant.InvalidMessage
98+
MessageHandshakePayloadTooLarge => 7133,
99+
MessageInvalidCcs => 7134,
100+
MessageInvalidContentType => 7135,
101+
MessageInvalidCertStatusType => 7136,
102+
MessageInvalidCertRequest => 7137,
103+
MessageInvalidDhParams => 7138,
104+
MessageInvalidEmptyPayload => 7139,
105+
MessageInvalidKeyUpdate => 7140,
106+
MessageInvalidServerName => 7141,
107+
MessageTooLarge => 7142,
108+
MessageTooShort => 7143,
109+
MessageMissingData => 7144,
110+
MessageMissingKeyExchange => 7145,
111+
MessageNoSignatureSchemes => 7146,
112+
MessageTrailingData => 7147,
113+
MessageUnexpectedMessage => 7148,
114+
MessageUnknownProtocolVersion => 7149,
115+
MessageUnsupportedCompression => 7150,
116+
MessageUnsupportedCurveType => 7151,
117+
MessageUnsupportedKeyExchangeAlgorithm => 7152,
118+
MessageInvalidOther => 7153, // Last added.
119+
120+
// From Error, with fields that get dropped.
121+
PeerIncompatibleError => 7107,
122+
PeerMisbehavedError => 7108,
123+
InappropriateMessage => 7109,
124+
InappropriateHandshakeMessage => 7110,
125+
General => 7112,
126+
127+
// From Error, with fields that get flattened.
128+
// https://docs.rs/rustls/latest/rustls/internal/msgs/enums/enum.AlertDescription.html
129+
AlertCloseNotify => 7200,
130+
AlertUnexpectedMessage => 7201,
131+
AlertBadRecordMac => 7202,
132+
AlertDecryptionFailed => 7203,
133+
AlertRecordOverflow => 7204,
134+
AlertDecompressionFailure => 7205,
135+
AlertHandshakeFailure => 7206,
136+
AlertNoCertificate => 7207,
137+
AlertBadCertificate => 7208,
138+
AlertUnsupportedCertificate => 7209,
139+
AlertCertificateRevoked => 7210,
140+
AlertCertificateExpired => 7211,
141+
AlertCertificateUnknown => 7212,
142+
AlertIllegalParameter => 7213,
143+
AlertUnknownCA => 7214,
144+
AlertAccessDenied => 7215,
145+
AlertDecodeError => 7216,
146+
AlertDecryptError => 7217,
147+
AlertExportRestriction => 7218,
148+
AlertProtocolVersion => 7219,
149+
AlertInsufficientSecurity => 7220,
150+
AlertInternalError => 7221,
151+
AlertInappropriateFallback => 7222,
152+
AlertUserCanceled => 7223,
153+
AlertNoRenegotiation => 7224,
154+
AlertMissingExtension => 7225,
155+
AlertUnsupportedExtension => 7226,
156+
AlertCertificateUnobtainable => 7227,
157+
AlertUnrecognisedName => 7228,
158+
AlertBadCertificateStatusResponse => 7229,
159+
AlertBadCertificateHashValue => 7230,
160+
AlertUnknownPSKIdentity => 7231,
161+
AlertCertificateRequired => 7232,
162+
AlertNoApplicationProtocol => 7233,
163+
AlertUnknown => 7234,
164+
165+
// https://docs.rs/sct/latest/sct/enum.Error.html
166+
CertSCTMalformed => 7319,
167+
CertSCTInvalidSignature => 7320,
168+
CertSCTTimestampInFuture => 7321,
169+
CertSCTUnsupportedVersion => 7322,
170+
CertSCTUnknownLog => 7323
171+
}
172+
}
173+
19174
impl rustls_result {
20175
/// After a rustls function returns an error, you may call
21176
/// this to get a pointer to a buffer containing a detailed error
@@ -135,136 +290,6 @@ fn test_rustls_result_is_cert_error() {
135290
}
136291
}
137292

138-
#[allow(dead_code)]
139-
#[repr(u32)]
140-
#[derive(Debug, TryFromPrimitive, Clone, Copy, PartialEq, Eq)]
141-
pub enum rustls_result {
142-
Ok = 7000,
143-
Io = 7001,
144-
NullParameter = 7002,
145-
InvalidDnsNameError = 7003,
146-
Panic = 7004,
147-
CertificateParseError = 7005,
148-
PrivateKeyParseError = 7006,
149-
InsufficientSize = 7007,
150-
NotFound = 7008,
151-
InvalidParameter = 7009,
152-
UnexpectedEof = 7010,
153-
PlaintextEmpty = 7011,
154-
AcceptorNotReady = 7012,
155-
AlreadyUsed = 7013,
156-
157-
// From https://docs.rs/rustls/latest/rustls/enum.Error.html
158-
NoCertificatesPresented = 7101,
159-
DecryptError = 7102,
160-
FailedToGetCurrentTime = 7103,
161-
FailedToGetRandomBytes = 7113,
162-
HandshakeNotComplete = 7104,
163-
PeerSentOversizedRecord = 7105,
164-
NoApplicationProtocol = 7106,
165-
BadMaxFragmentSize = 7114,
166-
UnsupportedNameType = 7115,
167-
EncryptError = 7116,
168-
169-
// Reserved from previous use pre rustls-ffi <0.21.0
170-
// CorruptMessage = 7100,
171-
// CorruptMessagePayload = 7111,
172-
// CertInvalidEncoding = 7117,
173-
// CertInvalidSignatureType = 7118,
174-
// CertInvalidSignature = 7119,
175-
// CertInvalidData = 7120,
176-
177-
// From InvalidCertificate, with fields that get flattened.
178-
// https://docs.rs/rustls/0.21.0/rustls/enum.Error.html#variant.InvalidCertificate
179-
CertEncodingBad = 7121,
180-
CertExpired = 7122,
181-
CertNotYetValid = 7123,
182-
CertRevoked = 7124,
183-
CertUnhandledCriticalExtension = 7125,
184-
CertUnknownIssuer = 7126,
185-
CertBadSignature = 7127,
186-
CertNotValidForName = 7128,
187-
CertInvalidPurpose = 7129,
188-
CertApplicationVerificationFailure = 7130,
189-
CertOtherError = 7131,
190-
191-
// From InvalidMessage, with fields that get flattened.
192-
// https://docs.rs/rustls/0.21.0/rustls/enum.Error.html#variant.InvalidMessage
193-
MessageHandshakePayloadTooLarge = 7133,
194-
MessageInvalidCcs = 7134,
195-
MessageInvalidContentType = 7135,
196-
MessageInvalidCertStatusType = 7136,
197-
MessageInvalidCertRequest = 7137,
198-
MessageInvalidDhParams = 7138,
199-
MessageInvalidEmptyPayload = 7139,
200-
MessageInvalidKeyUpdate = 7140,
201-
MessageInvalidServerName = 7141,
202-
MessageTooLarge = 7142,
203-
MessageTooShort = 7143,
204-
MessageMissingData = 7144,
205-
MessageMissingKeyExchange = 7145,
206-
MessageNoSignatureSchemes = 7146,
207-
MessageTrailingData = 7147,
208-
MessageUnexpectedMessage = 7148,
209-
MessageUnknownProtocolVersion = 7149,
210-
MessageUnsupportedCompression = 7150,
211-
MessageUnsupportedCurveType = 7151,
212-
MessageUnsupportedKeyExchangeAlgorithm = 7152,
213-
MessageInvalidOther = 7153, // Last added.
214-
215-
// From Error, with fields that get dropped.
216-
PeerIncompatibleError = 7107,
217-
PeerMisbehavedError = 7108,
218-
InappropriateMessage = 7109,
219-
InappropriateHandshakeMessage = 7110,
220-
General = 7112,
221-
222-
// From Error, with fields that get flattened.
223-
// https://docs.rs/rustls/latest/rustls/internal/msgs/enums/enum.AlertDescription.html
224-
AlertCloseNotify = 7200,
225-
AlertUnexpectedMessage = 7201,
226-
AlertBadRecordMac = 7202,
227-
AlertDecryptionFailed = 7203,
228-
AlertRecordOverflow = 7204,
229-
AlertDecompressionFailure = 7205,
230-
AlertHandshakeFailure = 7206,
231-
AlertNoCertificate = 7207,
232-
AlertBadCertificate = 7208,
233-
AlertUnsupportedCertificate = 7209,
234-
AlertCertificateRevoked = 7210,
235-
AlertCertificateExpired = 7211,
236-
AlertCertificateUnknown = 7212,
237-
AlertIllegalParameter = 7213,
238-
AlertUnknownCA = 7214,
239-
AlertAccessDenied = 7215,
240-
AlertDecodeError = 7216,
241-
AlertDecryptError = 7217,
242-
AlertExportRestriction = 7218,
243-
AlertProtocolVersion = 7219,
244-
AlertInsufficientSecurity = 7220,
245-
AlertInternalError = 7221,
246-
AlertInappropriateFallback = 7222,
247-
AlertUserCanceled = 7223,
248-
AlertNoRenegotiation = 7224,
249-
AlertMissingExtension = 7225,
250-
AlertUnsupportedExtension = 7226,
251-
AlertCertificateUnobtainable = 7227,
252-
AlertUnrecognisedName = 7228,
253-
AlertBadCertificateStatusResponse = 7229,
254-
AlertBadCertificateHashValue = 7230,
255-
AlertUnknownPSKIdentity = 7231,
256-
AlertCertificateRequired = 7232,
257-
AlertNoApplicationProtocol = 7233,
258-
AlertUnknown = 7234,
259-
260-
// https://docs.rs/sct/latest/sct/enum.Error.html
261-
CertSCTMalformed = 7319,
262-
CertSCTInvalidSignature = 7320,
263-
CertSCTTimestampInFuture = 7321,
264-
CertSCTUnsupportedVersion = 7322,
265-
CertSCTUnknownLog = 7323,
266-
}
267-
268293
pub(crate) fn map_error(input: rustls::Error) -> rustls_result {
269294
use rustls::AlertDescription as alert;
270295
use rustls_result::*;

0 commit comments

Comments
 (0)