Skip to content

Conversation

@kingstjo
Copy link
Contributor

Issues:

Addresses CryptoAlg-3073
Addresses CryptoAlg-3074
Addresses CryptoAlg-3075

Description of changes:

This PR implements the PKCS8 CLI command wrapper for AWS-LC, providing OpenSSL-compatible command-line interface to the existing PKCS8 functionality. The CLI tool adds support for:

  1. Converting traditional format private keys to PKCS#8 format through a command-line interface
  2. CLI options for both encrypted and unencrypted PKCS#8 private keys
  3. Command-line access to PKCS#5 v2.0 encryption algorithms
  4. Handling PRF parameters through AWS-LC's existing implementation
  5. Supporting multiple key types via d2i_AutoPrivateKey wrapper
  6. DER and PEM format input/output options

Call-outs:

  1. PRF Parameter Compatibility: The implementation accepts the -v2prf parameter for OpenSSL command-line compatibility and validates supported values (hmacWithSHA1, hmacWithSHA256, hmacWithSHA512). However, AWS-LC's underlying PKCS#8 implementation always uses its default PRF implementation (-1) in the PKCS8_encrypt function, regardless of the specified -v2prf value.

  2. Comprehensive Key Type Support: The CLI implementation uses d2i_AutoPrivateKey to provide support for all private key types that AWS-LC can handle. This allows users to process various key formats through the pkcs8 command without type restrictions.

  3. User-Friendly Error Handling: The implementation provides context-rich error messages that guide users through troubleshooting common issues including:

    • Invalid key types or formats
    • Password/passphrase problems
    • Permission or disk space issues for output files
    • Unsupported encryption parameters

Testing:

The CLI wrapper is validated through two testing approaches:

  1. Unit tests (PKCS8Test class) verify:

    • Basic CLI options processing (PKCS8ToolBasicTest)
    • Format handling with -inform and -outform options (PKCS8ToolFormatTest)
    • Encryption with -v2 aes-256-cbc options (PKCS8ToolEncryptionTest)
    • PRF parameter handling via -v2prf (PKCS8ToolPRFTest)
    • Error handling for invalid inputs (PKCS8OptionUsageErrorsTest)
  2. Comparison tests (PKCS8ComparisonTest class) directly compare AWS-LC's output with OpenSSL's:

    • Unencrypted PKCS#8 conversion (PKCS8ToolCompareUnencryptedOpenSSL)
    • Encrypted PKCS#8 with AES-256-CBC (PKCS8ToolCompareEncryptedOpenSSL)
    • DER format output handling (PKCS8ToolCompareDERFormatOpenSSL)
    • PRF parameter handling (PKCS8ToolCompareV2prfOpenSSL)

These tests ensure that the AWS-LC CLI tool produces output files with the correct PKCS#8 boundaries and format compatibility with OpenSSL's pkcs8 command.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

Jonathon Kingston and others added 8 commits April 17, 2025 21:12
Add the pkcs8 command to the AWS-LC OpenSSL-compatible CLI tools.
This implementation includes support for the following options:
- [-in filename] [-out filename]
- [-inform format] [-outform format]
- [-topk8] [-nocrypt]
- [-v2 alg] [-v2prf alg]
- [-passin arg] [-passout arg]

The implementation ensures identical output to OpenSSL 1.1.1 and
includes appropriate test files. This functionality allows users
to convert between traditional key formats and PKCS#8 with
options for encryption, format control, and password handling.
This fix addresses three main issues in the PKCS8 CLI tool:
1) Properly handle PRF parameters with AWS-LC's implementation
2) Support all key types by using d2i_AutoPrivateKey instead of RSA-specific function
3) Add more descriptive error messages for better troubleshooting
@codecov-commenter
Copy link

codecov-commenter commented Apr 21, 2025

Codecov Report

Attention: Patch coverage is 43.63257% with 270 lines in your changes missing coverage. Please review.

Project coverage is 78.74%. Comparing base (4131f11) to head (66ea951).

Files with missing lines Patch % Lines
tool-openssl/pkcs8_test.cc 30.20% 171 Missing ⚠️
tool-openssl/pkcs8.cc 59.73% 91 Missing ⚠️
tool-openssl/test_util.h 0.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2342      +/-   ##
==========================================
- Coverage   78.88%   78.74%   -0.14%     
==========================================
  Files         640      642       +2     
  Lines      109766   110241     +475     
  Branches    15526    15580      +54     
