2024-02-02 23:35:21 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"apc-p15-tool/pkg/pkcs15"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2024-06-07 02:51:12 +00:00
|
|
|
// pemToAPCP15 reads the specified pem files and returns the apc p15 files (both a
|
2024-06-04 22:59:36 +00:00
|
|
|
// p15 file with just the private key, and also a p15 file with both the private key
|
|
|
|
// and certificate). The key+cert file includes the required APC header, prepended.
|
2024-06-07 02:51:12 +00:00
|
|
|
func (app *app) pemToAPCP15(keyPem, certPem []byte, parentCmdName string) (keyFile []byte, apcKeyCertFile []byte, err error) {
|
2024-02-03 16:38:31 +00:00
|
|
|
app.stdLogger.Printf("%s: making apc p15 file from pem", parentCmdName)
|
2024-02-02 23:35:21 +00:00
|
|
|
|
|
|
|
// make p15 struct
|
|
|
|
p15, err := pkcs15.ParsePEMToPKCS15(keyPem, certPem)
|
|
|
|
if err != nil {
|
2024-06-04 22:59:36 +00:00
|
|
|
return nil, nil, fmt.Errorf("%s: failed to parse pem files (%w)", parentCmdName, err)
|
2024-02-02 23:35:21 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 16:38:31 +00:00
|
|
|
app.stdLogger.Printf("%s: successfully loaded pem files", parentCmdName)
|
2024-02-02 23:35:21 +00:00
|
|
|
|
|
|
|
// make file bytes
|
2024-06-04 22:59:36 +00:00
|
|
|
keyCertFile, keyFile, err := p15.ToP15Files()
|
2024-02-02 23:35:21 +00:00
|
|
|
if err != nil {
|
2024-06-04 22:59:36 +00:00
|
|
|
return nil, nil, fmt.Errorf("%s: failed to make p15 file (%w)", parentCmdName, err)
|
2024-02-02 23:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make header for file bytes
|
2024-06-04 22:59:36 +00:00
|
|
|
apcHeader, err := makeFileHeader(keyCertFile)
|
2024-02-02 23:35:21 +00:00
|
|
|
if err != nil {
|
2024-06-04 22:59:36 +00:00
|
|
|
return nil, nil, fmt.Errorf("%s: failed to make p15 file header (%w)", parentCmdName, err)
|
2024-02-02 23:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// combine header with file
|
2024-06-04 22:59:36 +00:00
|
|
|
apcKeyCertFile = append(apcHeader, keyCertFile...)
|
2024-02-02 23:35:21 +00:00
|
|
|
|
2024-02-03 16:38:31 +00:00
|
|
|
app.stdLogger.Printf("%s: apc p15 file data succesfully generated", parentCmdName)
|
2024-02-02 23:35:21 +00:00
|
|
|
|
2024-06-07 02:51:12 +00:00
|
|
|
return keyFile, apcKeyCertFile, nil
|
2024-02-02 23:35:21 +00:00
|
|
|
}
|