Debian Codegraph @

src/lib/crypto/OSSLRSAPublicKey.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
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 OSSLRSAPublicKey.cpp
29
30 OpenSSL RSA public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "OSSLComp.h"
36 #include "OSSLRSAPublicKey.h"
37 #include "OSSLUtil.h"
38 #include <string.h>
39 #include <openssl/bn.h>
40 #ifdef WITH_FIPS
41 #include <openssl/fips.h>
42 #endif
43
44 // Constructors
45 OSSLRSAPublicKey::OSSLRSAPublicKey()
46 {
47 rsa = NULL;
48 }
49
50 OSSLRSAPublicKey::OSSLRSAPublicKey(const RSA* inRSA)
51 {
52 rsa = NULL;
53
54 setFromOSSL(inRSA);
55 }
56
57 // Destructor
58 OSSLRSAPublicKey::~OSSLRSAPublicKey()
59 {
60 RSA_free(rsa);
61 }
62
63 // The type
64 /*static*/ const char* OSSLRSAPublicKey::type = "OpenSSL RSA Public Key";
65
66 // Check if the key is of the given type
67 bool OSSLRSAPublicKey::isOfType(const char* inType)
68 {
69 return !strcmp(type, inType);
70 }
71
72 // Set from OpenSSL representation
73 void OSSLRSAPublicKey::setFromOSSL(const RSA* inRSA)
74 {
75 const BIGNUM* bn_n = NULL;
76 const BIGNUM* bn_e = NULL;
77
78 RSA_get0_key(inRSA, &bn_n, &bn_e, NULL);
79
80 if (bn_n)
81 {
82 ByteString inN = OSSL::bn2ByteString(bn_n);
83 setN(inN);
84 }
85 if (bn_e)
86 {
87 ByteString inE = OSSL::bn2ByteString(bn_e);
88 setE(inE);
89 }
90 }
91
92 // Setters for the RSA public key components
93 void OSSLRSAPublicKey::setN(const ByteString& inN)
94 {
95 RSAPublicKey::setN(inN);
96
97 if (rsa)
98 {
99 RSA_free(rsa);
100 rsa = NULL;
101 }
102 }
103
104 void OSSLRSAPublicKey::setE(const ByteString& inE)
105 {
106 RSAPublicKey::setE(inE);
107
108 if (rsa)
109 {
110 RSA_free(rsa);
111 rsa = NULL;
112 }
113 }
114
115 // Retrieve the OpenSSL representation of the key
116 RSA* OSSLRSAPublicKey::getOSSLKey()
117 {
118 if (rsa == NULL) createOSSLKey();
119
120 return rsa;
121 }
122
123 // Create the OpenSSL representation of the key
124 void OSSLRSAPublicKey::createOSSLKey()
125 {
126 if (rsa != NULL) return;
127
128 rsa = RSA_new();
129 if (rsa == NULL)
130 {
131 ERROR_MSG("Could not create RSA object");
132 return;
133 }
134
135 // Use the OpenSSL implementation and not any engine
136 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
137
138 #ifdef WITH_FIPS
139 if (FIPS_mode())
140 RSA_set_method(rsa, FIPS_rsa_pkcs1_ssleay());
141 else
142 RSA_set_method(rsa, RSA_PKCS1_SSLeay());
143 #else
144 RSA_set_method(rsa, RSA_PKCS1_SSLeay());
145 #endif
146
147 #else
148 RSA_set_method(rsa, RSA_PKCS1_OpenSSL());
149 #endif
150
151 BIGNUM* bn_n = OSSL::byteString2bn(n);
152 BIGNUM* bn_e = OSSL::byteString2bn(e);
153
154 RSA_set0_key(rsa, bn_n, bn_e, NULL);
155 }