Skip to content

Commit 4dc000b

Browse files
authored
pem-rfc7468 v0.2.1 (#23)
1 parent b1c3c0f commit 4dc000b

5 files changed

Lines changed: 49 additions & 36 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pem-rfc7468/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 0.2.1 (2021-09-14)
8+
### Added
9+
- `decode_label` ([#22])
10+
- `Error::HeaderDisallowed` ([#13], [#19], [#21])
11+
12+
### Changed
13+
- Moved to `formats` repo ([#2])
14+
15+
[#2]: https://github.com/RustCrypto/formats/pull/2
16+
[#13]: https://github.com/RustCrypto/formats/pull/13
17+
[#19]: https://github.com/RustCrypto/formats/pull/19
18+
[#21]: https://github.com/RustCrypto/formats/pull/21
19+
[#22]: https://github.com/RustCrypto/formats/pull/22
20+
721
## 0.2.0 (2021-07-26)
822
### Added
923
- Support for customizing PEM line endings

pem-rfc7468/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pem-rfc7468"
3-
version = "0.2.0" # Also update html_root_url in lib.rs when bumping this
3+
version = "0.2.1" # Also update html_root_url in lib.rs when bumping this
44
description = """
55
PEM Encoding (RFC 7468) for PKIX, PKCS, and CMS Structures, implementing a
66
strict subset of the original Privacy-Enhanced Mail encoding intended

pem-rfc7468/src/decoder.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,6 @@ use crate::{
2020
use base64ct::{Base64, Encoding};
2121
use core::{convert::TryFrom, str};
2222

23-
fn decode_encapsulated_text<'i, 'o>(
24-
encapsulation: &Encapsulation<'i>,
25-
buf: &'o mut [u8],
26-
out_len: &mut usize,
27-
) -> Result<()> {
28-
for line in encapsulation.encapsulated_text() {
29-
let line = line?;
30-
31-
match Base64::decode(line, &mut buf[*out_len..]) {
32-
Err(error) => {
33-
// in the case that we are decoding the first line
34-
// and we error, then attribute the error to an unsupported header
35-
// if a colon char is present in the line
36-
if *out_len == 0 && line.iter().any(|&b| b == grammar::CHAR_COLON) {
37-
return Err(Error::HeaderDisallowed);
38-
} else {
39-
return Err(error.into());
40-
}
41-
}
42-
Ok(out) => *out_len += out.len(),
43-
}
44-
}
45-
Ok(())
46-
}
47-
/// Decode the encapsulation boundaries of a PEM document according to RFC 7468's "Strict" grammar.
48-
///
49-
/// On success, returning the decoded label.
50-
pub fn decode_label(pem: &[u8]) -> Result<&str> {
51-
Ok(Encapsulation::try_from(pem)?.label())
52-
}
53-
5423
/// Decode a PEM document according to RFC 7468's "Strict" grammar.
5524
///
5625
/// On success, writes the decoded document into the provided buffer, returning
@@ -60,9 +29,7 @@ pub fn decode<'i, 'o>(pem: &'i [u8], buf: &'o mut [u8]) -> Result<(&'i str, &'o
6029
let encapsulation = Encapsulation::try_from(pem)?;
6130
let label = encapsulation.label();
6231
let mut out_len = 0;
63-
6432
decode_encapsulated_text(&encapsulation, buf, &mut out_len)?;
65-
6633
Ok((label, &buf[..out_len]))
6734
}
6835

@@ -86,6 +53,38 @@ pub fn decode_vec(pem: &[u8]) -> Result<(&str, Vec<u8>)> {
8653
Ok((label, result))
8754
}
8855

56+
/// Decode the encapsulation boundaries of a PEM document according to RFC 7468's "Strict" grammar.
57+
///
58+
/// On success, returning the decoded label.
59+
pub fn decode_label(pem: &[u8]) -> Result<&str> {
60+
Ok(Encapsulation::try_from(pem)?.label())
61+
}
62+
63+
fn decode_encapsulated_text<'i, 'o>(
64+
encapsulation: &Encapsulation<'i>,
65+
buf: &'o mut [u8],
66+
out_len: &mut usize,
67+
) -> Result<()> {
68+
for line in encapsulation.encapsulated_text() {
69+
let line = line?;
70+
71+
match Base64::decode(line, &mut buf[*out_len..]) {
72+
Err(error) => {
73+
// in the case that we are decoding the first line
74+
// and we error, then attribute the error to an unsupported header
75+
// if a colon char is present in the line
76+
if *out_len == 0 && line.iter().any(|&b| b == grammar::CHAR_COLON) {
77+
return Err(Error::HeaderDisallowed);
78+
} else {
79+
return Err(error.into());
80+
}
81+
}
82+
Ok(out) => *out_len += out.len(),
83+
}
84+
}
85+
Ok(())
86+
}
87+
8988
/// PEM encapsulation parser.
9089
///
9190
/// This parser performs an initial pass over the data, locating the

pem-rfc7468/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
#![doc(
9696
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
9797
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
98-
html_root_url = "https://docs.rs/pem-rfc7468/0.1.1"
98+
html_root_url = "https://docs.rs/pem-rfc7468/0.2.1"
9999
)]
100100
#![forbid(unsafe_code, clippy::unwrap_used)]
101101
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]

0 commit comments

Comments
 (0)