mirror of
https://github.com/square/certigo.git
synced 2026-10-19 16:15:40 +00:00
This commit adds a JCEKS encoder to the `jceks` package. Previously, the package was only capable of reading JCEKS files. Internally, this commmit effectively rewrites the package. These changes are backward compatible with the previous package API and thus do not require a new major version. However, there are several behavioral changes that have the potential to break existing code that relied on edge cases: - The package now returns an error when attempting to parse a JCEKS file that contains an invalid X.509 certificate (either as a trusted certificate entry, or as part of the chain for a private key entry). Previously, the package silently ignored these certificates. JCEKS files containing invalid certificates are invalid, so affected files should be repaired. - The package now returns an error by default when parsing a JCEKS file containing a certificate or private key larger than 20MiB. This limit guards against trivial memory exhaustion denial of service attacks from malicious files, but the default is very generous. Callers that need to parse larger certificates or private keys can override the defaults using the new `KeyStore.ParseWithOptions` method with appropriate options. The internal changes to the package introduced by this commit are substantial. Some of the highlights include: - Fixed a bug with modified UTF-8 parsing. - `testdata` is now generated by `go generate`, rather than shelling out to `openssl` and `keytool` at unit test time. - PKCS#5 padding is now removed when parsing. - Tests are now far more comprehensive, including fuzz tests. - General code cleanup and linter fixes.
79 lines
2.5 KiB
Go
79 lines
2.5 KiB
Go
// Copyright 2025 Block, Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package jceks
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPKCS5PadUnpad(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
in []byte
|
|
expected []byte
|
|
}{
|
|
{in: []byte{}, expected: []byte{8, 8, 8, 8, 8, 8, 8, 8}},
|
|
{in: []byte{42}, expected: []byte{42, 7, 7, 7, 7, 7, 7, 7}},
|
|
{in: []byte{42, 42}, expected: []byte{42, 42, 6, 6, 6, 6, 6, 6}},
|
|
{in: []byte{42, 42, 42}, expected: []byte{42, 42, 42, 5, 5, 5, 5, 5}},
|
|
{in: []byte{42, 42, 42, 42}, expected: []byte{42, 42, 42, 42, 4, 4, 4, 4}},
|
|
{in: []byte{42, 42, 42, 42, 42}, expected: []byte{42, 42, 42, 42, 42, 3, 3, 3}},
|
|
{in: []byte{42, 42, 42, 42, 42, 42}, expected: []byte{42, 42, 42, 42, 42, 42, 2, 2}},
|
|
{in: []byte{42, 42, 42, 42, 42, 42, 42}, expected: []byte{42, 42, 42, 42, 42, 42, 42, 1}},
|
|
{in: []byte{42, 42, 42, 42, 42, 42, 42, 42}, expected: []byte{42, 42, 42, 42, 42, 42, 42, 42, 8, 8, 8, 8, 8, 8, 8, 8}},
|
|
{in: []byte{0}, expected: []byte{0, 7, 7, 7, 7, 7, 7, 7}},
|
|
{in: []byte{1, 2, 3, 4, 5}, expected: []byte{1, 2, 3, 4, 5, 3, 3, 3}},
|
|
{in: []byte{3, 3, 3, 3, 3}, expected: []byte{3, 3, 3, 3, 3, 3, 3, 3}},
|
|
} {
|
|
t.Run(fmt.Sprintf("In-%X", tc.in), func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
actual := pkcs5Pad(tc.in)
|
|
require.Equal(t, tc.expected, actual)
|
|
|
|
unpadded, err := pkcs5Unpad(actual)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.in, unpadded)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPKCS5UnpadFailures(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
in []byte
|
|
}{
|
|
{name: "Empty", in: nil},
|
|
{name: "TooShort", in: []byte{1, 2, 3, 4, 5, 6, 7}},
|
|
{name: "PadExceedsBlockSize", in: []byte{9, 9, 9, 9, 9, 9, 9, 9, 9}},
|
|
{name: "InvalidPadding", in: []byte{42, 7, 7, 7, 6, 7, 7, 7}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := pkcs5Unpad(tc.in)
|
|
require.Error(t, err)
|
|
require.ErrorIs(t, err, ErrInvalidCiphertext)
|
|
})
|
|
}
|
|
}
|