1/* ==================================================================== 2 * Copyright (c) 2003 The OpenSSL Project. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. All advertising materials mentioning features or use of this 17 * software must display the following acknowledgment: 18 * "This product includes software developed by the OpenSSL Project 19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 20 * 21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 22 * endorse or promote products derived from this software without 23 * prior written permission. For written permission, please contact 24 * openssl-core@openssl.org. 25 * 26 * 5. Products derived from this software may not be called "OpenSSL" 27 * nor may "OpenSSL" appear in their names without prior written 28 * permission of the OpenSSL Project. 29 * 30 * 6. Redistributions of any form whatsoever must retain the following 31 * acknowledgment: 32 * "This product includes software developed by the OpenSSL Project 33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 34 * 35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 * OF THE POSSIBILITY OF SUCH DAMAGE. 47 * 48 */ 49 50#include <openssl/fips.h> 51#include <openssl/rand.h> 52#include <openssl/fips_rand.h> 53#include <openssl/err.h> 54#include <openssl/bio.h> 55#include <openssl/hmac.h> 56#include <string.h> 57#include <limits.h> 58#include "fips_locl.h" 59 60#ifdef OPENSSL_FIPS 61 62#ifndef PATH_MAX 63#define PATH_MAX 1024 64#endif 65 66static int fips_md5_allowed = 0; 67static int fips_selftest_fail = 0; 68 69void FIPS_allow_md5(int onoff) 70 { 71 if (fips_is_started()) 72 { 73 int owning_thread = fips_is_owning_thread(); 74 75 if (!owning_thread) CRYPTO_w_lock(CRYPTO_LOCK_FIPS); 76 fips_md5_allowed = onoff; 77 if (!owning_thread) CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); 78 } 79 } 80 81int FIPS_md5_allowed(void) 82 { 83 int ret = 1; 84 if (fips_is_started()) 85 { 86 int owning_thread = fips_is_owning_thread(); 87 88 if (!owning_thread) CRYPTO_r_lock(CRYPTO_LOCK_FIPS); 89 ret = fips_md5_allowed; 90 if (!owning_thread) CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); 91 } 92 return ret; 93 } 94 95int FIPS_selftest_failed(void) 96 { 97 int ret = 0; 98 if (fips_is_started()) 99 { 100 int owning_thread = fips_is_owning_thread(); 101 102 if (!owning_thread) CRYPTO_r_lock(CRYPTO_LOCK_FIPS); 103 ret = fips_selftest_fail; 104 if (!owning_thread) CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); 105 } 106 return ret; 107 } 108 109int FIPS_selftest() 110 { 111 ERR_load_crypto_strings(); 112 113 return FIPS_selftest_sha1() 114 && FIPS_selftest_aes() 115 && FIPS_selftest_des() 116 && FIPS_selftest_rsa() 117 && FIPS_selftest_dsa(); 118 } 119 120static int FIPS_check_exe(const char *path) 121 { 122 unsigned char buf[1024]; 123 char p2[PATH_MAX]; 124 unsigned int n; 125 unsigned char mdbuf[EVP_MAX_MD_SIZE]; 126 FILE *f; 127 static char key[]="etaonrishdlcupfm"; 128 HMAC_CTX hmac; 129 const char *sha1_fmt="%s.sha1"; 130 131 f=fopen(path,"rb"); 132#ifdef __CYGWIN32__ 133 /* cygwin scrupulously strips .exe extentions:-( as of now it's 134 actually no point to attempt above fopen, but we keep the call 135 just in case the behavior changes in the future... */ 136 if (!f) 137 { 138 sha1_fmt="%s.exe.sha1"; 139 BIO_snprintf(p2,sizeof p2,"%s.exe",path); 140 f=fopen(p2,"rb"); 141 } 142#endif 143 if(!f) 144 { 145 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE); 146 return 0; 147 } 148 HMAC_Init(&hmac,key,strlen(key),EVP_sha1()); 149 while(!feof(f)) 150 { 151 n=fread(buf,1,sizeof buf,f); 152 if(ferror(f)) 153 { 154 clearerr(f); 155 fclose(f); 156 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE); 157 return 0; 158 } 159 if (n) HMAC_Update(&hmac,buf,n); 160 } 161 fclose(f); 162 HMAC_Final(&hmac,mdbuf,&n); 163 HMAC_CTX_cleanup(&hmac); 164 BIO_snprintf(p2,sizeof p2,sha1_fmt,path); 165 f=fopen(p2,"rb"); 166 if(!f || fread(buf,1,20,f) != 20) 167 { 168 if (f) fclose(f); 169 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE_DIGEST); 170 return 0; 171 } 172 fclose(f); 173 if(memcmp(buf,mdbuf,20)) 174 { 175 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_EXE_DIGEST_DOES_NOT_MATCH); 176 return 0; 177 } 178 return 1; 179 } 180 181int FIPS_mode_set(int onoff,const char *path) 182 { 183 void fips_set_mode(int _onoff); 184 int fips_set_owning_thread(); 185 int fips_clear_owning_thread(); 186 int ret = 0; 187 188 CRYPTO_w_lock(CRYPTO_LOCK_FIPS); 189 fips_set_started(); 190 fips_set_owning_thread(); 191 192 if(onoff) 193 { 194 unsigned char buf[24]; 195 196 fips_selftest_fail = 0; 197 198 /* Don't go into FIPS mode twice, just so we can do automagic 199 seeding */ 200 if(FIPS_mode()) 201 { 202 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET); 203 fips_selftest_fail = 1; 204 ret = 0; 205 goto end; 206 } 207 208 if(!FIPS_check_exe(path)) 209 { 210 fips_selftest_fail = 1; 211 ret = 0; 212 goto end; 213 } 214 215 /* automagically seed PRNG if not already seeded */ 216 if(!FIPS_rand_seeded()) 217 { 218 if(RAND_bytes(buf,sizeof buf) <= 0) 219 { 220 fips_selftest_fail = 1; 221 ret = 0; 222 goto end; 223 } 224 FIPS_set_prng_key(buf,buf+8); 225 FIPS_rand_seed(buf+16,8); 226 } 227 228 /* now switch into FIPS mode */ 229 fips_set_rand_check(FIPS_rand_method()); 230 RAND_set_rand_method(FIPS_rand_method()); 231 if(FIPS_selftest()) 232 fips_set_mode(1); 233 else 234 { 235 fips_selftest_fail = 1; 236 ret = 0; 237 goto end; 238 } 239 ret = 1; 240 goto end; 241 } 242 fips_set_mode(0); 243 fips_selftest_fail = 0; 244 ret = 1; 245end: 246 fips_clear_owning_thread(); 247 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); 248 return ret; 249 } 250 251#if 0 252/* here just to cause error codes to exist */ 253static void dummy() 254 { 255 FIPSerr(FIPS_F_HASH_FINAL,FIPS_F_NON_FIPS_METHOD); 256 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_FIPS_SELFTEST_FAILED); 257 } 258#endif 259 260#endif 261