2024-02-02 23:35:21 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"apc-p15-tool/pkg/pkcs15"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pemToAPCP15 reads the specified pem files and returns the apc p15 bytes
|
2024-02-02 23:35:22 +00:00
|
|
|
func (app *app) pemToAPCP15(keyPem, certPem []byte, parentCmdName string) ([]byte, 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 {
|
|
|
|
return nil, fmt.Errorf("%s: failed to parse pem files (%w)", parentCmdName, err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
p15File, err := p15.ToP15File()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: failed to make p15 file (%w)", parentCmdName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make header for file bytes
|
|
|
|
apcHeader, err := makeFileHeader(p15File)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: failed to make p15 file header (%w)", parentCmdName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// combine header with file
|
|
|
|
apcFile := append(apcHeader, p15File...)
|
|
|
|
|
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
|
|
|
|
|
|
|
return apcFile, nil
|
|
|
|
}
|