Stop compiler warnings.
[oweals/openssl.git] / fips / fips.c
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
66 int FIPS_md5_allowed;
67 int FIPS_selftest_fail;
68
69 int FIPS_selftest()
70     {
71     ERR_load_crypto_strings();
72
73     return FIPS_selftest_sha1()
74         && FIPS_selftest_aes()
75         && FIPS_selftest_des()
76         && FIPS_selftest_rsa()
77         && FIPS_selftest_dsa();
78     }
79
80 static int FIPS_check_exe(const char *path)
81     {
82     unsigned char buf[1024];
83     char p2[PATH_MAX];
84     unsigned int n;
85     unsigned char mdbuf[EVP_MAX_MD_SIZE];
86     FILE *f;
87     static char key[]="etaonrishdlcupfm";
88     HMAC_CTX hmac;
89
90     f=fopen(path,"rb");
91     if(!f)
92         {
93         FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE);
94         return 0;
95         }
96     HMAC_Init(&hmac,key,strlen(key),EVP_sha1());
97     do
98         {
99         n=fread(buf,1,sizeof buf,f);
100         if(n < 0)
101             {
102             fclose(f);
103             FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE);
104             return 0;
105             }
106         HMAC_Update(&hmac,buf,n);
107         } while(n > 0);
108     fclose(f);
109     HMAC_Final(&hmac,mdbuf,&n);
110     BIO_snprintf(p2,sizeof p2,"%s.sha1",path);
111     f=fopen(p2,"rb");
112     if(!f || fread(buf,1,20,f) != 20)
113         {
114         if (f) fclose(f);
115         FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE_DIGEST);
116         return 0;
117         }
118     fclose(f);
119     if(memcmp(buf,mdbuf,20))
120         {
121         FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_EXE_DIGEST_DOES_NOT_MATCH);
122         return 0;
123         }
124     return 1;
125     }
126
127 int FIPS_mode_set(int onoff,const char *path)
128     {
129     if(onoff)
130         {
131         unsigned char buf[24];
132
133         FIPS_selftest_fail=0;
134
135         /* Don't go into FIPS mode twice, just so we can do automagic
136            seeding */
137         if(FIPS_mode)
138             {
139             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
140             FIPS_selftest_fail=1;
141             return 0;
142             }
143
144         if(!FIPS_check_exe(path))
145             {
146             FIPS_selftest_fail=1;
147             return 0;
148             }
149
150         /* automagically seed PRNG if not already seeded */
151         if(!FIPS_rand_seeded())
152             {
153             if(RAND_bytes(buf,sizeof buf) <= 0)
154                 {
155                 FIPS_selftest_fail=1;
156                 return 0;
157                 }
158             FIPS_set_prng_key(buf,buf+8);
159             FIPS_rand_seed(buf+16,8);
160             }
161
162         /* now switch into FIPS mode */
163         FIPS_rand_check=FIPS_rand_method();
164         RAND_set_rand_method(FIPS_rand_method());
165         if(FIPS_selftest())
166             FIPS_mode=1;
167         else
168             {
169             FIPS_selftest_fail=1;
170             return 0;
171             }
172         return 1;
173         }
174     FIPS_mode=0;
175     FIPS_selftest_fail=0;
176     return 1;
177     }
178
179 void FIPS_allow_md5(int onoff)
180     {
181     FIPS_md5_allowed=onoff;
182     }
183
184 #if 0
185 /* here just to cause error codes to exist */
186 static void dummy()
187     {
188     FIPSerr(FIPS_F_HASH_FINAL,FIPS_F_NON_FIPS_METHOD);
189     FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_FIPS_SELFTEST_FAILED);
190     }
191 #endif
192
193 #endif