Debian Codegraph @

src/lib/crypto/BotanDSAPublicKey.cpp

raw
1 /*
2 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 BotanDSAPublicKey.cpp
29
30 Botan DSA public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "BotanDSAPublicKey.h"
36 #include "BotanUtil.h"
37 #include <string.h>
38
39 // Constructors
40 BotanDSAPublicKey::BotanDSAPublicKey()
41 {
42 dsa = NULL;
43 }
44
45 BotanDSAPublicKey::BotanDSAPublicKey(const Botan::DSA_PublicKey* inDSA)
46 {
47 dsa = NULL;
48
49 setFromBotan(inDSA);
50 }
51
52 // Destructor
53 BotanDSAPublicKey::~BotanDSAPublicKey()
54 {
55 delete dsa;
56 }
57
58 // The type
59 /*static*/ const char* BotanDSAPublicKey::type = "Botan DSA Public Key";
60
61 // Set from Botan representation
62 void BotanDSAPublicKey::setFromBotan(const Botan::DSA_PublicKey* inDSA)
63 {
64 ByteString inP = BotanUtil::bigInt2ByteString(inDSA->group_p());
65 setP(inP);
66 ByteString inQ = BotanUtil::bigInt2ByteString(inDSA->group_q());
67 setQ(inQ);
68 ByteString inG = BotanUtil::bigInt2ByteString(inDSA->group_g());
69 setG(inG);
70 ByteString inY = BotanUtil::bigInt2ByteString(inDSA->get_y());
71 setY(inY);
72 }
73
74 // Check if the key is of the given type
75 bool BotanDSAPublicKey::isOfType(const char* inType)
76 {
77 return !strcmp(type, inType);
78 }
79
80 // Setters for the DSA public key components
81 void BotanDSAPublicKey::setP(const ByteString& inP)
82 {
83 DSAPublicKey::setP(inP);
84
85 if (dsa)
86 {
87 delete dsa;
88 dsa = NULL;
89 }
90 }
91
92 void BotanDSAPublicKey::setQ(const ByteString& inQ)
93 {
94 DSAPublicKey::setQ(inQ);
95
96 if (dsa)
97 {
98 delete dsa;
99 dsa = NULL;
100 }
101 }
102
103 void BotanDSAPublicKey::setG(const ByteString& inG)
104 {
105 DSAPublicKey::setG(inG);
106
107 if (dsa)
108 {
109 delete dsa;
110 dsa = NULL;
111 }
112 }
113
114 void BotanDSAPublicKey::setY(const ByteString& inY)
115 {
116 DSAPublicKey::setY(inY);
117
118 if (dsa)
119 {
120 delete dsa;
121 dsa = NULL;
122 }
123 }
124
125 // Retrieve the Botan representation of the key
126 Botan::DSA_PublicKey* BotanDSAPublicKey::getBotanKey()
127 {
128 if (!dsa)
129 {
130 createBotanKey();
131 }
132
133 return dsa;
134 }
135
136 // Create the Botan representation of the key
137 void BotanDSAPublicKey::createBotanKey()
138 {
139 // We actually do not need to check q, since it can be set zero
140 if (p.size() != 0 &&
141 g.size() != 0 &&
142 y.size() != 0)
143 {
144 if (dsa)
145 {
146 delete dsa;
147 dsa = NULL;
148 }
149
150 try
151 {
152 dsa = new Botan::DSA_PublicKey(Botan::DL_Group(BotanUtil::byteString2bigInt(p),
153 BotanUtil::byteString2bigInt(q),
154 BotanUtil::byteString2bigInt(g)),
155 BotanUtil::byteString2bigInt(y));
156 }
157 catch (...)
158 {
159 ERROR_MSG("Could not create the Botan public key");
160 }
161 }
162 }