Skip to content

Commit

Permalink
Fix alias matching (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
godrei authored Feb 5, 2024
1 parent 40e1f89 commit 0b2c5ae
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
2 changes: 2 additions & 0 deletions keystore/jks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"strings"

"github.com/lwithers/minijks/jks"
)
Expand All @@ -12,6 +13,7 @@ type JKSKeystoreDecoder struct {
}

func (d JKSKeystoreDecoder) Decode(data []byte, password, alias, keyPassword string) (privateKey interface{}, certificate *x509.Certificate, err error) {
alias = strings.ToLower(alias)
ks, err := jks.Parse(data, &jks.Options{
Password: password,
SkipVerifyDigest: false,
Expand Down
49 changes: 39 additions & 10 deletions keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestParse(t *testing.T) {
}{
{
name: "PKCS12 keystore test",
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
pth: filepath.Join("testdata", "keystore.pkcs12"),
password: "storepass",
privateKeyAlias: "key0",
privateKeyPassword: "keypass",
Expand All @@ -39,7 +39,7 @@ func TestParse(t *testing.T) {
},
{
name: "JKS keystore test",
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
pth: filepath.Join("testdata", "keystore.jks"),
password: "keystore",
privateKeyAlias: "mykey",
privateKeyPassword: "keystore",
Expand All @@ -54,6 +54,35 @@ func TestParse(t *testing.T) {
ValidUntil: "2043-11-30 10:10:41 +0000 UTC",
},
},
{
name: "PKCS12 Keystore with upper case letters in the alias",
pth: filepath.Join("testdata", "upper_case_alias_keystore.pkcs12"),
password: "keystore",
privateKeyAlias: "MyKey",
privateKeyPassword: "keystore",
want: &CertificateInformation{
Organization: "Bitrise",
ValidFrom: "2024-01-31 14:08:42 +0000 UTC",
ValidUntil: "2049-01-24 14:08:42 +0000 UTC",
},
},
{
name: "JKS Keystore with upper case letters in the alias",
pth: filepath.Join("testdata", "upper_case_alias_keystore.jks"),
password: "keystore",
privateKeyAlias: "Alias0",
privateKeyPassword: "keystore",
want: &CertificateInformation{
FirstAndLastName: "Unknown",
OrganizationalUnit: "Unknown",
Organization: "Bitrise",
CityOrLocality: "Unknown",
StateOrProvince: "Unknown",
CountryCode: "Unknown",
ValidFrom: "2024-01-31 14:34:34 +0000 UTC",
ValidUntil: "2051-06-18 14:34:34 +0000 UTC",
},
},
{
name: "Invalid file",
pth: filepath.Join("testdata", "empty_file"),
Expand Down Expand Up @@ -100,47 +129,47 @@ func TestIncorrectKeystoreCredentials(t *testing.T) {
}{
{
name: "PKCS12 keystore test - incorrect password",
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
pth: filepath.Join("testdata", "keystore.pkcs12"),
password: "incorrect-password",
privateKeyAlias: "key0",
privateKeyPassword: "keypass",
wantError: IncorrectKeystorePasswordError.Error(),
},
{
name: "PKCS12 keystore test - incorrect alias",
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
pth: filepath.Join("testdata", "keystore.pkcs12"),
password: "storepass",
privateKeyAlias: "incorrect-alias",
privateKeyPassword: "keypass",
wantError: IncorrectAliasError.Error(),
},
{
name: "PKCS12 keystore test - incorrect key password",
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
pth: filepath.Join("testdata", "keystore.pkcs12"),
password: "storepass",
privateKeyAlias: "key0",
privateKeyPassword: "incorrect-keypassword",
wantError: IncorrectKeyPasswordError.Error(),
},
{
name: "JKS keystore test - incorrect password",
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
pth: filepath.Join("testdata", "keystore.jks"),
password: "incorrect-password",
privateKeyAlias: "mykey",
privateKeyPassword: "keystore",
wantError: IncorrectKeystorePasswordError.Error(),
},
{
name: "JKS keystore test - incorrect alias",
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
pth: filepath.Join("testdata", "keystore.jks"),
password: "keystore",
privateKeyAlias: "incorrect-alias",
privateKeyPassword: "keystore",
wantError: IncorrectAliasError.Error(),
},
{
name: "JKS keystore test - incorrect key password",
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
pth: filepath.Join("testdata", "keystore.jks"),
password: "keystore",
privateKeyAlias: "mykey",
privateKeyPassword: "incorrect-keypassword",
Expand Down Expand Up @@ -180,7 +209,7 @@ func TestIsInvalidCredentialsError(t *testing.T) {
{
name: "PKCS12 keystore, JKS decoder",
decoder: JKSKeystoreDecoder{},
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
pth: filepath.Join("testdata", "keystore.pkcs12"),
password: "storepass",
privateKeyAlias: "key0",
privateKeyPassword: "keypass",
Expand All @@ -189,7 +218,7 @@ func TestIsInvalidCredentialsError(t *testing.T) {
{
name: "JKS keystore, PKCS12 decoder",
decoder: PKCS12KeystoreDecoder{},
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
pth: filepath.Join("testdata", "keystore.jks"),
password: "keystore",
privateKeyAlias: "mykey",
privateKeyPassword: "keystore",
Expand Down
2 changes: 2 additions & 0 deletions keystore/pkcs12.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"strings"

"github.com/bitrise-io/go-pkcs12"
)
Expand All @@ -12,6 +13,7 @@ type PKCS12KeystoreDecoder struct {
}

func (d PKCS12KeystoreDecoder) Decode(data []byte, password, alias, keyPassword string) (privateKey interface{}, certificate *x509.Certificate, err error) {
alias = strings.ToLower(alias)
key, cert, err := pkcs12.DecodeKeystore(data, password, alias, keyPassword)
if err != nil {
return nil, nil, keystoreErrorFromPKCS12Error(err)
Expand Down
8 changes: 4 additions & 4 deletions keystore/testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ The files stored here are used in the packages tests:

This is just an empty file for testing keystore reading with an invalid file.

`pkcs12_type_keystore.jks`
`<keystore_name>.pkcs12`

This file is a PKCS12 type keystore and was generated using Android Studio (Build / "Generate Signed Bundle / APK" and going with the create new keystore option).
These files are PKCS12 type keystores and were generated using Android Studio (Build / "Generate Signed Bundle / APK" and going with the create new keystore option).

`jks_type_keystore.keystore`
`<keystore_name>.jks`

This file is a JKS type keystore, such a keystore can be generated using the following command:
These files are JKS type keystores, such a keystore can be generated using keytool:

`keytool -genkey -v -keystore my.keystore -alias my_alias -keyalg RSA -keysize 2048 -validity 1095 -storetype jks -dname "CN=My Common Name,O=My Organisation,C=My Local"`
File renamed without changes.
File renamed without changes.
Binary file added keystore/testdata/upper_case_alias_keystore.jks
Binary file not shown.
Binary file not shown.

0 comments on commit 0b2c5ae

Please sign in to comment.