-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypto.go
166 lines (154 loc) · 4.31 KB
/
crypto.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
/* ****************************************************************************
* Copyright 2020 51 Degrees Mobile Experts Limited (51degrees.com)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* ***************************************************************************/
package owid
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"math/big"
)
/**
* All the public and support methods associated with the signing.
* Nothing to do with the web or HTTP.
*/
// Crypto structure containing the public and private keys
type Crypto struct {
publicKey *ecdsa.PublicKey
privateKey *ecdsa.PrivateKey
}
// NewCrypto creates an new instance of the Crypto structure and generates
// a public / private key pair used to sign and verify OWIDs
func NewCrypto() (*Crypto, error) {
var c Crypto
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
c.publicKey = &k.PublicKey
c.privateKey = k
return &c, nil
}
// NewCryptoSignOnly creates a new instance of the Crypto structure for signing
// OWIDs only from the PEM provided.
// privatePem PEM format non password protected ECDSA private PEM key.
func NewCryptoSignOnly(privatePem string) (*Crypto, error) {
var c Crypto
block, _ := pem.Decode([]byte(privatePem))
if block == nil {
return nil, fmt.Errorf("not a valid PEM key")
}
privateKey, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, err
}
c.privateKey = privateKey
return &c, nil
}
// NewCryptoVerifyOnly creates a new instance of the Crypto structure
// for Verifying OWIDs only from the PEM key.
// publicPemKey PEM format ECDSA public PEM key.
func NewCryptoVerifyOnly(publicPemKey string) (*Crypto, error) {
var c Crypto
block, _ := pem.Decode([]byte(publicPemKey))
if block == nil {
return nil, fmt.Errorf("not a valid PEM key")
}
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
c.publicKey = publicKey.(*ecdsa.PublicKey)
return &c, nil
}
// SignByteArray signs the byte array with the private key of the crypto
// provider.
func (c *Crypto) SignByteArray(data []byte) ([]byte, error) {
if c.privateKey == nil && c.publicKey != nil {
return nil, errors.New(
"instance of Crypto cannot be used to generate a signature")
}
h := sha256.Sum256(data)
r, s, err := ecdsa.Sign(
rand.Reader,
c.privateKey,
h[:])
if err != nil {
return nil, err
}
signature := make([]byte, signatureLength)
for i, b := range r.Bytes() {
signature[i] = b
}
for i, b := range s.Bytes() {
signature[i+halfSignatureLength] = b
}
return signature, nil
}
// VerifyByteArray returns true if the signature is valid for the data.
func (c *Crypto) VerifyByteArray(data []byte, sig []byte) (bool, error) {
if c.publicKey == nil {
return false, errors.New(
"instance of Crypto cannot be used to verify a signature")
}
h := sha256.Sum256(data)
var r, s big.Int
r.SetBytes(sig[:32])
s.SetBytes(sig[32:])
return ecdsa.Verify(
c.publicKey,
h[:],
&r,
&s), nil
}
// getSubjectPublicKeyInfo returns the public key in SPKI format for use with
// JavaScript SubtleCrypto.importKey() method or other methods that require
// SPKI format public keys.
func (c *Crypto) getSubjectPublicKeyInfo() (string, error) {
spki, err := x509.MarshalPKIXPublicKey(c.publicKey)
if err != nil {
return "", err
}
return string(
pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: spki,
},
),
), nil
}
func (c Crypto) publicKeyToPemString() (string, error) {
return c.getSubjectPublicKeyInfo()
}
func (c Crypto) privateKeyToPemString() (string, error) {
k, err := x509.MarshalECPrivateKey(c.privateKey)
if err != nil {
return "", err
}
return string(
pem.EncodeToMemory(
&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: k,
},
),
), nil
}