Copyright 2011 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package openpgp

import (
	
	
	

	
	
	
)
PublicKeyType is the armor type for a PGP public key.
var PublicKeyType = "PGP PUBLIC KEY BLOCK"
PrivateKeyType is the armor type for a PGP private key.
var PrivateKeyType = "PGP PRIVATE KEY BLOCK"
An Entity represents the components of an OpenPGP key: a primary public key (which must be a signing key), one or more identities claimed by that key, and zero or more subkeys, which may be encryption keys.
An Identity represents an identity claimed by an Entity and zero or more assertions by other entities about that claim.
type Identity struct {
	Name          string // by convention, has the form "Full Name (comment) <email@example.com>"
	UserId        *packet.UserId
	SelfSignature *packet.Signature
	Signatures    []*packet.Signature
}
A Subkey is an additional public key in an Entity. Subkeys can be used for encryption.
A Key identifies a specific public key in an Entity. This is either the Entity's primary key or a subkey.
A KeyRing provides access to public and private keys.
KeysById returns the set of keys that have the given key id.
KeysByIdAndUsage returns the set of keys with the given id that also meet the key usage given by requiredUsage. The requiredUsage is expressed as the bitwise-OR of packet.KeyFlag* values.
DecryptionKeys returns all private keys that are valid for decryption.
	DecryptionKeys() []Key
}
primaryIdentity returns the Identity marked as primary or the first identity if none are so marked.
func ( *Entity) () *Identity {
	var  *Identity
	for ,  := range .Identities {
		if  == nil {
			 = 
		}
		if .SelfSignature.IsPrimaryId != nil && *.SelfSignature.IsPrimaryId {
			return 
		}
	}
	return 
}
encryptionKey returns the best candidate Key for encrypting a message to the given Entity.
func ( *Entity) ( time.Time) (Key, bool) {
	 := -1
Iterate the keys to find the newest key
	var  time.Time
	for ,  := range .Subkeys {
		if .Sig.FlagsValid &&
			.Sig.FlagEncryptCommunications &&
			.PublicKey.PubKeyAlgo.CanEncrypt() &&
			!.Sig.KeyExpired() &&
			(.IsZero() || .Sig.CreationTime.After()) {
			 = 
			 = .Sig.CreationTime
		}
	}

	if  != -1 {
		 := .Subkeys[]
		return Key{, .PublicKey, .PrivateKey, .Sig}, true
	}
If we don't have any candidate subkeys for encryption and the primary key doesn't have any usage metadata then we assume that the primary key is ok. Or, if the primary key is marked as ok to encrypt to, then we can obviously use it.
This Entity appears to be signing only.
	return Key{}, false
}
signingKey return the best candidate Key for signing a message with this Entity.
func ( *Entity) ( time.Time) (Key, bool) {
	 := -1

	for ,  := range .Subkeys {
		if .Sig.FlagsValid &&
			.Sig.FlagSign &&
			.PublicKey.PubKeyAlgo.CanSign() &&
			!.Sig.KeyExpired() {
			 = 
			break
		}
	}

	if  != -1 {
		 := .Subkeys[]
		return Key{, .PublicKey, .PrivateKey, .Sig}, true
	}
If we have no candidate subkey then we assume that it's ok to sign with the primary key.
An EntityList contains one or more Entities.
KeysById returns the set of keys that have the given key id.
func ( EntityList) ( uint64) ( []Key) {
	for ,  := range  {
		if .PrimaryKey.KeyId ==  {
			var  *packet.Signature
			for ,  := range .Identities {
				if  == nil {
					 = .SelfSignature
				} else if .SelfSignature.IsPrimaryId != nil && *.SelfSignature.IsPrimaryId {
					 = .SelfSignature
					break
				}
			}
			 = append(, Key{, .PrimaryKey, .PrivateKey, })
		}

		for ,  := range .Subkeys {
			if .PublicKey.KeyId ==  {
				 = append(, Key{, .PublicKey, .PrivateKey, .Sig})
			}
		}
	}
	return
}
KeysByIdAndUsage returns the set of keys with the given id that also meet the key usage given by requiredUsage. The requiredUsage is expressed as the bitwise-OR of packet.KeyFlag* values.
func ( EntityList) ( uint64,  byte) ( []Key) {
	for ,  := range .KeysById() {
		if len(.Entity.Revocations) > 0 {
			continue
		}

		if .SelfSignature.RevocationReason != nil {
			continue
		}

		if .SelfSignature.FlagsValid &&  != 0 {
			var  byte
			if .SelfSignature.FlagCertify {
				 |= packet.KeyFlagCertify
			}
			if .SelfSignature.FlagSign {
				 |= packet.KeyFlagSign
			}
			if .SelfSignature.FlagEncryptCommunications {
				 |= packet.KeyFlagEncryptCommunications
			}
			if .SelfSignature.FlagEncryptStorage {
				 |= packet.KeyFlagEncryptStorage
			}
			if & !=  {
				continue
			}
		}

		 = append(, )
	}
	return
}
DecryptionKeys returns all private keys that are valid for decryption.
func ( EntityList) () ( []Key) {
	for ,  := range  {
		for ,  := range .Subkeys {
			if .PrivateKey != nil && (!.Sig.FlagsValid || .Sig.FlagEncryptStorage || .Sig.FlagEncryptCommunications) {
				 = append(, Key{, .PublicKey, .PrivateKey, .Sig})
			}
		}
	}
	return
}
ReadArmoredKeyRing reads one or more public/private keys from an armor keyring file.
func ( io.Reader) (EntityList, error) {
	,  := armor.Decode()
	if  == io.EOF {
		return nil, errors.InvalidArgumentError("no armored data found")
	}
	if  != nil {
		return nil, 
	}
	if .Type != PublicKeyType && .Type != PrivateKeyType {
		return nil, errors.InvalidArgumentError("expected public or private key block, got: " + .Type)
	}

	return ReadKeyRing(.Body)
}
ReadKeyRing reads one or more public/private keys. Unsupported keys are ignored as long as at least a single valid key is found.
func ( io.Reader) ( EntityList,  error) {
	 := packet.NewReader()
	var  error

	for {
		var  *Entity
		,  = ReadEntity()
TODO: warn about skipped unsupported/unreadable keys
			if ,  := .(errors.UnsupportedError);  {
				 = 
				 = readToNextPublicKey()
Skip unreadable, badly-formatted keys
				 = 
				 = readToNextPublicKey()
			}
			if  == io.EOF {
				 = nil
				break
			}
			if  != nil {
				 = nil
				break
			}
		} else {
			 = append(, )
		}
	}

	if len() == 0 &&  == nil {
		 = 
	}
	return
}
readToNextPublicKey reads packets until the start of the entity and leaves the first packet of the new entity in the Reader.
func ( *packet.Reader) ( error) {
	var  packet.Packet
	for {
		,  = .Next()
		if  == io.EOF {
			return
		} else if  != nil {
			if ,  := .(errors.UnsupportedError);  {
				 = nil
				continue
			}
			return
		}

		if ,  := .(*packet.PublicKey);  && !.IsSubkey {
			.Unread()
			return
		}
	}
}
ReadEntity reads an entity (public key, identities, subkeys etc) from the given Reader.
func ( *packet.Reader) (*Entity, error) {
	 := new(Entity)
	.Identities = make(map[string]*Identity)

	,  := .Next()
	if  != nil {
		return nil, 
	}

	var  bool
	if .PrimaryKey,  = .(*packet.PublicKey); ! {
		if .PrivateKey,  = .(*packet.PrivateKey); ! {
			.Unread()
			return nil, errors.StructuralError("first packet was not a public/private key")
		}
		.PrimaryKey = &.PrivateKey.PublicKey
	}

	if !.PrimaryKey.PubKeyAlgo.CanSign() {
		return nil, errors.StructuralError("primary key cannot be used for signatures")
	}

	var  []*packet.Signature
:
	for {
		,  := .Next()
		if  == io.EOF {
			break
		} else if  != nil {
			return nil, 
		}

		switch pkt := .(type) {
		case *packet.UserId:
			if  := addUserID(, , );  != nil {
				return nil, 
			}
		case *packet.Signature:
			if .SigType == packet.SigTypeKeyRevocation {
				 = append(, )
TODO: RFC4880 5.2.1 permits signatures directly on keys (eg. to bind additional revocation keys).
Else, ignoring the signature as it does not follow anything we would know to attach it to.
		case *packet.PrivateKey:
			if .IsSubkey == false {
				.Unread()
				break 
			}
			 = addSubkey(, , &.PublicKey, )
			if  != nil {
				return nil, 
			}
		case *packet.PublicKey:
			if .IsSubkey == false {
				.Unread()
				break 
			}
			 = addSubkey(, , , nil)
			if  != nil {
				return nil, 
			}
we ignore unknown packets
		}
	}

	if len(.Identities) == 0 {
		return nil, errors.StructuralError("entity without any identities")
	}

	for ,  := range  {
		 = .PrimaryKey.VerifyRevocationSignature()
		if  == nil {
			.Revocations = append(.Revocations, )
TODO: RFC 4880 5.2.3.15 defines revocation keys.
			return nil, errors.StructuralError("revocation signature signed by alternate key")
		}
	}

	return , nil
}

Make a new Identity object, that we might wind up throwing away. We'll only add it if we get a valid self-signature over this userID.
	 := new(Identity)
	.Name = .Id
	.UserId = 

	for {
		,  := .Next()
		if  == io.EOF {
			break
		} else if  != nil {
			return 
		}

		,  := .(*packet.Signature)
		if ! {
			.Unread()
			break
		}

		if (.SigType == packet.SigTypePositiveCert || .SigType == packet.SigTypeGenericCert) && .IssuerKeyId != nil && *.IssuerKeyId == .PrimaryKey.KeyId {
			if  = .PrimaryKey.VerifyUserIdSignature(.Id, .PrimaryKey, );  != nil {
				return errors.StructuralError("user ID self-signature invalid: " + .Error())
			}
			.SelfSignature = 
			.Identities[.Id] = 
		} else {
			.Signatures = append(.Signatures, )
		}
	}

	return nil
}

