arnaucube
arnaucube/darkID-prototypePublic Self-hosted repository
darkID-prototype/clientApp/keys.go
clientApp/keys.goRaw
  1package main
  2
  3import (
  4	"crypto/rsa"
  5	"crypto/x509"
  6	"encoding/asn1"
  7	"encoding/json"
  8	"encoding/pem"
  9	"errors"
 10	"fmt"
 11	"io/ioutil"
 12	"os"
 13	"time"
 14
 15	"github.com/fatih/color"
 16)
 17
 18type Key struct {
 19	ID             string         `json:"id"`
 20	PrivK          string         `json:"privK"` //path of the PrivK file
 21	PubK           string         `json:"pubK"`  //path of the PubK file
 22	Date           time.Time      `json:"date"`
 23	Hashed         []byte         `json:"hashed"`
 24	UnblindedSig   []byte         `json:"unblindedsig"`
 25	Verified       bool           `json:"verified"`
 26	ServerVerifier *rsa.PublicKey `json:"serververifier"`
 27}
 28
 29func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string {
 30	privkey_bytes := x509.MarshalPKCS1PrivateKey(privkey)
 31	privkey_pem := pem.EncodeToMemory(
 32		&pem.Block{
 33			Type:  "RSA PRIVATE KEY",
 34			Bytes: privkey_bytes,
 35		},
 36	)
 37	return string(privkey_pem)
 38}
 39
 40func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error) {
 41	block, _ := pem.Decode([]byte(privPEM))
 42	if block == nil {
 43		return nil, errors.New("failed to parse PEM block containing the key")
 44	}
 45	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
 46	if err != nil {
 47		return nil, err
 48	}
 49	return priv, nil
 50}
 51
 52func ExportRsaPublicKeyAsPemStr(pubkey rsa.PublicKey) (string, error) {
 53	asn1Bytes, err := asn1.Marshal(pubkey)
 54	check(err)
 55	pubkey_pem := pem.EncodeToMemory(
 56		&pem.Block{
 57			Type:  "PUBLIC KEY",
 58			Bytes: asn1Bytes,
 59		},
 60	)
 61	color.Red("pubkey_pem")
 62	fmt.Println(pubkey_pem)
 63	return string(pubkey_pem), nil
 64}
 65
 66func ParseRsaPublicKeyFromPemStr(pubPEM string) (pub rsa.PublicKey, err error) {
 67	pemBlock, _ := pem.Decode([]byte(pubPEM))
 68	_, err = asn1.Unmarshal(pemBlock.Bytes, &pub)
 69	return
 70}
 71
 72func savePEMKey(fileName string, key *rsa.PrivateKey) {
 73	outFile, err := os.Create(fileName)
 74	check(err)
 75	defer outFile.Close()
 76
 77	var privateKey = &pem.Block{
 78		Type:  "PRIVATE KEY",
 79		Bytes: x509.MarshalPKCS1PrivateKey(key),
 80	}
 81
 82	err = pem.Encode(outFile, privateKey)
 83	check(err)
 84}
 85func savePublicPEMKey(fileName string, pubkey rsa.PublicKey) {
 86	asn1Bytes, err := asn1.Marshal(pubkey)
 87	check(err)
 88
 89	var pemkey = &pem.Block{
 90		Type:  "PUBLIC KEY",
 91		Bytes: asn1Bytes,
 92	}
 93
 94	pemfile, err := os.Create(fileName)
 95	check(err)
 96	defer pemfile.Close()
 97
 98	err = pem.Encode(pemfile, pemkey)
 99	check(err)
100}
101func openPEMKey(path string) (key *rsa.PrivateKey, err error) {
102	b, err := ioutil.ReadFile(path)
103	if err != nil {
104		fmt.Print(err)
105	}
106	key, err = ParseRsaPrivateKeyFromPemStr(string(b))
107	return
108}
109func openPublicPEMKey(path string) (key rsa.PublicKey, err error) {
110	b, err := ioutil.ReadFile(path)
111	if err != nil {
112		fmt.Print(err)
113	}
114	key, err = ParseRsaPublicKeyFromPemStr(string(b))
115	return
116}
117func readKeys() []Key {
118	path := keysDir + "/keys.json"
119	var keys []Key
120
121	file, err := ioutil.ReadFile(path)
122	check(err)
123	content := string(file)
124	json.Unmarshal([]byte(content), &keys)
125
126	return keys
127}
128
129func saveKeys(keys []Key) {
130	jsonKeys, err := json.Marshal(keys)
131	check(err)
132	err = ioutil.WriteFile(keysDir+"/keys.json", jsonKeys, 0644)
133	check(err)
134}
135func saveKey(k Key) {
136	fmt.Println(k)
137	keys := readKeys()
138	for i, key := range keys {
139		if key.ID == k.ID {
140			keys[i] = k
141		}
142	}
143	saveKeys(keys)
144}
145func getKeyByKeyID(keyID string) (k Key) {
146	keys := readKeys()
147	for _, key := range keys {
148		if key.ID == keyID {
149			k = key
150		}
151	}
152	return k
153}
154
155/*
156func getServerPubK(url string) ownrsa.RSAPublicKey {
157	r, err := http.Get(url + "/")
158	check(err)
159	fmt.Println(r)
160
161	decoder := json.NewDecoder(r.Body)
162	//var sigmaString string
163	var pubK ownrsa.RSAPublicKey
164	err = decoder.Decode(&pubK)
165	if err != nil {
166		panic(err)
167	}
168	defer r.Body.Close()
169	color.Blue("received server pubK:")
170	fmt.Println(pubK)
171	return pubK
172}
173*/