-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw2der.go
122 lines (109 loc) · 2.68 KB
/
raw2der.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package rsa
import (
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"math/big"
"bandr.me/p/pocryp/internal/cli/cmd"
)
var Raw2DerCmd = &cmd.Command{
Name: "rsa-raw2der",
Run: runRaw2Der,
Brief: "Convert RSA key from raw values(n, e, d, p, q) to PKCS#1 ASN.1 DER",
Usage: `Usage: pocryp rsa-raw2der [-priv|-pub] -n modulus [-e publicExponent] -d privateExponent [-p prime1 -q prime2]
Convert RSA key from raw values(n, e, d, p, q) to PKCS#1 ASN.1 DER.
`,
}
func runRaw2Der(cmd *cmd.Command) error {
fPriv := cmd.Flags.Bool("priv", false, "Encode PrivateKey from given inputs.")
fPub := cmd.Flags.Bool("pub", false, "Encode PublicKey from given inputs.")
fMod := cmd.Flags.String("n", "", "Modulus as hex string")
fPubExp := cmd.Flags.Int("e", 0, "Public exponent as integer")
fPrivExp := cmd.Flags.String("d", "", "Private exponent as hex string")
fPrime1 := cmd.Flags.String("p", "", "First prime number as hex string")
fPrime2 := cmd.Flags.String("q", "", "Second prime number as hex string")
if isHelp, err := cmd.Parse(); err != nil {
if isHelp {
return nil
}
return err
}
if *fMod == "" {
cmd.Flags.Usage()
return errors.New("modulus not specified, use -n to specify it")
}
if *fPub && *fPriv {
cmd.Flags.Usage()
return errors.New("cannot specify -priv and -pub at the same time, choose one")
}
nBytes, err := hex.DecodeString(*fMod)
if err != nil {
return err
}
n := new(big.Int)
n.SetBytes(nBytes)
var result []byte
switch {
case *fPriv:
if *fPubExp == 0 {
cmd.Flags.Usage()
return errors.New("-e is needed")
}
if *fPrivExp == "" {
cmd.Flags.Usage()
return errors.New("-d is needed")
}
if *fPrime1 == "" {
cmd.Flags.Usage()
return errors.New("-p is needed")
}
if *fPrime2 == "" {
cmd.Flags.Usage()
return errors.New("-q is needed")
}
dBytes, err := hex.DecodeString(*fPrivExp)
if err != nil {
return err
}
d := new(big.Int)
d.SetBytes(dBytes)
pBytes, err := hex.DecodeString(*fPrime1)
if err != nil {
return err
}
p := new(big.Int)
p.SetBytes(pBytes)
qBytes, err := hex.DecodeString(*fPrime2)
if err != nil {
return err
}
q := new(big.Int)
q.SetBytes(qBytes)
key := &rsa.PrivateKey{
PublicKey: rsa.PublicKey{
N: n,
E: *fPubExp,
},
D: d,
Primes: []*big.Int{p, q},
}
result = x509.MarshalPKCS1PrivateKey(key)
case *fPub:
if *fPubExp == 0 {
cmd.Flags.Usage()
return errors.New("-e is needed")
}
key := &rsa.PublicKey{
N: n,
E: *fPubExp,
}
result = x509.MarshalPKCS1PublicKey(key)
default:
cmd.Flags.Usage()
return errors.New("need to specify one of -priv or -pub")
}
fmt.Println(hex.EncodeToString(result))
return nil
}