func (r *KeyRing) SetState(seqnum int, newState api.KeyState) error {
	key := r.keyDataBySeqnum(seqnum)
	if key == nil {
		return api.ErrKeyNotExist
	}
	oldState := api.KeyState(key.State)
	log := r.log.WithField("oldState", oldState).WithField("newState", newState)
	if !api.KeyStateTransitionValid(oldState, newState) {
		log.Debug("invalid state transition requested")
		return api.ErrInvalidState
	}
	err := r.changeKeyState(seqnum, oldState, newState)
	if err != nil {
		log.WithError(err).Debug("failed to change key state")
		return err
	}
	log.Trace("changed key state")
	return nil
}
SetState changes key State to the given one, if allowed.
package filesystem
import (
	"fmt"
	"time"
	"github.com/cossacklabs/acra/keystore/v2/keystore/api"
	"github.com/cossacklabs/acra/keystore/v2/keystore/asn1"
	log "github.com/sirupsen/logrus"
)
func (r *KeyRing) newKey(k api.KeyDescription) (*asn1.Key, error) {
	nextSeqnum := r.nextSeqnum()
	key := &asn1.Key{
		Seqnum:     nextSeqnum,
		State:      asn1.KeyPreActive,
		ValidSince: k.ValidSince,
		ValidUntil: k.ValidUntil,
		Data:       make([]asn1.KeyData, 0, 1),
	}
	if k.ValidSince.After(k.ValidUntil) {
		return nil, api.ErrInvalidCryptoperiod
	}
	if len(k.Data) == 0 {
		return nil, api.ErrNoKeyData
	}
	for _, data := range k.Data {
		err := r.addKeyData(data, key)
		if err != nil {
			log.WithError(err).Debug("failed to add key data")
			return nil, err
		}
	}
	return key, nil
}
func (r *KeyRing) copyKey(other *asn1.Key) (*asn1.Key, error) {
	if other.ValidSince.After(other.ValidUntil) {
		return nil, api.ErrInvalidCryptoperiod
	}
	if len(other.Data) == 0 {
		return nil, api.ErrNoKeyData
	}
	key := *other
	key.Data = make([]asn1.KeyData, 0, len(other.Data))
	for _, otherKey := range other.Data {
		data := api.KeyData{
			Format:       api.KeyFormat(otherKey.Format),
			PublicKey:    otherKey.PublicKey,
			PrivateKey:   otherKey.PrivateKey,
			SymmetricKey: otherKey.SymmetricKey,
		}
		err := r.addKeyData(data, &key)
		if err != nil {
			return nil, err
		}
	}
	return &key, nil
}
func (r *KeyRing) keyDataByFormat(seqnum int, format api.KeyFormat) (*asn1.KeyData, error) {
	key := r.keyDataBySeqnum(seqnum)
	if key == nil {
		return nil, api.ErrKeyNotExist
	}
	if api.KeyState(key.State) == api.KeyDestroyed {
		return nil, api.ErrKeyDestroyed
	}
	for i := range key.Data {
		if api.KeyFormat(key.Data[i].Format) == format {
			return &key.Data[i], nil
		}
	}
	return nil, api.ErrFormatMissing
}
func (r *KeyRing) State(seqnum int) (api.KeyState, error) {
	key := r.keyDataBySeqnum(seqnum)
	if key == nil {
		return api.KeyDestroyed, api.ErrKeyNotExist
	}
	return api.KeyState(key.State), nil
}