==========================================
+ Hits        86590    86811     +221     
- Misses      22479    22732     +253     
- Partials      697      698       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kingstjo kingstjo force-pushed the implement-pkcs8-cli branch from 1d59317 to fe370f1 Compare April 22, 2025 20:53
@kingstjo kingstjo marked this pull request as ready for review April 23, 2025 17:07
@kingstjo kingstjo requested a review from a team as a code owner April 23, 2025 17:07
@kingstjo kingstjo force-pushed the implement-pkcs8-cli branch from ab578cf to fe370f1 Compare April 24, 2025 21:27
kingstjo and others added 6 commits April 24, 2025 15:06
- Updated the implementation to only accept hmacWithSHA1 as a valid PRF algorithm
- Added explicit error messages when users specify unsupported PRF algorithms
- Updated comments to clearly indicate that only hmacWithSHA1 is supported
- Updated tests to use hmacWithSHA1 and added test for unsupported PRF rejection
Added OpenSSLPointer template in internal.h to handle memory allocated with OPENSSL functions.
Updated pkcs8.cc to use smart pointers for better resource management and to eliminate manual cleanup.
This improves code safety, simplifies error paths, and aligns with patterns used in the rest of the codebase.
When -v2 option is specified without a value in OpenSSL 1.1.1, it defaults to using aes-256-cbc. This commit ensures AWS-LC maintains the same behavior for compatibility. Added tests to verify this behavior.
- Added cross-compatibility tests to verify AWS-LC and OpenSSL can decrypt each other's outputs
- Added tests for custom PRF algorithm and default cipher interoperability
- Moved helper functions to test_util.h for better code organization
- Enhanced test validation to verify decrypted keys match original keys
- Maintained backward compatibility with existing tests
@kingstjo kingstjo marked this pull request as draft April 25, 2025 17:56
@smittals2 smittals2 self-requested a review April 28, 2025 20:44
samuel40791765
samuel40791765 previously approved these changes May 16, 2025
Copy link
Contributor

@samuel40791765 samuel40791765 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome tests and the code is much more concise with only what's needed! There are still some examples of "over-documentation" and a few nits here and there. Preferably would like the comments and additional test debugging error messages cut down a bit before merging, but I don't see any major blockers.

…s8.cc

This commit replaces the goto error handling pattern with modern C++ RAII
techniques by introducing a SensitiveString class that automatically clears
sensitive data on destruction. The implementation:

1. Adds a SensitiveString wrapper class that securely handles sensitive data
2. Refactors pkcs8Tool to use early returns instead of goto statements
3. Removes the now-obsolete secure_clear function
4. Maintains all existing functionality while improving code clarity
kingstjo and others added 4 commits June 13, 2025 11:52
- Removed std::cout statements that duplicated information already provided by the test framework

- Eliminated content dumps that add visual noise without providing additional test value

- Removed redundant section comments and normalized whitespace

- Preserved all test logic and functionality while making the code cleaner

- Maintained descriptive assertion messages that explain failure scenarios
… implement-pkcs8-cli

Pulling down changes from main
smittals2
smittals2 previously approved these changes Jun 18, 2025
kingstjo added 2 commits June 20, 2025 21:38
- Replace custom SensitiveString class with bssl::UniquePtr<std::string>
- Use BORINGSSL_MAKE_DELETER with custom SensitiveStringDeleter function
- Maintain same security by calling OPENSSL_cleanse before deletion
- Update all function calls to use pointer semantics (.get(), *, ->)
- Improve code consistency with existing bssl::UniquePtr usage in file
- All PKCS#8 tests continue to pass

This addresses team feedback to leverage existing BoringSSL deleter
infrastructure instead of custom RAII implementation.
kingstjo and others added 2 commits June 24, 2025 10:23
Resolved unresolved merge conflict markers that were causing compilation failures.
The conflict was between different include statements that needed to be merged properly.
Copy link
Contributor

@smittals2 smittals2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! My only other ask is could you verify before merge if this is one of the cli tools where order of input flags determines order of output in Openssl?

If it is, see these as examples of what would be required to change:
#2501
17530c1

@kingstjo kingstjo merged commit 45d7b89 into aws:main Jul 9, 2025
129 of 133 checks passed
@kingstjo kingstjo deleted the implement-pkcs8-cli branch August 21, 2025 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants