mirror of
https://github.com/gregtwallace/apc-p15-tool.git
synced 2025-01-22 08:14:08 +00:00
cbb831e009
* apcssh: add descriptive error when required file(s) not passed * create: dont create key+cert file when key isn't supported by NMC2 * config: fix usage messages re: key types * p15 files: dont generate key+cert when it isn't needed (aka NMC2 doesn't support key) * pkcs15: pre-calculate envelope when making the p15 struct * pkcs15: omit key ID 8 & 9 from EC keys * pkcs15: update key decode logic * pkcs15: add key type value for easy determination of compatibility * pkcs15: add ec key support * pkcs15: separate functions for key and key+cert p15 files * update README see: https://github.com/gregtwallace/apc-p15-tool/issues/6
41 lines
984 B
Go
41 lines
984 B
Go
package pkcs15
|
|
|
|
import (
|
|
"apc-p15-tool/pkg/tools/asn1obj"
|
|
"crypto/ecdsa"
|
|
"crypto/rsa"
|
|
)
|
|
|
|
// privateKeyObject returns the ASN.1 representation of a private key
|
|
func (p15 *pkcs15KeyCert) privateKeyObject() []byte {
|
|
var privKeyObj []byte
|
|
|
|
switch privKey := p15.key.(type) {
|
|
case *rsa.PrivateKey:
|
|
privKey.Precompute()
|
|
|
|
// ensure all expected vals are available
|
|
privKeyObj = asn1obj.Sequence([][]byte{
|
|
// P
|
|
asn1obj.IntegerExplicitValue(3, privKey.Primes[0]),
|
|
// Q
|
|
asn1obj.IntegerExplicitValue(4, privKey.Primes[1]),
|
|
// Dp
|
|
asn1obj.IntegerExplicitValue(5, privKey.Precomputed.Dp),
|
|
// Dq
|
|
asn1obj.IntegerExplicitValue(6, privKey.Precomputed.Dq),
|
|
// Qinv
|
|
asn1obj.IntegerExplicitValue(7, privKey.Precomputed.Qinv),
|
|
})
|
|
|
|
case *ecdsa.PrivateKey:
|
|
// Only private piece is the integer D
|
|
privKeyObj = asn1obj.Integer(privKey.D)
|
|
|
|
default:
|
|
// panic if unsupported key
|
|
panic("private key type is unexpected and unsupported")
|
|
}
|
|
|
|
return privKeyObj
|
|
}
|