Skip to content

Commit 0b2c5ae

Browse files
authored
Fix alias matching (#58)
1 parent 40e1f89 commit 0b2c5ae

8 files changed

+47
-14
lines changed

keystore/jks.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/x509"
55
"errors"
66
"fmt"
7+
"strings"
78

89
"github.com/lwithers/minijks/jks"
910
)
@@ -12,6 +13,7 @@ type JKSKeystoreDecoder struct {
1213
}
1314

1415
func (d JKSKeystoreDecoder) Decode(data []byte, password, alias, keyPassword string) (privateKey interface{}, certificate *x509.Certificate, err error) {
16+
alias = strings.ToLower(alias)
1517
ks, err := jks.Parse(data, &jks.Options{
1618
Password: password,
1719
SkipVerifyDigest: false,

keystore/keystore_test.go

+39-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestParse(t *testing.T) {
2222
}{
2323
{
2424
name: "PKCS12 keystore test",
25-
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
25+
pth: filepath.Join("testdata", "keystore.pkcs12"),
2626
password: "storepass",
2727
privateKeyAlias: "key0",
2828
privateKeyPassword: "keypass",
@@ -39,7 +39,7 @@ func TestParse(t *testing.T) {
3939
},
4040
{
4141
name: "JKS keystore test",
42-
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
42+
pth: filepath.Join("testdata", "keystore.jks"),
4343
password: "keystore",
4444
privateKeyAlias: "mykey",
4545
privateKeyPassword: "keystore",
@@ -54,6 +54,35 @@ func TestParse(t *testing.T) {
5454
ValidUntil: "2043-11-30 10:10:41 +0000 UTC",
5555
},
5656
},
57+
{
58+
name: "PKCS12 Keystore with upper case letters in the alias",
59+
pth: filepath.Join("testdata", "upper_case_alias_keystore.pkcs12"),
60+
password: "keystore",
61+
privateKeyAlias: "MyKey",
62+
privateKeyPassword: "keystore",
63+
want: &CertificateInformation{
64+
Organization: "Bitrise",
65+
ValidFrom: "2024-01-31 14:08:42 +0000 UTC",
66+
ValidUntil: "2049-01-24 14:08:42 +0000 UTC",
67+
},
68+
},
69+
{
70+
name: "JKS Keystore with upper case letters in the alias",
71+
pth: filepath.Join("testdata", "upper_case_alias_keystore.jks"),
72+
password: "keystore",
73+
privateKeyAlias: "Alias0",
74+
privateKeyPassword: "keystore",
75+
want: &CertificateInformation{
76+
FirstAndLastName: "Unknown",
77+
OrganizationalUnit: "Unknown",
78+
Organization: "Bitrise",
79+
CityOrLocality: "Unknown",
80+
StateOrProvince: "Unknown",
81+
CountryCode: "Unknown",
82+
ValidFrom: "2024-01-31 14:34:34 +0000 UTC",
83+
ValidUntil: "2051-06-18 14:34:34 +0000 UTC",
84+
},
85+
},
5786
{
5887
name: "Invalid file",
5988
pth: filepath.Join("testdata", "empty_file"),
@@ -100,47 +129,47 @@ func TestIncorrectKeystoreCredentials(t *testing.T) {
100129
}{
101130
{
102131
name: "PKCS12 keystore test - incorrect password",
103-
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
132+
pth: filepath.Join("testdata", "keystore.pkcs12"),
104133
password: "incorrect-password",
105134
privateKeyAlias: "key0",
106135
privateKeyPassword: "keypass",
107136
wantError: IncorrectKeystorePasswordError.Error(),
108137
},
109138
{
110139
name: "PKCS12 keystore test - incorrect alias",
111-
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
140+
pth: filepath.Join("testdata", "keystore.pkcs12"),
112141
password: "storepass",
113142
privateKeyAlias: "incorrect-alias",
114143
privateKeyPassword: "keypass",
115144
wantError: IncorrectAliasError.Error(),
116145
},
117146
{
118147
name: "PKCS12 keystore test - incorrect key password",
119-
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
148+
pth: filepath.Join("testdata", "keystore.pkcs12"),
120149
password: "storepass",
121150
privateKeyAlias: "key0",
122151
privateKeyPassword: "incorrect-keypassword",
123152
wantError: IncorrectKeyPasswordError.Error(),
124153
},
125154
{
126155
name: "JKS keystore test - incorrect password",
127-
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
156+
pth: filepath.Join("testdata", "keystore.jks"),
128157
password: "incorrect-password",
129158
privateKeyAlias: "mykey",
130159
privateKeyPassword: "keystore",
131160
wantError: IncorrectKeystorePasswordError.Error(),
132161
},
133162
{
134163
name: "JKS keystore test - incorrect alias",
135-
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
164+
pth: filepath.Join("testdata", "keystore.jks"),
136165
password: "keystore",
137166
privateKeyAlias: "incorrect-alias",
138167
privateKeyPassword: "keystore",
139168
wantError: IncorrectAliasError.Error(),
140169
},
141170
{
142171
name: "JKS keystore test - incorrect key password",
143-
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
172+
pth: filepath.Join("testdata", "keystore.jks"),
144173
password: "keystore",
145174
privateKeyAlias: "mykey",
146175
privateKeyPassword: "incorrect-keypassword",
@@ -180,7 +209,7 @@ func TestIsInvalidCredentialsError(t *testing.T) {
180209
{
181210
name: "PKCS12 keystore, JKS decoder",
182211
decoder: JKSKeystoreDecoder{},
183-
pth: filepath.Join("testdata", "pkcs12_type_keystore.jks"),
212+
pth: filepath.Join("testdata", "keystore.pkcs12"),
184213
password: "storepass",
185214
privateKeyAlias: "key0",
186215
privateKeyPassword: "keypass",
@@ -189,7 +218,7 @@ func TestIsInvalidCredentialsError(t *testing.T) {
189218
{
190219
name: "JKS keystore, PKCS12 decoder",
191220
decoder: PKCS12KeystoreDecoder{},
192-
pth: filepath.Join("testdata", "jks_type_keystore.keystore"),
221+
pth: filepath.Join("testdata", "keystore.jks"),
193222
password: "keystore",
194223
privateKeyAlias: "mykey",
195224
privateKeyPassword: "keystore",

keystore/pkcs12.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/x509"
55
"errors"
66
"fmt"
7+
"strings"
78

89
"github.com/bitrise-io/go-pkcs12"
910
)
@@ -12,6 +13,7 @@ type PKCS12KeystoreDecoder struct {
1213
}
1314

1415
func (d PKCS12KeystoreDecoder) Decode(data []byte, password, alias, keyPassword string) (privateKey interface{}, certificate *x509.Certificate, err error) {
16+
alias = strings.ToLower(alias)
1517
key, cert, err := pkcs12.DecodeKeystore(data, password, alias, keyPassword)
1618
if err != nil {
1719
return nil, nil, keystoreErrorFromPKCS12Error(err)

keystore/testdata/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ The files stored here are used in the packages tests:
66

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

9-
`pkcs12_type_keystore.jks`
9+
`<keystore_name>.pkcs12`
1010

11-
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).
11+
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).
1212

13-
`jks_type_keystore.keystore`
13+
`<keystore_name>.jks`
1414

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

1717
`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"`
2.19 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)