2024-01-28 16:16:26 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-04 22:59:36 +00:00
|
|
|
"encoding/base64"
|
2024-01-28 16:16:26 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
const (
|
|
|
|
createDefaultOutFilePath = "apctool.p15"
|
|
|
|
createDefaultOutKeyFilePath = "apctool.key.p15"
|
|
|
|
)
|
2024-01-28 16:16:26 +00:00
|
|
|
|
|
|
|
// cmdCreate is the app's command to create an apc p15 file from key and cert
|
|
|
|
// pem files
|
|
|
|
func (app *app) cmdCreate(_ context.Context, args []string) error {
|
|
|
|
// extra args == error
|
|
|
|
if len(args) != 0 {
|
2024-02-02 23:35:20 +00:00
|
|
|
return fmt.Errorf("create: failed, %w (%d)", ErrExtraArgs, len(args))
|
2024-01-28 16:16:26 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 23:35:22 +00:00
|
|
|
keyPem, certPem, err := app.config.create.keyCertPemCfg.GetPemBytes("create")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-28 16:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validation done
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
// make p15 files
|
2024-06-07 02:51:12 +00:00
|
|
|
keyFile, apcKeyCertFile, err := app.pemToAPCP15(keyPem, certPem, "create")
|
2024-01-28 16:16:26 +00:00
|
|
|
if err != nil {
|
2024-02-02 23:35:21 +00:00
|
|
|
return err
|
2024-01-28 16:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// determine file name (should already be done by flag parsing, but avoid nil just in case)
|
2024-06-04 22:59:36 +00:00
|
|
|
keyCertFileName := createDefaultOutFilePath
|
2024-01-28 16:16:26 +00:00
|
|
|
if app.config.create.outFilePath != nil && *app.config.create.outFilePath != "" {
|
2024-06-04 22:59:36 +00:00
|
|
|
keyCertFileName = *app.config.create.outFilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
keyFileName := createDefaultOutFilePath
|
|
|
|
if app.config.create.outKeyFilePath != nil && *app.config.create.outKeyFilePath != "" {
|
|
|
|
keyFileName = *app.config.create.outKeyFilePath
|
2024-01-28 16:16:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
// write file(s)
|
2024-06-07 02:51:13 +00:00
|
|
|
err = os.WriteFile(keyFileName, keyFile, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create: failed to write apc p15 key file (%s)", err)
|
|
|
|
}
|
|
|
|
app.stdLogger.Printf("create: apc p15 key file %s written to disk", keyFileName)
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
err = os.WriteFile(keyCertFileName, apcKeyCertFile, 0600)
|
2024-01-28 16:16:26 +00:00
|
|
|
if err != nil {
|
2024-06-04 22:59:36 +00:00
|
|
|
return fmt.Errorf("create: failed to write apc p15 key+cert file (%s)", err)
|
2024-01-28 16:16:26 +00:00
|
|
|
}
|
2024-06-04 22:59:36 +00:00
|
|
|
app.stdLogger.Printf("create: apc p15 key+cert file %s written to disk", keyCertFileName)
|
2024-01-28 16:16:26 +00:00
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
// if debug, write additional debug files (b64 format to make copy/paste into asn1 decoder
|
|
|
|
// easy to do e.g., https://lapo.it/asn1js)
|
|
|
|
if app.config.debugLogging != nil && *app.config.debugLogging {
|
2024-06-07 02:51:13 +00:00
|
|
|
keyFileNameDebug := keyFileName + ".b64"
|
|
|
|
err = os.WriteFile(keyFileNameDebug, []byte(base64.StdEncoding.EncodeToString(keyFile)), 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create: failed to write apc p15 key file (%s)", err)
|
|
|
|
}
|
|
|
|
app.debugLogger.Printf("create: apc p15 key file %s written to disk", keyFileNameDebug)
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
keyCertFileNameDebug := keyCertFileName + ".noheader.b64"
|
2024-06-04 22:59:36 +00:00
|
|
|
err = os.WriteFile(keyCertFileNameDebug, []byte(base64.StdEncoding.EncodeToString(apcKeyCertFile[apcHeaderLen:])), 0600)
|
2024-06-04 22:59:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create: failed to write apc p15 key+cert file (%s)", err)
|
|
|
|
}
|
|
|
|
app.debugLogger.Printf("create: apc p15 key+cert file %s written to disk", keyCertFileNameDebug)
|
|
|
|
|
|
|
|
keyCertFileNameHeaderDebug := keyCertFileName + ".header.b64"
|
2024-06-04 23:00:56 +00:00
|
|
|
err = os.WriteFile(keyCertFileNameHeaderDebug, []byte(base64.StdEncoding.EncodeToString(apcKeyCertFile[:apcHeaderLen])), 0600)
|
2024-06-04 22:59:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create: failed to write apc p15 key+cert file (%s)", err)
|
|
|
|
}
|
|
|
|
app.debugLogger.Printf("create: apc p15 key+cert file header %s written to disk", keyCertFileNameHeaderDebug)
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 16:16:26 +00:00
|
|
|
return nil
|
|
|
|
}
|