2018-07-09 18:43:13 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package peertls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/asn1"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNotExist is used when a file or directory doesn't exist
|
|
|
|
ErrNotExist = errs.Class("file or directory not found error")
|
|
|
|
// ErrNoOverwrite is used when `create == true && overwrite == false`
|
|
|
|
// and tls certs/keys already exist at the specified paths
|
|
|
|
ErrNoOverwrite = errs.Class("tls overwrite disabled error")
|
|
|
|
// ErrGenerate is used when an error occured during cert/key generation
|
|
|
|
ErrGenerate = errs.Class("tls generation error")
|
|
|
|
// ErrTLSOptions is used inconsistently and should probably just be removed
|
|
|
|
ErrTLSOptions = errs.Class("tls options error")
|
|
|
|
// ErrTLSTemplate is used when an error occurs during tls template generation
|
|
|
|
ErrTLSTemplate = errs.Class("tls template error")
|
|
|
|
// ErrVerifyPeerCert is used when an error occurs during `VerifyPeerCertificate`
|
|
|
|
ErrVerifyPeerCert = errs.Class("tls peer certificate verification error")
|
|
|
|
// ErrVerifySignature is used when a cert-chain signature verificaion error occurs
|
|
|
|
ErrVerifySignature = errs.Class("tls certificate signature verification error")
|
|
|
|
)
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
// IsNotExist checks that a file or directory does not exist
|
2018-07-09 18:43:13 +01:00
|
|
|
func IsNotExist(err error) bool {
|
|
|
|
return os.IsNotExist(err) || ErrNotExist.Has(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSFileOptions stores information about a tls certificate and key, and options for use with tls helper functions/methods
|
|
|
|
type TLSFileOptions struct {
|
|
|
|
RootCertRelPath string
|
|
|
|
RootCertAbsPath string
|
|
|
|
LeafCertRelPath string
|
|
|
|
LeafCertAbsPath string
|
|
|
|
// NB: Populate absolute paths from relative paths,
|
|
|
|
// with respect to pwd via `.EnsureAbsPaths`
|
|
|
|
RootKeyRelPath string
|
|
|
|
RootKeyAbsPath string
|
|
|
|
LeafKeyRelPath string
|
|
|
|
LeafKeyAbsPath string
|
|
|
|
LeafCertificate *tls.Certificate
|
|
|
|
// Create if cert or key nonexistent
|
|
|
|
Create bool
|
|
|
|
// Overwrite if `create` is true and cert and/or key exist
|
|
|
|
Overwrite bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ecdsaSignature struct {
|
|
|
|
R, S *big.Int
|
|
|
|
}
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
// VerifyPeerCertificate verifies that the provided raw certificates are valid
|
2018-07-09 18:43:13 +01:00
|
|
|
func VerifyPeerCertificate(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
|
|
|
// Verify parent ID/sig
|
|
|
|
// Verify leaf ID/sig
|
|
|
|
// Verify leaf signed by parent
|
|
|
|
|
|
|
|
// TODO(bryanchriswhite): see "S/Kademlia extensions - Secure nodeId generation"
|
|
|
|
// (https://www.pivotaltracker.com/story/show/158238535)
|
|
|
|
|
|
|
|
for i, cert := range rawCerts {
|
|
|
|
isValid := false
|
|
|
|
|
|
|
|
if i < len(rawCerts)-1 {
|
|
|
|
parentCert, err := x509.ParseCertificate(rawCerts[i+1])
|
|
|
|
if err != nil {
|
|
|
|
return ErrVerifyPeerCert.New("unable to parse certificate", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
childCert, err := x509.ParseCertificate(cert)
|
|
|
|
if err != nil {
|
|
|
|
return ErrVerifyPeerCert.New("unable to parse certificate", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid, err = verifyCertSignature(parentCert, childCert)
|
|
|
|
if err != nil {
|
|
|
|
return ErrVerifyPeerCert.Wrap(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rootCert, err := x509.ParseCertificate(cert)
|
|
|
|
if err != nil {
|
|
|
|
return ErrVerifyPeerCert.New("unable to parse certificate", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid, err = verifyCertSignature(rootCert, rootCert)
|
|
|
|
if err != nil {
|
|
|
|
return ErrVerifyPeerCert.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isValid {
|
|
|
|
return ErrVerifyPeerCert.New("certificate chain signature verification failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTLSFileOptions initializes a new `TLSFileOption` struct given the arguments
|
|
|
|
func NewTLSFileOptions(baseCertPath, baseKeyPath string, create, overwrite bool) (_ *TLSFileOptions, _ error) {
|
|
|
|
t := &TLSFileOptions{
|
|
|
|
RootCertRelPath: fmt.Sprintf("%s.root.cert", baseCertPath),
|
|
|
|
RootKeyRelPath: fmt.Sprintf("%s.root.key", baseKeyPath),
|
|
|
|
LeafCertRelPath: fmt.Sprintf("%s.leaf.cert", baseCertPath),
|
|
|
|
LeafKeyRelPath: fmt.Sprintf("%s.leaf.key", baseKeyPath),
|
|
|
|
Overwrite: overwrite,
|
|
|
|
Create: create,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.EnsureExists(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyCertSignature(parentCert, childCert *x509.Certificate) (bool, error) {
|
|
|
|
pubkey := parentCert.PublicKey.(*ecdsa.PublicKey)
|
|
|
|
signature := new(ecdsaSignature)
|
|
|
|
|
|
|
|
if _, err := asn1.Unmarshal(childCert.Signature, signature); err != nil {
|
|
|
|
return false, ErrVerifySignature.New("unable to unmarshal ecdsa signature", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := crypto.SHA256.New()
|
captplanet (#159)
* captplanet
I kind of went overboard this weekend.
The major goal of this changeset is to provide an environment
for local development where all of the various services can
be easily run together. Developing on Storj v3 should be as
easy as running a setup command and a run command!
To do this, this changeset introduces a new tool called
captplanet, which combines the powers of the Overlay Cache,
the PointerDB, the PieceStore, Kademlia, the Minio Gateway,
etc.
Running 40 farmers and a heavy client inside the same process
forced a rethinking of the "services" that we had. To
avoid confusion by reusing prior terms, this changeset
introduces two new types: Providers and Responsibilities.
I wanted to avoid as many merge conflicts as possible, so
I left the existing Services and code for now, but if people
like this route we can clean up the duplication.
A Responsibility is a collection of gRPC methods and
corresponding state. The following systems are examples of
Responsibilities:
* Kademlia
* OverlayCache
* PointerDB
* StatDB
* PieceStore
* etc.
A Provider is a collection of Responsibilities that
share an Identity, such as:
* The heavy client
* The farmer
* The gateway
An Identity is a public/private key pair, a node id, etc.
Farmers all need different Identities, so captplanet
needs to support running multiple concurrent Providers
with different Identities.
Each Responsibility and Provider should allow for configuration
of multiple copies on its own so creating Responsibilities and
Providers use a new workflow.
To make a Responsibility, one should create a "config"
struct, such as:
```
type Config struct {
RepairThreshold int `help:"If redundancy falls below this number of
pieces, repair is triggered" default:"30"`
SuccessThreshold int `help:"If redundancy is above this number then
no additional uploads are needed" default:"40"`
}
```
To use "config" structs, this changeset introduces another
new library called 'cfgstruct', which allows for the configuration
of arbitrary structs through flagsets, and thus through cobra and
viper.
cfgstruct relies on Go's "struct tags" feature to document
help information and default values. Config structs can be
configured via cfgstruct.Bind for binding the struct to a flagset.
Because this configuration system makes setup and configuration
easier *in general*, additional commands are provided that allow
for easy standup of separate Providers. Please make sure to
check out:
* cmd/captplanet/farmer/main.go (a new farmer binary)
* cmd/captplanet/hc/main.go (a new heavy client binary)
* cmd/captplanet/gw/main.go (a new minio gateway binary)
Usage:
```
$ go install -v storj.io/storj/cmd/captplanet
$ captplanet setup
$ captplanet run
```
Configuration is placed by default in `~/.storj/capt/`
Other changes:
* introduces new config structs for currently existing
Responsibilities that conform to the new Responsibility
interface. Please see the `pkg/*/config.go` files for
examples.
* integrates the PointerDB API key with other global
configuration via flags, instead of through environment
variables through viper like it's been doing. (ultimately
this should also change to use the PointerDB config
struct but this is an okay shortterm solution).
* changes the Overlay cache to use a URL for database
configuration instead of separate redis and bolt config
settings.
* stubs out some peer identity skeleton code (but not the
meat).
* Fixes the SegmentStore to use the overlay client and
pointerdb clients instead of gRPC client code directly
* Leaves a very clear spot where we need to tie the object to
stream to segment store together. There's sort of a "golden
spike" opportunity to connect all the train tracks together
at the bottom of pkg/miniogw/config.go, labeled with a
bunch of TODOs.
Future stuff:
* I now prefer this design over the original
pkg/process.Service thing I had been pushing before (sorry!)
* The experience of trying to have multiple farmers
configurable concurrently led me to prefer config structs
over global flags (I finally came around) or using viper
directly. I think global flags are okay sometimes but in
general going forward we should try and get all relevant
config into config structs.
* If you all like this direction, I think we can go delete my
old Service interfaces and a bunch of flags and clean up a
bunch of stuff.
* If you don't like this direction, it's no sweat at all, and
despite how much code there is here I'm not very tied to any
of this! Considering a lot of this was written between midnight
and 6 am, it might not be any good!
* bind tests
2018-07-24 17:08:28 +01:00
|
|
|
_, err := h.Write(childCert.RawTBSCertificate)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-07-09 18:43:13 +01:00
|
|
|
digest := h.Sum(nil)
|
|
|
|
|
|
|
|
isValid := ecdsa.Verify(pubkey, digest, signature.R, signature.S)
|
|
|
|
|
|
|
|
return isValid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// * Copyright 2017 gRPC authors.
|
|
|
|
// * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// * (see https://github.com/grpc/grpc-go/blob/v1.13.0/credentials/credentials_util_go18.go)
|
|
|
|
// cloneTLSConfig returns a shallow clone of the exported
|
|
|
|
// fields of cfg, ignoring the unexported sync.Once, which
|
|
|
|
// contains a mutex and must not be copied.
|
|
|
|
//
|
|
|
|
// If cfg is nil, a new zero tls.Config is returned.
|
|
|
|
func cloneTLSConfig(cfg *tls.Config) *tls.Config {
|
|
|
|
if cfg == nil {
|
|
|
|
return &tls.Config{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.Clone()
|
|
|
|
}
|