-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
369 lines (311 loc) · 7.85 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/pem"
"fmt"
"math/big"
"os"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/kubasiemion/ibmdilithium/ibmdilithium"
"github.com/kubasiemion/x509PQexpansion/x509"
kmsclitool "github.com/proveniencenft/kmsclitool/common"
)
var ethaddr = "746D2391A33011e8515FC9D2C0fA9c7DDd882205" //"b0704A27a633ED43afB168AE7d9C662bCfc1d678"
var ethkey string
func init() {
kf, e := kmsclitool.ReadKeyfile("test746d2391a33.json")
if e != nil {
panic(e)
}
key, e := kmsclitool.KeyFromPassScrypt([]byte("abcdef"), kf.Crypto.KdfScryptParams)
if e != nil {
panic(e)
}
pltx, e := kmsclitool.Decrypt(kf, key)
if e != nil {
panic(e)
}
ethkey = hex.EncodeToString(pltx)
}
func main() {
block, _ := pem.Decode([]byte(selfSignedRootCertRSA4096Pem))
RootCert, e := x509.ParseCertificate(block.Bytes)
if e != nil {
fmt.Println(e)
return
}
//fmt.Println(RootCert.SerialNumber)
cte := GetTemplate()
cte.SerialNumber = big.NewInt(420042)
cte.Issuer.OrganizationalUnit = []string{"Some_Intermediate_Unit"}
block, _ = pem.Decode([]byte(rsa4096privPem))
rootRSAKey, e := x509.ParsePKCS8PrivateKey(block.Bytes)
if e != nil {
fmt.Println(e)
return
}
block, _ = pem.Decode([]byte(intermediateRSA2048keyPem))
interRSAKey, e := x509.ParsePKCS1PrivateKey(block.Bytes)
if e != nil {
fmt.Println(e)
return
}
interCertBytes, e := x509.CreateCertificate(rand.Reader, cte, RootCert, &interRSAKey.PublicKey, rootRSAKey)
if e != nil {
fmt.Println(e)
return
}
interCerts, e := x509.ParseCertificates(interCertBytes)
if e != nil {
fmt.Println(e)
return
}
if len(interCerts) == 0 {
fmt.Println("No certificates found")
return
}
interCert := interCerts[0]
e = interCert.CheckSignatureFrom(RootCert)
if e != nil {
fmt.Println(e)
return
}
//fmt.Println(interCert.SerialNumber)
pqPubKey := new(x509.PQPublicKey)
propr := ibmdilithium.TestPubKey()
pqPubKey.OID = propr.GetOID()
pqPubKey.RawBytes = propr.Bytes
ca := GetTemplate()
p2, txh, e := CreateAndNotarizeCertificate(ca, interCert, pqPubKey, interRSAKey, big.NewInt(11155111), true)
if e != nil {
fmt.Println(e)
return
}
fmt.Println("Issued new PQ certificate:")
fmt.Println(CertString(p2))
block = &pem.Block{Type: "CERTIFICATE", Bytes: p2.Raw}
certbuf := new(bytes.Buffer)
e = pem.Encode(certbuf, block)
if e != nil {
fmt.Println(e)
return
}
filename := "tx" + txh
os.WriteFile(filename, certbuf.Bytes(), 0700)
rootpool := x509.NewCertPool()
rootpool.AddCert(RootCert)
intpool := x509.NewCertPool()
intpool.AddCert(interCert)
fmt.Println()
fmt.Println("==================================")
fmt.Println("Verifying the chain of trust")
chains, err := p2.Verify(x509.VerifyOptions{Roots: rootpool, Intermediates: intpool})
fmt.Println("Chain verification error:", err)
fmt.Println(PrCertChains(chains))
//fmt.Println("v:", p2.CheckSignatureFrom(interCert))
}
func CreateAndNotarizeCertificate(template *x509.Certificate, parent *x509.Certificate, pubkey interface{},
privkey interface{}, chainID *big.Int, locking bool) (*x509.Certificate, string, error) {
e := Dial(int(chainID.Int64()))
if e != nil {
return nil, "", e
}
nonce, e := getNonce()
if e != nil {
return nil, "", e
}
noncebytes := big.NewInt(int64(nonce)).Bytes()
if len(noncebytes) == 0 {
noncebytes = []byte{0}
}
chainIDbytes := chainID.Bytes()
data := append(getEthAddr(), byte(len(chainIDbytes)))
data = append(data, chainIDbytes...)
data = append(data, noncebytes...)
template.SerialNumber = new(big.Int).SetBytes(data)
certb, e := x509.CreateCertificate(rand.Reader, template, parent, pubkey, privkey)
if e != nil {
return nil, "", e
}
p2, e := x509.ParseCertificate(certb)
if e != nil {
return nil, "", e
}
txh, e := NotarizeLocking(p2.Signature, nonce)
return p2, txh, e
}
func getEthAddr() []byte {
b, err := hex.DecodeString(ethaddr)
if err != nil {
fmt.Println(err)
}
return b
}
func getEthKey() []byte {
b, _ := hex.DecodeString(ethkey)
return b
}
var client *ethclient.Client
func Dial(chainid int) error {
nets, ok := apibychainid[chainid]
if !ok {
return fmt.Errorf("Unknown chain")
}
if len(nets) < 2 {
return fmt.Errorf("Unknown API endpoint for %v", chainid)
}
var err error
apiaddr := nets[1]
client, err = ethclient.Dial(apiaddr)
return err
}
func getNonce() (uint64, error) {
privateKey, err := crypto.HexToECDSA(ethkey)
if err != nil {
return 0, err
}
addr := crypto.PubkeyToAddress(privateKey.PublicKey)
nonce, err := client.PendingNonceAt(context.Background(), addr)
if err != nil {
return 0, err
}
return nonce, nil
}
func getChainID() (*big.Int, error) {
return client.ChainID(context.Background())
}
func NotarizeLocking(data []byte, nonce uint64) (string, error) {
txh, err := Notarize(data, nonce)
if err != nil {
return "", err
}
var pending bool
for pending || err == ethereum.NotFound {
_, pending, err = client.TransactionByHash(context.Background(), common.HexToHash(txh))
time.Sleep(time.Second * 20)
}
return txh, err
}
func Notarize(data []byte, nonce uint64) (string, error) {
//If data too long, hash to 512
//if len(data) > 64 {
// h := sha512.Sum512(data)
// data = h[:]
//}
chainID, err := client.ChainID(context.Background())
if err != nil {
return "", err
}
privateKey, err := crypto.HexToECDSA(ethkey)
if err != nil {
return "", err
}
toAddress := crypto.PubkeyToAddress(privateKey.PublicKey)
value := big.NewInt(0)
gasLimit := uint64(210000) // in units
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
//toAddress := common.HexToAddress(ethaddr)
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
return "", err
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return "", err
}
fmt.Printf("tx sent: %s\n\n", signedTx.Hash().Hex())
return signedTx.Hash().Hex(), nil
}
/*
func ReadBlockchainTime(cert *Certificate) string {
serbytes := cert.SerialNumber.Bytes()
if len(serbytes) < 23 {
return "Serial number too short for a notarized cert"
}
addrbytes := serbytes[:20]
chainidlen := int(serbytes[20])
if len(serbytes) - 20 - chainidlen <1 {
return "Serial number too short for a notarized cert"
}
chainid := new(big.Int).SetBytes(serbytes[21:21+chainidlen])
nonce := new(big.Int).SetBytes(serbytes[21+chainidlen:])
err := Dial(int(chainid.Int64()))
if err!= nil {
return fmt.Sprintf("Error connecting: %s", err)
}
client
}
*/
var apibychainid = map[int][]string{ // chainid -> [name, url]
80001: {"Polygon Testnet Mumbai", "https://matic-mumbai.chainstacklabs.com"},
1: {"Ethereum Mainnet"},
3: {"Ethereum Testnet Ropsten", "https://ropsten.infura.io/v3/9580584257984a17927f94f2dd44aa46"},
4: {"Ethereum Testnet Rinkeby"},
5: {"Ethereum Testnet Goerli", "https://goerli.infura.io/v3/9580584257984a17927f94f2dd44aa46"},
11155111: {"Ethereum Sepolia", "https://nunki.htznr.fault.dev/rpc"},
42: {"Ethereum Testnet Kovan"},
}
/*
Chain ID
Network
56
Binance Smart Chain Mainnet
97
Binance Smart Chain Testnet
137
Polygon (previously Matic) Mainnet
30
RSK Mainnet
31
RSK Testnet
16
Flare Testnet Coston
19
Songbird Mainnet
128
HECO Chain Mainnet
100
Gnosis Chain (prev. xDai Chain) Mainnet
250
Fantom Mainnet Opera
50
XinFin Network Mainnet
42220
Celo Mainnet
321
KCC Mainnet
43114
Avalanche C-Chain Mainnet
42161
Arbitrum
421611
Arbitrum Testnet Rinkeby
288
BOBA L2
66
OEC
10
Metadium Mainnet
12
Metadium Testnet
246
EWC (Energy Web Token) Mainnet
1666600000
Harmony Mainnet
1666700000
Harmony Testnet
2017
Orbit Chain Mainnet
*/