Compare commits
2 Commits
e95929ed26
...
8726fcfa3a
Author | SHA1 | Date |
---|---|---|
Gibheer | 8726fcfa3a | |
Gibheer | 9fcfb1009c |
|
@ -29,6 +29,7 @@ func TestCertificateCreation(t *testing.T) {
|
||||||
cert_opts := CertificateOptions{
|
cert_opts := CertificateOptions{
|
||||||
// KeyUsage: x509.KeyUsageEncipherOnly | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign,
|
// KeyUsage: x509.KeyUsageEncipherOnly | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign,
|
||||||
SerialNumber: big.NewInt(1),
|
SerialNumber: big.NewInt(1),
|
||||||
|
CALength: -1,
|
||||||
}
|
}
|
||||||
|
|
||||||
cert, err := csr.ToCertificate(pk, cert_opts, nil)
|
cert, err := csr.ToCertificate(pk, cert_opts, nil)
|
||||||
|
@ -42,6 +43,9 @@ func TestCertificateCreation(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func fieldsAreSame(data CertificateData, cert *Certificate) bool {
|
func fieldsAreSame(data CertificateData, cert *Certificate) bool {
|
||||||
|
if cert == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if data.Subject.CommonName != cert.Subject.CommonName {
|
if data.Subject.CommonName != cert.Subject.CommonName {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
28
ed25519.go
28
ed25519.go
|
@ -3,12 +3,12 @@ package pki
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto"
|
"crypto"
|
||||||
|
"crypto/ed25519"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/agl/ed25519"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -17,11 +17,11 @@ const (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Ed25519PrivateKey struct {
|
Ed25519PrivateKey struct {
|
||||||
private_key [ed25519.PrivateKeySize]byte
|
private_key ed25519.PrivateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
Ed25519PublicKey struct {
|
Ed25519PublicKey struct {
|
||||||
public_key [ed25519.PublicKeySize]byte
|
public_key ed25519.PublicKey
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,16 +31,16 @@ func NewPrivateKeyEd25519() (*Ed25519PrivateKey, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Ed25519PrivateKey{*pr_raw}, nil
|
return &Ed25519PrivateKey{pr_raw}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore an ed25519 private key from a raw byte stream.
|
// Restore an ed25519 private key from a raw byte stream.
|
||||||
// TODO does this have to be asn1? all other functions expect asn1
|
// TODO does this have to be asn1? all other functions expect asn1
|
||||||
func LoadPrivateKeyEd25519(raw []byte) (*Ed25519PrivateKey, error) {
|
func LoadPrivateKeyEd25519(raw []byte) (*Ed25519PrivateKey, error) {
|
||||||
var pr_loaded [ed25519.PrivateKeySize]byte
|
pr_loaded := make([]byte, ed25519.PrivateKeySize)
|
||||||
length := copy(pr_loaded[:], raw)
|
length := copy(pr_loaded, raw)
|
||||||
if length != ed25519.PrivateKeySize {
|
if length != ed25519.PrivateKeySize {
|
||||||
return nil, errors.New("private key length incorrect")
|
return nil, fmt.Errorf("private key length incorrect - got: %d - expected: %d", length, ed25519.PrivateKeySize)
|
||||||
}
|
}
|
||||||
return &Ed25519PrivateKey{pr_loaded}, nil
|
return &Ed25519PrivateKey{pr_loaded}, nil
|
||||||
}
|
}
|
||||||
|
@ -57,14 +57,14 @@ func (pr *Ed25519PrivateKey) Public() PublicKey {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &Ed25519PublicKey{*pu_raw}
|
return &Ed25519PublicKey{pu_raw}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash the message given the hash algorythm and sign the hash using the private key.
|
// Hash the message given the hash algorythm and sign the hash using the private key.
|
||||||
func (pr *Ed25519PrivateKey) Sign(message []byte, hash crypto.Hash) ([]byte, error) {
|
func (pr *Ed25519PrivateKey) Sign(message []byte, hash crypto.Hash) ([]byte, error) {
|
||||||
hashed_message := hash.New()
|
hashed_message := hash.New()
|
||||||
hashed_message.Write(message)
|
hashed_message.Write(message)
|
||||||
result := ed25519.Sign(&pr.private_key, hashed_message.Sum(nil))[:]
|
result := ed25519.Sign(pr.private_key, hashed_message.Sum(nil))[:]
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,8 +84,8 @@ func (pr Ed25519PrivateKey) ToPem() (pem.Block, error) {
|
||||||
// Load the public key from a raw byte stream.
|
// Load the public key from a raw byte stream.
|
||||||
// TODO should this be read from ASN.1? All other functions do that.
|
// TODO should this be read from ASN.1? All other functions do that.
|
||||||
func LoadPublicKeyEd25519(raw []byte) (*Ed25519PublicKey, error) {
|
func LoadPublicKeyEd25519(raw []byte) (*Ed25519PublicKey, error) {
|
||||||
var pu_loaded [ed25519.PublicKeySize]byte
|
pu_loaded := make([]byte, ed25519.PublicKeySize)
|
||||||
length := copy(pu_loaded[:], raw)
|
length := copy(pu_loaded, raw)
|
||||||
if length != ed25519.PublicKeySize {
|
if length != ed25519.PublicKeySize {
|
||||||
return nil, errors.New("public key length incorrect")
|
return nil, errors.New("public key length incorrect")
|
||||||
}
|
}
|
||||||
|
@ -108,12 +108,12 @@ func (pu Ed25519PublicKey) MarshalPem() (io.WriterTo, error) {
|
||||||
|
|
||||||
// Hash the message with the hash algorythm and check the signature against the result.
|
// Hash the message with the hash algorythm and check the signature against the result.
|
||||||
func (pu Ed25519PublicKey) Verify(message []byte, signature []byte, hash crypto.Hash) (bool, error) {
|
func (pu Ed25519PublicKey) Verify(message []byte, signature []byte, hash crypto.Hash) (bool, error) {
|
||||||
var sig [ed25519.SignatureSize]byte
|
sig := make([]byte, ed25519.SignatureSize)
|
||||||
length := copy(sig[:], signature)
|
length := copy(sig[:], signature)
|
||||||
if length != ed25519.SignatureSize {
|
if length != ed25519.SignatureSize {
|
||||||
return false, errors.New("signature does not fit length")
|
return false, errors.New("signature does not fit length")
|
||||||
}
|
}
|
||||||
hashed_message := hash.New()
|
hashed_message := hash.New()
|
||||||
hashed_message.Write(message)
|
hashed_message.Write(message)
|
||||||
return ed25519.Verify(&pu.public_key, hashed_message.Sum(nil), &sig), nil
|
return ed25519.Verify(pu.public_key, hashed_message.Sum(nil), sig), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue