github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/newfileproxy/key_util/keyUtil.go (about)

     1  // Copyright (c) 2014, 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  //     http://www.apache.org/licenses/LICENSE-2.0
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // File: keyUtil.go
    14  
    15  package main
    16  
    17  import (
    18  	"crypto/x509"
    19  	"flag"
    20  	"fmt"
    21  	"io/ioutil"
    22  	"path"
    23  	"strconv"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  	"github.com/jlmucb/cloudproxy/go/apps/newfileproxy/common"
    27  	"github.com/jlmucb/cloudproxy/go/tao"
    28  )
    29  
    30  var configPath = flag.String("configPath", "/Domains/domain.fileproxy/tao.config", "The Tao domain config")
    31  var domainPass = flag.String("password", "xxx", "The domain password")
    32  var keyPath = flag.String("path", "./FileClient", "path to user keys files")
    33  var numKeys = flag.Int("numKeys", 3, "number of keys to generate")
    34  var baseName = flag.String("baseUserName", "TestUser", "generic user name")
    35  
    36  // Generate some user keys
    37  func main() {
    38  
    39  	// Parse flags
    40  	flag.Parse()
    41  	outputFileName := path.Join(*keyPath, "serialized_user_keys")
    42  	fmt.Printf("Make user keys, destination: %s\n", outputFileName)
    43  
    44  	// Get policy key and cert.
    45  	domain, err := tao.LoadDomain(*configPath, []byte(*domainPass))
    46          if domain == nil {
    47                  fmt.Printf("keyUtil: no domain path - %s, pass - %s, err - %s\n",
    48                          *configPath, *domainPass, err)
    49                  return
    50          } else if err != nil {
    51                  fmt.Printf("keyUtil: Couldn't load the config path %s: %s\n",
    52                          *configPath, err)
    53                  return
    54          }
    55          fmt.Printf("key_util: Loaded domain\n")
    56  	policyKey := domain.Keys
    57  
    58  	var signerPriv interface{}
    59  	signerPriv = policyKey.SigningKey.PrivKey
    60  	var signerCertificate *x509.Certificate
    61  	signerCertificate = policyKey.Cert
    62  
    63  	userKeys := new(common.UserKeysMessage)
    64  
    65  	for i := 0; i < *numKeys; i++ {
    66  		userName := *baseName + strconv.Itoa(i)
    67  		key, err := common.GenerateUserPublicKey()
    68  		if err != nil {
    69  			fmt.Printf("Can't generate user key %d\n", i)
    70  			return
    71  		}
    72  		keyData, err := common.MakeUserKeyStructure(key, userName, signerPriv, signerCertificate)
    73  		serializedKey, err := common.SerializeUserKey(keyData)
    74  		if err != nil {
    75  			fmt.Printf("Can't serialize user key %d\n", i)
    76  			return
    77  		}
    78  		userCertificate, err := x509.ParseCertificate(keyData.Cert)
    79  		if err != nil {
    80  		}
    81  		fmt.Printf("User cert %d:\n", i)
    82  		fmt.Printf("%x\n\n", userCertificate)
    83  		userKeys.SerializedKeys = append(userKeys.SerializedKeys, serializedKey)
    84  	}
    85  	serializedKeys, err := proto.Marshal(userKeys)
    86  	if err != nil {
    87  	}
    88  	err = ioutil.WriteFile(outputFileName, serializedKeys, 0666)
    89  	if err != nil {
    90  		fmt.Printf("Can't write %s\n", outputFileName)
    91  		return
    92  	}
    93  }