github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/crypto/keys/der/proto/register.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package proto registers a DER keys.ProtoHandler using keys.RegisterHandler.
    16  // This handler will extract a crypto.Signer from a keyspb.PrivateKey protobuf message.
    17  package proto
    18  
    19  import (
    20  	"context"
    21  	"crypto"
    22  	"fmt"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  	"github.com/google/trillian/crypto/keys"
    26  	"github.com/google/trillian/crypto/keys/der"
    27  	"github.com/google/trillian/crypto/keyspb"
    28  )
    29  
    30  func init() {
    31  	keys.RegisterHandler(&keyspb.PrivateKey{}, func(ctx context.Context, pb proto.Message) (crypto.Signer, error) {
    32  		if pb, ok := pb.(*keyspb.PrivateKey); ok {
    33  			return der.FromProto(pb)
    34  		}
    35  		return nil, fmt.Errorf("der: got %T, want *keyspb.PrivateKey", pb)
    36  	})
    37  }