func ( *Entity,  *packet.Reader,  *packet.PublicKey,  *packet.PrivateKey) error {
	var  Subkey
	.PublicKey = 
	.PrivateKey = 

	for {
		,  := .Next()
		if  == io.EOF {
			break
		} else if  != nil {
			return errors.StructuralError("subkey signature invalid: " + .Error())
		}

		,  := .(*packet.Signature)
		if ! {
			.Unread()
			break
		}

		if .SigType != packet.SigTypeSubkeyBinding && .SigType != packet.SigTypeSubkeyRevocation {
			return errors.StructuralError("subkey signature with wrong type")
		}

		if  := .PrimaryKey.VerifyKeySignature(.PublicKey, );  != nil {
			return errors.StructuralError("subkey signature invalid: " + .Error())
		}

		switch .SigType {
		case packet.SigTypeSubkeyRevocation:
			.Sig = 
		case packet.SigTypeSubkeyBinding:

			if shouldReplaceSubkeySig(.Sig, ) {
				.Sig = 
			}
		}
	}

	if .Sig == nil {
		return errors.StructuralError("subkey packet not followed by signature")
	}

	.Subkeys = append(.Subkeys, )

	return nil
}

func (,  *packet.Signature) bool {
	if  == nil {
		return false
	}

	if  == nil {
		return true
	}

	if .SigType == packet.SigTypeSubkeyRevocation {
		return false // never override a revocation signature
	}

	return .CreationTime.After(.CreationTime)
}

