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
|
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package crypto
import (
"crypto"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"net"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/crypto/rand"
"software.sslmate.com/src/go-pkcs12"
)
// RSAKeyLength define the length of RSA keys
const RSAKeyLength = 2048
// RSAPair generate a pair of rsa keys
func RSAPair() (*RSAPrivKey, error) {
key, err := rsa.GenerateKey(rand.Reader, RSAKeyLength)
if err != nil {
return nil, err
}
return &RSAPrivKey{key}, nil
}
// RSAPubKey represents the public key of RSA
type RSAPubKey struct {
key *rsa.PublicKey
}
// Type returns the type of the key, e.g. RSA
func (k *RSAPubKey) Type() string {
return KeyTypeRSA
}
// Scheme returns the scheme of signature algorithm, e.g. rsassa-pss-sha256
func (k *RSAPubKey) Scheme() string {
return KeySchemeRSASSAPSSSHA256
}
// Key returns the raw public key
func (k *RSAPubKey) Key() crypto.PublicKey {
return k.key
}
// Serialize generate the pem format for a key
func (k *RSAPubKey) Serialize() ([]byte, error) {
asn1Bytes, err := x509.MarshalPKIXPublicKey(k.key)
if err != nil {
return nil, err
}
pemKey := &pem.Block{
Type: "PUBLIC KEY",
Bytes: asn1Bytes,
}
return pem.EncodeToMemory(pemKey), nil
}
// Deserialize generate a public key from pem format
func (k *RSAPubKey) Deserialize(key []byte) error {
block, _ := pem.Decode(key)
if block == nil {
return ErrorDeserializeKey
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
k.key = pubInterface.(*rsa.PublicKey)
return nil
}
// VerifySignature check the signature is right
func (k *RSAPubKey) VerifySignature(payload []byte, sig string) error {
if k.key == nil {
return ErrorKeyUninitialized
}
sha256 := crypto.SHA256.New()
_, err := sha256.Write(payload)
if err != nil {
return errors.AddStack(err)
}
hashed := sha256.Sum(nil)
b64decSig, err := base64.StdEncoding.DecodeString(sig)
if err != nil {
return err
}
return rsa.VerifyPSS(k.key, crypto.SHA256, hashed, b64decSig, nil)
}
// RSAPrivKey represents the private key of RSA
type RSAPrivKey struct {
key *rsa.PrivateKey
}
// Type returns the type of the key, e.g. RSA
func (k *RSAPrivKey) Type() string {
return KeyTypeRSA
}
// Scheme returns the scheme of signature algorithm, e.g. rsassa-pss-sha256
func (k *RSAPrivKey) Scheme() string {
return KeySchemeRSASSAPSSSHA256
}
// Serialize generate the pem format for a key
func (k *RSAPrivKey) Serialize() ([]byte, error) {
pemKey := &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(k.key),
}
return pem.EncodeToMemory(pemKey), nil
}
// Deserialize generate a private key from pem format
func (k *RSAPrivKey) Deserialize(key []byte) error {
block, _ := pem.Decode(key)
if block == nil {
return ErrorDeserializeKey
}
privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return err
}
k.key = privKey
return nil
}
// Signature sign a signature with the key for payload
func (k *RSAPrivKey) Signature(payload []byte) (string, error) {
if k.key == nil {
return "", ErrorKeyUninitialized
}
sha256 := crypto.SHA256.New()
_, err := sha256.Write(payload)
if err != nil {
return "", errors.AddStack(err)
}
hashed := sha256.Sum(nil)
sig, err := rsa.SignPSS(rand.Reader, k.key, crypto.SHA256, hashed, nil)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(sig), nil
}
// Public returns public key of the PrivKey
func (k *RSAPrivKey) Public() PubKey {
return &RSAPubKey{
key: &k.key.PublicKey,
}
}
// Signer returns the signer of the private key
func (k *RSAPrivKey) Signer() crypto.Signer {
return k.key
}
// Pem returns the raw private key im PEM format
func (k *RSAPrivKey) Pem() []byte {
return pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(k.key),
})
}
// CSR generates a new CSR from given private key
func (k *RSAPrivKey) CSR(role, commonName string, hostList, ipList []string) ([]byte, error) {
var ipAddrList []net.IP
for _, ip := range ipList {
ipAddr := net.ParseIP(ip)
ipAddrList = append(ipAddrList, ipAddr)
}
// set CSR attributes
csrTemplate := &x509.CertificateRequest{
Subject: pkix.Name{
Organization: []string{pkixOrganization},
OrganizationalUnit: []string{pkixOrganizationalUnit, role},
CommonName: commonName,
},
DNSNames: hostList,
IPAddresses: ipAddrList,
}
csr, err := x509.CreateCertificateRequest(rand.Reader, csrTemplate, k.key)
if err != nil {
return nil, err
}
return csr, nil
}
// PKCS12 encodes the private and certificate to a PKCS#12 pfxData
func (k *RSAPrivKey) PKCS12(cert *x509.Certificate, ca *CertificateAuthority) ([]byte, error) {
return pkcs12.Encode(
rand.Reader,
k.key,
cert,
[]*x509.Certificate{ca.Cert},
PKCS12Password,
)
}
|