Skip to content

Commit

Permalink
elliptic-curve: PKCS#8 PEM support
Browse files Browse the repository at this point in the history
Support for decoding PEM-encoded PKCS#8 `SecretKey`s
  • Loading branch information
tarcieri committed Dec 4, 2020
1 parent 9fc65e9 commit 2ef64c6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/elliptic-curve.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
- run: cargo build --no-default-features --release --target ${{ matrix.target }}
- run: cargo build --no-default-features --release --target ${{ matrix.target }} --features arithmetic
- run: cargo build --no-default-features --release --target ${{ matrix.target }} --features ecdh
- run: cargo build --no-default-features --release --target ${{ matrix.target }} --features pem
- run: cargo build --no-default-features --release --target ${{ matrix.target }} --features pkcs8
- run: cargo build --no-default-features --release --target ${{ matrix.target }} --features zeroize
test:
runs-on: ubuntu-latest
Expand Down
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ default = ["arithmetic"]
alloc = []
arithmetic = ["bitvec", "ff", "group"]
ecdh = ["arithmetic", "zeroize"]
pem = ["pkcs8/pem"]
std = ["alloc"]

[package.metadata.docs.rs]
Expand Down
17 changes: 17 additions & 0 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ where
}

/// Deserialize PKCS#8-encoded private key from ASN.1 DER
/// (binary format).
#[cfg(feature = "pkcs8")]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
pub fn from_pkcs8_der(bytes: &[u8]) -> Result<Self, Error>
Expand All @@ -132,6 +133,22 @@ where
Self::from_pkcs8_private_key(private_key_info.private_key)
}

/// Deserialize PKCS#8-encoded private key from PEM.
///
/// Keys in this format begin with the following delimiter:
///
/// ```text
/// -----BEGIN PRIVATE KEY-----
/// ```
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn from_pkcs8_pem(s: &str) -> Result<Self, Error>
where
C: AlgorithmParameters,
{
Self::from_pkcs8_der(pkcs8::Document::from_pem(s)?.as_ref())
}

/// Parse the `private_key` field of a PKCS#8-encoded private key's
/// `PrivateKeyInfo`.
#[cfg(feature = "pkcs8")]
Expand Down

0 comments on commit 2ef64c6

Please sign in to comment.