2015-02-17 21:43:21 +01:00
|
|
|
package pki
|
2015-02-15 21:04:59 +01:00
|
|
|
|
|
|
|
import (
|
2015-03-25 20:36:21 +01:00
|
|
|
"crypto"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2015-02-15 21:04:59 +01:00
|
|
|
)
|
|
|
|
|
2015-02-18 21:36:32 +01:00
|
|
|
const (
|
2015-03-25 20:36:21 +01:00
|
|
|
PemLabelRsa = "RSA PRIVATE KEY"
|
2015-02-18 21:36:32 +01:00
|
|
|
)
|
|
|
|
|
2015-02-15 21:04:59 +01:00
|
|
|
type (
|
2015-03-25 20:36:21 +01:00
|
|
|
RsaPrivateKey struct {
|
|
|
|
private_key *rsa.PrivateKey
|
|
|
|
}
|
2015-02-15 21:04:59 +01:00
|
|
|
|
2015-03-25 20:36:21 +01:00
|
|
|
RsaPublicKey struct {
|
|
|
|
public_key *rsa.PublicKey
|
|
|
|
}
|
2015-02-15 21:04:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// generate a new rsa private key
|
|
|
|
func NewPrivateKeyRsa(size int) (*RsaPrivateKey, error) {
|
2015-03-25 20:36:21 +01:00
|
|
|
key, err := rsa.GenerateKey(rand.Reader, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RsaPrivateKey{key}, nil
|
2015-02-15 21:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// load a rsa private key its ASN.1 presentation
|
|
|
|
func LoadPrivateKeyRsa(raw []byte) (*RsaPrivateKey, error) {
|
2015-03-25 20:36:21 +01:00
|
|
|
key, err := x509.ParsePKCS1PrivateKey(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RsaPrivateKey{key}, nil
|
2015-02-15 21:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *RsaPrivateKey) Public() PublicKey {
|
2015-03-25 20:36:21 +01:00
|
|
|
return &RsaPublicKey{pr.private_key.Public().(*rsa.PublicKey)}
|
2015-02-15 21:04:59 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 22:55:29 +01:00
|
|
|
func (pr RsaPrivateKey) Sign(message []byte, hash crypto.Hash) ([]byte, error) {
|
2015-03-25 20:36:21 +01:00
|
|
|
return make([]byte, 0), errors.New("not implemented yet!")
|
2015-02-15 21:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the private key
|
2015-03-15 20:06:09 +01:00
|
|
|
func (pr RsaPrivateKey) PrivateKey() crypto.PrivateKey {
|
2015-03-25 20:36:21 +01:00
|
|
|
return pr.private_key
|
2015-02-15 21:04:59 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 20:12:41 +01:00
|
|
|
func (pr RsaPrivateKey) MarshalPem() (io.WriterTo, error) {
|
2015-03-25 20:36:21 +01:00
|
|
|
asn1 := x509.MarshalPKCS1PrivateKey(pr.private_key)
|
|
|
|
pem_block := pem.Block{Type: PemLabelRsa, Bytes: asn1}
|
|
|
|
return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
|
2015-02-15 21:04:59 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 21:50:07 +01:00
|
|
|
// restore a rsa public key
|
2015-02-19 21:48:08 +01:00
|
|
|
func LoadPublicKeyRsa(raw []byte) (*RsaPublicKey, error) {
|
2015-03-25 20:36:21 +01:00
|
|
|
return nil, errors.New("not implemented yet!")
|
2015-02-19 21:48:08 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 21:50:07 +01:00
|
|
|
// marshal a rsa public key into pem format
|
2015-03-25 20:12:41 +01:00
|
|
|
func (pu *RsaPublicKey) MarshalPem() (io.WriterTo, error) {
|
2015-03-25 20:36:21 +01:00
|
|
|
asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1}
|
|
|
|
return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
|
2015-02-17 21:43:21 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 21:50:07 +01:00
|
|
|
// verify a message with a signature using the public key
|
2015-02-18 22:55:29 +01:00
|
|
|
func (pu *RsaPublicKey) Verify(message []byte, signature []byte, hash crypto.Hash) (bool, error) {
|
2015-03-25 20:36:21 +01:00
|
|
|
return false, errors.New("not implemented yet!")
|
2015-02-15 21:04:59 +01:00
|
|
|
}
|