aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/cert_ver.cpp
blob: 2c2ec9b2a0d33bb661dd20fc1c31d5f5ff6fba0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Certificate Verify Message
* (C) 2004,2006,2011,2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/internal/tls_messages.h>
#include <botan/internal/tls_reader.h>
#include <botan/internal/assert.h>
#include <botan/tls_exceptn.h>
#include <botan/pubkey.h>
#include <botan/rsa.h>
#include <botan/dsa.h>
#include <botan/loadstor.h>
#include <memory>

namespace Botan {

/*
* Create a new Certificate Verify message
*/
Certificate_Verify::Certificate_Verify(Record_Writer& writer,
                                       TLS_Handshake_Hash& hash,
                                       RandomNumberGenerator& rng,
                                       Version_Code version,
                                       const SecureVector<byte>& master_secret,
                                       const Private_Key* priv_key)
   {
   BOTAN_ASSERT_NONNULL(priv_key);

   std::string padding = "";
   Signature_Format format = IEEE_1363;

   if(priv_key->algo_name() == "RSA")
      padding = "EMSA3(TLS.Digest.0)";
   else if(priv_key->algo_name() == "DSA")
      {
      if(version == SSL_V3)
         padding = "Raw";
      else
         padding = "EMSA1(SHA-1)";
      format = DER_SEQUENCE;
      }
   else
      throw Invalid_Argument(priv_key->algo_name() +
                             " is invalid/unknown for TLS signatures");

   PK_Signer signer(*priv_key, padding, format);

   if(version == SSL_V3)
      {
      SecureVector<byte> md5_sha = hash.final_ssl3(master_secret);

      signature = signer.sign_message(&md5_sha[16], md5_sha.size()-16, rng);
      }
   else if(version == TLS_V10 || version == TLS_V11)
      {
      signature = signer.sign_message(hash.get_contents(), rng);
      }
   else
      throw TLS_Exception(PROTOCOL_VERSION,
                          "Unknown TLS version in certificate verification");

   send(writer, hash);
   }

/*
* Serialize a Certificate Verify message
*/
MemoryVector<byte> Certificate_Verify::serialize() const
   {
   MemoryVector<byte> buf;

   const u16bit sig_len = signature.size();
   buf.push_back(get_byte(0, sig_len));
   buf.push_back(get_byte(1, sig_len));
   buf += signature;

   return buf;
   }

/*
* Deserialize a Certificate Verify message
*/
void Certificate_Verify::deserialize(const MemoryRegion<byte>& buf)
   {
   TLS_Data_Reader reader(buf);
   signature = reader.get_range<byte>(2, 0, 65535);
   }

/*
* Verify a Certificate Verify message
*/
bool Certificate_Verify::verify(const X509_Certificate& cert,
                                TLS_Handshake_Hash& hash,
                                Version_Code version,
                                const SecureVector<byte>& master_secret)
   {
   std::auto_ptr<Public_Key> key(cert.subject_public_key());

   std::string padding = "";
   Signature_Format format = IEEE_1363;

   if(key->algo_name() == "RSA")
      {
      padding = "EMSA3(TLS.Digest.0)";
      }
   else if(key->algo_name() == "DSA")
      {
      if(version == SSL_V3)
         padding = "Raw";
      else
         padding = "EMSA1(SHA-1)";
      format = DER_SEQUENCE;
      }
   else
      throw Invalid_Argument(key->algo_name() +
                             " is invalid/unknown for TLS signatures");

   PK_Verifier verifier(*key, padding, format);

   if(version == SSL_V3)
      {
      SecureVector<byte> md5_sha = hash.final_ssl3(master_secret);

      return verifier.verify_message(&md5_sha[16], md5_sha.size()-16,
                                     &signature[0], signature.size());
      }
   else if(version == TLS_V10 || version == TLS_V11)
      return verifier.verify_message(hash.get_contents(), signature);
   else
      throw TLS_Exception(PROTOCOL_VERSION,
                          "Unknown TLS version in certificate verification");
   }

}