const defaultRSAKeyBits = 2048
NewEntity returns an Entity that contains a fresh RSA/RSA keypair with a single identity composed of the given full name, comment and email, any of which may be empty but must not contain any of "()<>\x00". If config is nil, sensible defaults will be used.
func (, ,  string,  *packet.Config) (*Entity, error) {
	 := .Now()

	 := defaultRSAKeyBits
	if  != nil && .RSABits != 0 {
		 = .RSABits
	}

	 := packet.NewUserId(, , )
	if  == nil {
		return nil, errors.InvalidArgumentError("user id field contained invalid characters")
	}
	,  := rsa.GenerateKey(.Random(), )
	if  != nil {
		return nil, 
	}
	,  := rsa.GenerateKey(.Random(), )
	if  != nil {
		return nil, 
	}

	 := &Entity{
		PrimaryKey: packet.NewRSAPublicKey(, &.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(, ),
		Identities: make(map[string]*Identity),
	}
	 := true
	.Identities[.Id] = &Identity{
		Name:   .Id,
		UserId: ,
		SelfSignature: &packet.Signature{
			CreationTime: ,
			SigType:      packet.SigTypePositiveCert,
			PubKeyAlgo:   packet.PubKeyAlgoRSA,
			Hash:         .Hash(),
			IsPrimaryId:  &,
			FlagsValid:   true,
			FlagSign:     true,
			FlagCertify:  true,
			IssuerKeyId:  &.PrimaryKey.KeyId,
		},
	}
	 = .Identities[.Id].SelfSignature.SignUserId(.Id, .PrimaryKey, .PrivateKey, )
	if  != nil {
		return nil, 
	}
If the user passes in a DefaultHash via packet.Config, set the PreferredHash for the SelfSignature.
	if  != nil && .DefaultHash != 0 {
		.Identities[.Id].SelfSignature.PreferredHash = []uint8{hashToHashId(.DefaultHash)}
	}
Likewise for DefaultCipher.
	if  != nil && .DefaultCipher != 0 {
		.Identities[.Id].SelfSignature.PreferredSymmetric = []uint8{uint8(.DefaultCipher)}
	}

	.Subkeys = make([]Subkey, 1)
	.Subkeys[0] = Subkey{
		PublicKey:  packet.NewRSAPublicKey(, &.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(, ),
		Sig: &packet.Signature{
			CreationTime:              ,
			SigType:                   packet.SigTypeSubkeyBinding,
			PubKeyAlgo:                packet.PubKeyAlgoRSA,
			Hash:                      .Hash(),
			FlagsValid:                true,
			FlagEncryptStorage:        true,
			FlagEncryptCommunications: true,
			IssuerKeyId:               &.PrimaryKey.KeyId,
		},
	}
	.Subkeys[0].PublicKey.IsSubkey = true
	.Subkeys[0].PrivateKey.IsSubkey = true
	 = .Subkeys[0].Sig.SignKey(.Subkeys[0].PublicKey, .PrivateKey, )
	if  != nil {
		return nil, 
	}
	return , nil
}
SerializePrivate serializes an Entity, including private key material, but excluding signatures from other entities, to the given Writer. Identities and subkeys are re-signed in case they changed since NewEntry. If config is nil, sensible defaults will be used.
func ( *Entity) ( io.Writer,  *packet.Config) ( error) {
	 = .PrivateKey.Serialize()
	if  != nil {
		return
	}
	for ,  := range .Identities {
		 = .UserId.Serialize()
		if  != nil {
			return
		}
		 = .SelfSignature.SignUserId(.UserId.Id, .PrimaryKey, .PrivateKey, )
		if  != nil {
			return
		}
		 = .SelfSignature.Serialize()
		if  != nil {
			return
		}
	}
	for ,  := range .Subkeys {
		 = .PrivateKey.Serialize()
		if  != nil {
			return
		}
		 = .Sig.SignKey(.PublicKey, .PrivateKey, )
		if  != nil {
			return
		}
		 = .Sig.Serialize()
		if  != nil {
			return
		}
	}
	return nil
}
Serialize writes the public part of the given Entity to w, including signatures from other entities. No private key material will be output.
func ( *Entity) ( io.Writer) error {
	 := .PrimaryKey.Serialize()
	if  != nil {
		return 
	}
	for ,  := range .Identities {
		 = .UserId.Serialize()
		if  != nil {
			return 
		}
		 = .SelfSignature.Serialize()
		if  != nil {
			return 
		}
		for ,  := range .Signatures {
			 = .Serialize()
			if  != nil {
				return 
			}
		}
	}
	for ,  := range .Subkeys {
		 = .PublicKey.Serialize()
		if  != nil {
			return 
		}
		 = .Sig.Serialize()
		if  != nil {
			return 
		}
	}
	return nil
}
SignIdentity adds a signature to e, from signer, attesting that identity is associated with e. The provided identity must already be an element of e.Identities and the private key of signer must have been decrypted if necessary. If config is nil, sensible defaults will be used.
func ( *Entity) ( string,  *Entity,  *packet.Config) error {
	if .PrivateKey == nil {
		return errors.InvalidArgumentError("signing Entity must have a private key")
	}
	if .PrivateKey.Encrypted {
		return errors.InvalidArgumentError("signing Entity's private key must be decrypted")
	}
	,  := .Identities[]
	if ! {
		return errors.InvalidArgumentError("given identity string not found in Entity")
	}

	 := &packet.Signature{
		SigType:      packet.SigTypeGenericCert,
		PubKeyAlgo:   .PrivateKey.PubKeyAlgo,
		Hash:         .Hash(),
		CreationTime: .Now(),
		IssuerKeyId:  &.PrivateKey.KeyId,
	}
	if  := .SignUserId(, .PrimaryKey, .PrivateKey, );  != nil {
		return 
	}
	.Signatures = append(.Signatures, )
	return nil