-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
- Loading branch information
Showing
8 changed files
with
308 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Copyright 2018-2020 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 libjvm | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pavlo-v-chernykh/keystore-go/v4" | ||
"golang.org/x/sys/unix" | ||
"software.sslmate.com/src/go-pkcs12" | ||
) | ||
|
||
type Keystore interface { | ||
Add(string, *pem.Block) error | ||
Write() error | ||
} | ||
|
||
func DetectKeystore(location string) (Keystore, error) { | ||
buf, err := os.ReadFile(location) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(buf) == 0 { | ||
return nil, errors.New("empty keystore found") | ||
} | ||
|
||
if len(buf) > 3 && buf[0] == 0xFE && buf[1] == 0xED && buf[2] == 0xFE && buf[3] == 0xED { | ||
return NewJKSKeystore(location, "changeit") | ||
} | ||
|
||
return NewPKCS12Keystore(location), nil | ||
} | ||
|
||
var _ Keystore = &JKSKeystore{} | ||
|
||
type JKSKeystore struct { | ||
location string | ||
password string | ||
store keystore.KeyStore | ||
} | ||
|
||
func NewJKSKeystore(location, password string) (*JKSKeystore, error) { | ||
in, err := os.Open(location) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to open %s\n%w", location, err) | ||
} | ||
defer in.Close() | ||
|
||
ks := keystore.New(keystore.WithOrderedAliases()) | ||
if err := ks.Load(in, []byte(password)); err != nil { | ||
return nil, fmt.Errorf("unable to decode keystore\n %w", err) | ||
} | ||
return &JKSKeystore{ | ||
location: location, | ||
password: password, | ||
store: ks, | ||
}, nil | ||
} | ||
|
||
func (k *JKSKeystore) Add(name string, b *pem.Block) error { | ||
entry := keystore.TrustedCertificateEntry{ | ||
CreationTime: NormalizedDateTime, | ||
Certificate: keystore.Certificate{ | ||
Type: "X.509", | ||
Content: b.Bytes, | ||
}, | ||
} | ||
if err := k.store.SetTrustedCertificateEntry(name, entry); err != nil { | ||
return fmt.Errorf("unable to add trusted entry\n%w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (k *JKSKeystore) Write() error { | ||
if unix.Access(k.location, unix.W_OK) != nil { | ||
return nil | ||
} | ||
|
||
out, err := os.OpenFile(k.location, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return fmt.Errorf("unable to open %s\n%w", k.location, err) | ||
} | ||
defer out.Close() | ||
|
||
if err := k.store.Store(out, []byte(k.password)); err != nil { | ||
return fmt.Errorf("unable to encode keystore\n%w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (k *JKSKeystore) Len() int { | ||
return len(k.store.Aliases()) | ||
} | ||
|
||
var _ Keystore = &PKCS12Keystore{} | ||
|
||
type PKCS12Keystore struct { | ||
location string | ||
entries []pkcs12.TrustStoreEntry | ||
} | ||
|
||
func NewPKCS12Keystore(location string) *PKCS12Keystore { | ||
return &PKCS12Keystore{ | ||
location: location, | ||
entries: []pkcs12.TrustStoreEntry{}, | ||
} | ||
} | ||
|
||
func (k *PKCS12Keystore) Add(name string, b *pem.Block) error { | ||
cert, err := x509.ParseCertificate(b.Bytes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
k.entries = append(k.entries, pkcs12.TrustStoreEntry{ | ||
Cert: cert, | ||
FriendlyName: name, | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func (k *PKCS12Keystore) Write() error { | ||
out, err := os.OpenFile(k.location, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
data, err := pkcs12.Passwordless.EncodeTrustStoreEntries(k.entries, "") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = out.Write(data) | ||
return err | ||
} | ||
|
||
func (k *PKCS12Keystore) Len() int { | ||
data, _ := os.ReadFile(k.location) | ||
certs, _ := pkcs12.DecodeTrustStore(data, "") | ||
return len(certs) + len(k.entries) | ||
} |
Oops, something went wrong.