/*************************************************
* X.509 Public Key Source File                   *
* (C) 1999-2003 The Botan Project                *
*************************************************/

#include <botan/x509_key.h>
#include <botan/filters.h>
#include <botan/asn1_obj.h>
#include <botan/pk_algs.h>
#include <botan/oids.h>
#include <botan/pem.h>
#include <memory>

namespace Botan {

/*************************************************
* Return the default NULL algorithm parameters   *
*************************************************/
SecureVector<byte> X509_PublicKey::DER_encode_params() const
   {
   DER_Encoder encoder;
   DER::encode_null(encoder);
   return encoder.get_contents();
   }

/*************************************************
* Discard the parameters, which should be NULL   *
*************************************************/
void X509_PublicKey::BER_decode_params(DataSource& source)
   {
   byte dummy = 0;
   while(!source.end_of_data())
      source.read_byte(dummy);
   }

namespace X509 {

namespace {

/*************************************************
* Extract the fields of a subjectPublicKeyInfo   *
*************************************************/
void X509_extract_info(DataSource& source, AlgorithmIdentifier& alg_id,
                       SecureVector<byte>& key)
   {
   BER_Decoder decoder(source);
   BER_Decoder sequence = BER::get_subsequence(decoder);
   BER::decode(sequence, alg_id);
   BER::decode(sequence, key, BIT_STRING);
   sequence.verify_end();
   }

}

/*************************************************
* DER or PEM encode a X.509 public key           *
*************************************************/
void encode(const X509_PublicKey& key, Pipe& pipe, X509_Encoding encoding)
   {
   DER_Encoder encoder;
   AlgorithmIdentifier alg_id(key.get_oid(), key.DER_encode_params());

   encoder.start_sequence();
   DER::encode(encoder, alg_id);
   DER::encode(encoder, key.DER_encode_pub(), BIT_STRING);
   encoder.end_sequence();

   SecureVector<byte> der = encoder.get_contents();
   if(encoding == PEM)
      pipe.write(PEM_Code::encode(der, der.size(), "PUBLIC KEY"));
   else
      pipe.write(der);
   }

/*************************************************
* PEM encode a X.509 public key                  *
*************************************************/
std::string PEM_encode(const X509_PublicKey& key)
   {
   Pipe pem;
   pem.start_msg();
   encode(key, pem, PEM);
   pem.end_msg();
   return pem.read_all_as_string();
   }

/*************************************************
* Extract a public key and return it             *
*************************************************/
X509_PublicKey* load_key(DataSource& source)
   {
   try {
      AlgorithmIdentifier alg_id;
      SecureVector<byte> key;

      if(BER::maybe_BER(source) && !PEM_Code::matches(source, "PUBLIC KEY"))
         X509_extract_info(source, alg_id, key);
      else
         {
         DataSource_Memory ber(
            PEM_Code::decode_check_label(source, "PUBLIC KEY")
            );
         X509_extract_info(ber, alg_id, key);
         }

      if(key.is_empty())
         throw Decoding_Error("X.509 public key decoding failed");

      const std::string alg_name = OIDS::lookup(alg_id.oid);
      if(alg_name == "")
         throw Decoding_Error("Unknown algorithm OID: " +
                              alg_id.oid.as_string());

      std::auto_ptr<X509_PublicKey> key_obj(get_public_key(alg_name));
      if(!key_obj.get())
         throw Decoding_Error("Unknown PK algorithm/OID: " + alg_name + ", " +
                              alg_id.oid.as_string());

      Pipe output;
      output.process_msg(alg_id.parameters);
      output.process_msg(key);
      key_obj->BER_decode_params(output);
      output.set_default_msg(1);
      key_obj->BER_decode_pub(output);

      return key_obj.release();
      }
   catch(Decoding_Error)
      {
      throw Decoding_Error("X.509 public key decoding failed");
      }
   }

/*************************************************
* Extract a public key and return it             *
*************************************************/
X509_PublicKey* load_key(const std::string& fsname)
   {
   DataSource_Stream source(fsname);
   return X509::load_key(source);
   }

/*************************************************
* Extract a public key and return it             *
*************************************************/
X509_PublicKey* load_key(const SecureVector<byte>& pub_key)
   {
   DataSource_Memory source(pub_key);
   return X509::load_key(source);
   }

/*************************************************
* Find the allowable key constraints             *
*************************************************/
Key_Constraints find_constraints(const X509_PublicKey& pub_key,
                                 Key_Constraints limits)
   {
   const X509_PublicKey* key = &pub_key;
   u32bit constraints = 0;

   if(dynamic_cast<const PK_Encrypting_Key*>(key))
      constraints |= KEY_ENCIPHERMENT;

   if(dynamic_cast<const PK_Key_Agreement_Key*>(key))
      constraints |= KEY_AGREEMENT;

   if(dynamic_cast<const PK_Verifying_wo_MR_Key*>(key) ||
      dynamic_cast<const PK_Verifying_with_MR_Key*>(key))
      constraints |= DIGITAL_SIGNATURE | NON_REPUDIATION;

   if(limits)
      constraints &= limits;

   return Key_Constraints(constraints);
   }

}

}
