2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google 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,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2010-12-09 03:24:48 +00:00
|
|
|
package jsonsign
|
|
|
|
|
|
|
|
import (
|
2011-05-16 16:01:35 +00:00
|
|
|
"bytes"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2010-12-09 03:24:48 +00:00
|
|
|
"fmt"
|
2011-05-04 00:34:16 +00:00
|
|
|
"io"
|
2010-12-09 03:24:48 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2011-05-16 21:35:38 +00:00
|
|
|
"sync"
|
2012-07-28 23:32:31 +00:00
|
|
|
"time"
|
2010-12-09 03:24:48 +00:00
|
|
|
"unicode"
|
2011-05-04 00:34:16 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-12-10 16:52:52 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/third_party/code.google.com/p/go.crypto/openpgp"
|
2014-02-09 00:04:14 +00:00
|
|
|
"camlistore.org/third_party/code.google.com/p/go.crypto/openpgp/packet"
|
2010-12-09 03:24:48 +00:00
|
|
|
)
|
|
|
|
|
2011-05-16 21:35:38 +00:00
|
|
|
type EntityFetcher interface {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
FetchEntity(keyId string) (*openpgp.Entity, error)
|
2011-05-16 21:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileEntityFetcher struct {
|
|
|
|
File string
|
|
|
|
}
|
|
|
|
|
|
|
|
func FlagEntityFetcher() *FileEntityFetcher {
|
2013-12-10 16:52:52 +00:00
|
|
|
return &FileEntityFetcher{File: osutil.IdentitySecretRing()}
|
2011-05-16 21:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CachingEntityFetcher struct {
|
|
|
|
Fetcher EntityFetcher
|
|
|
|
|
|
|
|
lk sync.Mutex
|
|
|
|
m map[string]*openpgp.Entity
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ce *CachingEntityFetcher) FetchEntity(keyId string) (*openpgp.Entity, error) {
|
2011-05-16 21:35:38 +00:00
|
|
|
ce.lk.Lock()
|
|
|
|
if ce.m != nil {
|
|
|
|
e := ce.m[keyId]
|
|
|
|
if e != nil {
|
|
|
|
ce.lk.Unlock()
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ce.lk.Unlock()
|
|
|
|
|
|
|
|
e, err := ce.Fetcher.FetchEntity(keyId)
|
|
|
|
if err == nil {
|
|
|
|
ce.lk.Lock()
|
|
|
|
defer ce.lk.Unlock()
|
|
|
|
if ce.m == nil {
|
|
|
|
ce.m = make(map[string]*openpgp.Entity)
|
|
|
|
}
|
|
|
|
ce.m[keyId] = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (fe *FileEntityFetcher) FetchEntity(keyId string) (*openpgp.Entity, error) {
|
2011-05-16 21:35:38 +00:00
|
|
|
f, err := os.Open(fe.File)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("jsonsign: FetchEntity: %v", err)
|
|
|
|
}
|
2011-05-18 03:25:00 +00:00
|
|
|
defer f.Close()
|
2011-05-16 21:35:38 +00:00
|
|
|
el, err := openpgp.ReadKeyRing(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("jsonsign: openpgp.ReadKeyRing of %q: %v", fe.File, err)
|
|
|
|
}
|
|
|
|
for _, e := range el {
|
2011-05-18 03:25:00 +00:00
|
|
|
pubk := &e.PrivateKey.PublicKey
|
|
|
|
if pubk.KeyIdString() != keyId {
|
2011-05-16 21:35:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e.PrivateKey.Encrypted {
|
2011-05-18 03:25:00 +00:00
|
|
|
if err := fe.decryptEntity(e); err == nil {
|
|
|
|
return e, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
2011-05-16 21:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("jsonsign: entity for keyid %q not found in %q", keyId, fe.File)
|
2011-05-10 12:00:53 +00:00
|
|
|
}
|
2010-12-09 03:24:48 +00:00
|
|
|
|
|
|
|
type SignRequest struct {
|
2012-07-28 22:42:56 +00:00
|
|
|
UnsignedJSON string
|
2013-08-04 02:54:30 +00:00
|
|
|
Fetcher interface{} // blobref.Fetcher or blob.StreamingFetcher
|
2011-05-16 21:35:38 +00:00
|
|
|
ServerMode bool // if true, can't use pinentry or gpg-agent, etc.
|
|
|
|
|
2012-07-28 23:32:31 +00:00
|
|
|
// Optional signature time. If zero, time.Now() is used.
|
|
|
|
SignatureTime time.Time
|
|
|
|
|
2011-05-16 21:35:38 +00:00
|
|
|
// Optional function to return an entity (including decrypting
|
|
|
|
// the PrivateKey, if necessary)
|
|
|
|
EntityFetcher EntityFetcher
|
2011-03-15 16:55:10 +00:00
|
|
|
|
2011-05-16 21:35:38 +00:00
|
|
|
// SecretKeyringPath is only used if EntityFetcher is nil,
|
|
|
|
// in which case SecretKeyringPath is used if non-empty.
|
2014-02-08 23:15:23 +00:00
|
|
|
// As a final resort, we default to osutil.IdentitySecretRing().
|
2011-05-16 21:35:38 +00:00
|
|
|
SecretKeyringPath string
|
2011-03-15 16:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *SignRequest) secretRingPath() string {
|
|
|
|
if sr.SecretKeyringPath != "" {
|
|
|
|
return sr.SecretKeyringPath
|
|
|
|
}
|
2013-12-10 16:52:52 +00:00
|
|
|
return osutil.IdentitySecretRing()
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|
|
|
|
|
2012-07-28 22:42:56 +00:00
|
|
|
func (sr *SignRequest) Sign() (signedJSON string, err error) {
|
|
|
|
trimmedJSON := strings.TrimRightFunc(sr.UnsignedJSON, unicode.IsSpace)
|
2010-12-09 03:24:48 +00:00
|
|
|
|
|
|
|
// TODO: make sure these return different things
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
inputfail := func(msg string) (string, error) {
|
|
|
|
return "", errors.New(msg)
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
execfail := func(msg string) (string, error) {
|
|
|
|
return "", errors.New(msg)
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jmap := make(map[string]interface{})
|
2012-07-28 22:42:56 +00:00
|
|
|
if err := json.Unmarshal([]byte(trimmedJSON), &jmap); err != nil {
|
2010-12-09 03:24:48 +00:00
|
|
|
return inputfail("json parse error")
|
|
|
|
}
|
|
|
|
|
|
|
|
camliSigner, hasSigner := jmap["camliSigner"]
|
|
|
|
if !hasSigner {
|
|
|
|
return inputfail("json lacks \"camliSigner\" key with public key blobref")
|
|
|
|
}
|
|
|
|
|
2011-03-15 05:51:36 +00:00
|
|
|
camliSignerStr, _ := camliSigner.(string)
|
2013-08-04 02:54:30 +00:00
|
|
|
signerBlob, ok := blob.Parse(camliSignerStr)
|
|
|
|
if !ok {
|
2010-12-09 03:24:48 +00:00
|
|
|
return inputfail("json \"camliSigner\" key is malformed or unsupported")
|
|
|
|
}
|
|
|
|
|
2011-05-04 00:34:16 +00:00
|
|
|
var pubkeyReader io.ReadCloser
|
|
|
|
switch fetcher := sr.Fetcher.(type) {
|
2013-08-04 02:54:30 +00:00
|
|
|
case blob.SeekFetcher:
|
2011-05-04 00:34:16 +00:00
|
|
|
pubkeyReader, _, err = fetcher.Fetch(signerBlob)
|
2013-08-04 02:54:30 +00:00
|
|
|
case blob.StreamingFetcher:
|
2011-05-04 00:34:16 +00:00
|
|
|
pubkeyReader, _, err = fetcher.FetchStreaming(signerBlob)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("jsonsign: bogus SignRequest.Fetcher of type %T", sr.Fetcher))
|
|
|
|
}
|
2010-12-09 03:24:48 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO: not really either an inputfail or an execfail.. but going
|
|
|
|
// with exec for now.
|
2013-08-23 19:30:01 +00:00
|
|
|
return execfail(fmt.Sprintf("failed to find public key %s: %v", signerBlob.String(), err))
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 16:01:35 +00:00
|
|
|
pubk, err := openArmoredPublicKeyFile(pubkeyReader)
|
2013-10-22 03:39:08 +00:00
|
|
|
pubkeyReader.Close()
|
2010-12-09 03:24:48 +00:00
|
|
|
if err != nil {
|
2011-01-17 05:16:54 +00:00
|
|
|
return execfail(fmt.Sprintf("failed to parse public key from blobref %s: %v", signerBlob.String(), err))
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This check should be redundant if the above JSON parse succeeded, but
|
|
|
|
// for explicitness...
|
2012-07-28 22:42:56 +00:00
|
|
|
if len(trimmedJSON) == 0 || trimmedJSON[len(trimmedJSON)-1] != '}' {
|
2010-12-09 03:24:48 +00:00
|
|
|
return inputfail("json parameter lacks trailing '}'")
|
|
|
|
}
|
2012-07-28 22:42:56 +00:00
|
|
|
trimmedJSON = trimmedJSON[0 : len(trimmedJSON)-1]
|
2010-12-09 03:24:48 +00:00
|
|
|
|
2011-05-16 16:01:35 +00:00
|
|
|
// sign it
|
2011-05-16 21:35:38 +00:00
|
|
|
entityFetcher := sr.EntityFetcher
|
|
|
|
if entityFetcher == nil {
|
2011-10-13 05:59:47 +00:00
|
|
|
file := sr.secretRingPath()
|
2011-05-16 21:35:38 +00:00
|
|
|
if file == "" {
|
2014-02-08 23:15:23 +00:00
|
|
|
return "", errors.New("jsonsign: no EntityFetcher, and no secret ring file defined.")
|
2011-05-16 16:01:35 +00:00
|
|
|
}
|
2011-10-13 05:59:47 +00:00
|
|
|
secring, err := os.Open(sr.secretRingPath())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("jsonsign: failed to open secret ring file %q: %v", sr.secretRingPath(), err)
|
|
|
|
}
|
|
|
|
secring.Close() // just opened to see if it's readable
|
2011-05-16 21:35:38 +00:00
|
|
|
entityFetcher = &FileEntityFetcher{File: file}
|
|
|
|
}
|
|
|
|
signer, err := entityFetcher.FetchEntity(pubk.KeyIdString())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 16:01:35 +00:00
|
|
|
var buf bytes.Buffer
|
2014-02-09 00:04:14 +00:00
|
|
|
err = openpgp.ArmoredDetachSign(
|
|
|
|
&buf,
|
|
|
|
signer,
|
|
|
|
strings.NewReader(trimmedJSON),
|
|
|
|
&packet.Config{Time: func() time.Time { return sr.SignatureTime }},
|
|
|
|
)
|
2011-05-16 16:01:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-16 16:01:35 +00:00
|
|
|
output := buf.String()
|
2010-12-09 03:24:48 +00:00
|
|
|
|
|
|
|
index1 := strings.Index(output, "\n\n")
|
|
|
|
index2 := strings.Index(output, "\n-----")
|
2011-03-16 01:01:04 +00:00
|
|
|
if index1 == -1 || index2 == -1 {
|
2010-12-09 03:24:48 +00:00
|
|
|
return execfail("Failed to parse signature from gpg.")
|
|
|
|
}
|
2011-03-16 01:01:04 +00:00
|
|
|
inner := output[index1+2 : index2]
|
2010-12-09 03:24:48 +00:00
|
|
|
signature := strings.Replace(inner, "\n", "", -1)
|
|
|
|
|
2012-07-28 22:42:56 +00:00
|
|
|
return fmt.Sprintf("%s,\"camliSig\":\"%s\"}\n", trimmedJSON, signature), nil
|
2010-12-09 03:24:48 +00:00
|
|
|
}
|