// NewPreKeyBundle creates a PreKeyBundle structure with the given fields.
965188
func NewPreKeyBundle(registrationID, deviceID, preKeyID uint32, preKey *ECPublicKey,
	signedPreKeyID int32, signedPreKey *ECPublicKey, signature []byte,
	identityKey *IdentityKey) (*PreKeyBundle, error) {
	pkb := &PreKeyBundle{
		RegistrationID:     registrationID,
		DeviceID:           deviceID,
		PreKeyID:           preKeyID,
		PreKeyPublic:       preKey,
		SignedPreKeyID:     signedPreKeyID,
		SignedPreKeyPublic: signedPreKey,
		IdentityKey:        identityKey,
	}
	copy(pkb.SignedPreKeySignature[:], signature)
	return pkb, nil
}
573620
func (bldr *fooBarQueryBuilder) KeysOnly() *fooBarQueryBuilder {
	bldr.q = bldr.q.KeysOnly()
	if bldr.plugin != nil {
		bldr.plugin.KeysOnly()
	}
	return bldr
}
1142182
func NewPrivateKeyFileStorage(baseDir, fileExt string) (*FilesystemStore, error) {
	baseDir = filepath.Join(baseDir, notary.PrivDir)
	myStore, err := NewFileStore(baseDir, fileExt)
	myStore.migrateTo0Dot4()
	return myStore, err
}
735441
func (bldr *aeDataQueryBuilder) KeysOnly() *aeDataQueryBuilder {
	bldr.q = bldr.q.KeysOnly()
	if bldr.plugin != nil {
		bldr.plugin.KeysOnly()
	}
	return bldr
}
1186408
func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
	file := cfg.RequiredString("file")
	if err := cfg.Validate(); err != nil {
		return nil, err
	}
	opts := &kv.Options{}
	db, err := kvutil.Open(file, opts)
	if err != nil {
		return nil, err
	}
	is := &kvis{
		db:   db,
		opts: opts,
		path: file,
	}
	return is, nil
}
1200310
func NewECDSAPublicKey(public []byte) *ECDSAPublicKey {
	return &ECDSAPublicKey{
		TUFKey: TUFKey{
			Type: ECDSAKey,
			Value: KeyPair{
				Public:  public,
				Private: nil,
			},
		},
	}
}
641114
func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
	if err := os.MkdirAll(path, 0711); err != nil {
		return nil, err
	}
	path = filepath.Join(path, id)
	if err := os.Mkdir(path, 0711); err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			os.RemoveAll(path)
		}
	}()
	workDir = filepath.Join(workDir, id)
	if err := os.MkdirAll(workDir, 0711); err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			os.RemoveAll(workDir)
		}
	}()
	if err := os.Mkdir(filepath.Join(path, "rootfs"), 0711); err != nil {
		return nil, err
	}
	err = ioutil.WriteFile(filepath.Join(path, configFilename), spec, 0666)
	return &bundle{
		id:      id,
		path:    path,
		workDir: workDir,
	}, err
}
162760
func NewKeyfile(f string) *Keyfile {
	if strings.ToUpper(f) == "TRANSIENT" {
		f = ""
	}
	return &Keyfile{
		fname: f,
	}
}
354191
func NewSignedPreKeyRecord(id uint32, timestamp uint64, kp *ECKeyPair, signature []byte) *SignedPreKeyRecord {
	return &SignedPreKeyRecord{
		&protobuf.SignedPreKeyRecordStructure{
			Id:         &id,
			PublicKey:  kp.PublicKey.Key()[:],
			PrivateKey: kp.PrivateKey.Key()[:],
			Timestamp:  &timestamp,
			Signature:  signature,
		},
	}
}
397575
func (c *ConfigLocal) SetKeyBundleCache(k kbfsmd.KeyBundleCache) {
	c.lock.Lock()
	defer c.lock.Unlock()
	c.kbcache = k
}
122210
func (md *RootMetadataV2) getTLFKeyBundles(keyGen KeyGen) (
	*TLFWriterKeyBundleV2, *TLFReaderKeyBundleV2, error) {
	if md.ID.Type() != tlf.Private {
		return nil, nil, InvalidNonPrivateTLFOperation{md.ID, "getTLFKeyBundles", md.Version()}
	}

	if keyGen < FirstValidKeyGen {
		return nil, nil, InvalidKeyGenerationError{md.ID, keyGen}
	}
	i := int(keyGen - FirstValidKeyGen)
	if i >= len(md.WKeys) || i >= len(md.RKeys) {
		return nil, nil, NewKeyGenerationError{md.ID, keyGen}
	}
	return &md.WKeys[i], &md.RKeys[i], nil
}
386946
func AdvanceBundle(prevBundle stellar1.Bundle) stellar1.Bundle {
	nextBundle := prevBundle.DeepCopy()
	nextBundle.Prev = nextBundle.OwnHash
	nextBundle.OwnHash = nil
	nextBundle.Revision++
	return nextBundle
}
1046588
func serializeBundle(b ConfigBundle) ([]byte, error) {
	keys := make([]string, 0, len(b))
	for k := range b {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	items := make([]blobWithType, 0, len(b))
	for _, k := range keys {
		v := b[k]
		blob, err := proto.Marshal(v)
		if err != nil {
			return nil, err
		}
		items = append(items, blobWithType{
			Path: k,
			Kind: proto.MessageName(v),
			Blob: blob,
		})
	}

	out := bytes.Buffer{}
	if err := gob.NewEncoder(&out).Encode(items); err != nil {
		return nil, err
	}
	return out.Bytes(), nil
}
1184007
func NewKey() (key *Key, err error) {
	priv, pub, err := native.GenerateKeyPair("")
	if err != nil {
		return nil, trace.Wrap(err)
	}

	return &Key{
		Priv: priv,
		Pub:  pub,
	}, nil
}
299277
func NewBundlerFromPEM(caBundlePEM, intBundlePEM []byte, opt ...Option) (*Bundler, error) {
	opts := defaultOptions
	for _, o := range opt {
		o(&opts)
	}

	log.Debug("parsing root certificates from PEM")
	roots, err := helpers.ParseCertificatesPEM(caBundlePEM)
	if err != nil {
		log.Errorf("failed to parse root bundle: %v", err)
		return nil, errors.New(errors.RootError, errors.ParseFailed)
	}

	log.Debug("parse intermediate certificates from PEM")
	intermediates, err := helpers.ParseCertificatesPEM(intBundlePEM)
	if err != nil {
		log.Errorf("failed to parse intermediate bundle: %v", err)
		return nil, errors.New(errors.IntermediatesError, errors.ParseFailed)
	}

	b := &Bundler{
		KnownIssuers:     map[string]bool{},
		IntermediatePool: x509.NewCertPool(),
		opts:             opts,
	}

	log.Debug("building certificate pools")

	// RootPool will be nil if caBundlePEM is nil, also
	// that translates to caBundleFile is "".
	// Systems root store will be used.
	if caBundlePEM != nil {
		b.RootPool = x509.NewCertPool()
	}

	for _, c := range roots {
		b.RootPool.AddCert(c)
		b.KnownIssuers[string(c.Signature)] = true
	}

	for _, c := range intermediates {
		b.IntermediatePool.AddCert(c)
		b.KnownIssuers[string(c.Signature)] = true
	}

	log.Debug("bundler set up")
	return b, nil
}
1033018
func New(key interface{}) (Key, error) {
	if key == nil {
		return nil, errors.New(`jwk.New requires a non-nil key`)
	}

	switch v := key.(type) {
	case *rsa.PrivateKey:
		return newRSAPrivateKey(v)
	case *rsa.PublicKey:
		return newRSAPublicKey(v)
	case *ecdsa.PrivateKey:
		return newECDSAPrivateKey(v)
	case *ecdsa.PublicKey:
		return newECDSAPublicKey(v)
	case []byte:
		return newSymmetricKey(v)
	default:
		return nil, errors.Errorf(`invalid key type %T`, key)
	}
}
707786
func NewFieldKey(obj map[string]interface{}, field string) FieldKey {
	return FieldKey{object: reflect.ValueOf(obj), field: field}
}