Debian Codegraph @

src/lib/crypto/BotanRSAPublicKey.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 BotanRSAPublicKey.cpp
29
30 Botan RSA public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "BotanRSAPublicKey.h"
36 #include "BotanUtil.h"
37 #include <string.h>
38
39 // Constructors
40 BotanRSAPublicKey::BotanRSAPublicKey()
41 {
42 rsa = NULL;
43 }
44
45 BotanRSAPublicKey::BotanRSAPublicKey(const Botan::RSA_PublicKey* inRSA)
46 {
47 rsa = NULL;
48
49 setFromBotan(inRSA);
50 }
51
52 // Destructor
53 BotanRSAPublicKey::~BotanRSAPublicKey()
54 {
55 delete rsa;
56 }
57
58 // The type
59 /*static*/ const char* BotanRSAPublicKey::type = "Botan RSA Public Key";
60
61 // Check if the key is of the given type
62 bool BotanRSAPublicKey::isOfType(const char* inType)
63 {
64 return !strcmp(type, inType);
65 }
66
67 // Set from OpenSSL representation
68 void BotanRSAPublicKey::setFromBotan(const Botan::RSA_PublicKey* inRSA)
69 {
70 ByteString inN = BotanUtil::bigInt2ByteString(inRSA->get_n());
71 setN(inN);
72 ByteString inE = BotanUtil::bigInt2ByteString(inRSA->get_e());
73 setE(inE);
74 }
75
76 // Setters for the RSA public key components
77 void BotanRSAPublicKey::setN(const ByteString& inN)
78 {
79 RSAPublicKey::setN(inN);
80
81 if (rsa)
82 {
83 delete rsa;
84 rsa = NULL;
85 }
86 }
87
88 void BotanRSAPublicKey::setE(const ByteString& inE)
89 {
90 RSAPublicKey::setE(inE);
91
92 if (rsa)
93 {
94 delete rsa;
95 rsa = NULL;
96 }
97 }
98
99 // Retrieve the Botan representation of the key
100 Botan::RSA_PublicKey* BotanRSAPublicKey::getBotanKey()
101 {
102 if (!rsa)
103 {
104 createBotanKey();
105 }
106
107 return rsa;
108 }
109
110 // Create the Botan representation of the key
111 void BotanRSAPublicKey::createBotanKey()
112 {
113 if (n.size() != 0 && e.size() != 0)
114 {
115 if (rsa)
116 {
117 delete rsa;
118 rsa = NULL;
119 }
120
121 try
122 {
123 rsa = new Botan::RSA_PublicKey(BotanUtil::byteString2bigInt(n),
124 BotanUtil::byteString2bigInt(e));
125 }
126 catch (...)
127 {
128 ERROR_MSG("Could not create the Botan public key");
129 }
130 }
131 }