|
| 1 | +const crypto = require('crypto'); |
| 2 | +var http = require('http'); |
| 3 | +var port = process.env.PORT || process.env.port || process.env.OPENSHIFT_NODEJS_PORT || 8080; |
| 4 | +var ip = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0'; |
| 5 | +var server = http.createServer(function (req, res) { |
| 6 | + const fipsMode = getFipsMode(); |
| 7 | + |
| 8 | + res.writeHead(200, {'Content-Type': 'text/plain'}); |
| 9 | + verifySupportedHash(fipsMode); |
| 10 | + res.write('Hash generation succesfully verified\n'); |
| 11 | + verifySupportedCiphers(fipsMode); |
| 12 | + res.write('Cipher generation succesfully verified\n'); |
| 13 | + res.end('\n'); |
| 14 | + |
| 15 | +}); |
| 16 | +server.listen(port); |
| 17 | +console.log('Server running on ' + ip + ':' + port); |
| 18 | + |
| 19 | +/* |
| 20 | + * Return boolean value |
| 21 | + * True if FIPS is enabled |
| 22 | + * False if Disabled |
| 23 | +*/ |
| 24 | +function getFipsMode () { |
| 25 | + return !!crypto.getFips(); |
| 26 | +} |
| 27 | + |
| 28 | +/* |
| 29 | + * Verify usage of FIPS supported hash algs |
| 30 | + * sha256 is FIPS supported |
| 31 | + * MLD isn't FIPS supported |
| 32 | +*/ |
| 33 | +function verifySupportedHash(fipsMode) { |
| 34 | + try { |
| 35 | + const hashSha256 = crypto.createHash('sha256').update('FIPS test').digest('hex'); |
| 36 | + } catch (e) { |
| 37 | + console.error("Error: SHA256 generation should be supported with FIPS."); |
| 38 | + exit.process(1); |
| 39 | + } |
| 40 | + try { |
| 41 | + crypto.createHash('md5').update('MD5 test').digest('hex'); |
| 42 | + if (fipsMode) { |
| 43 | + console.error('Error: MD5 generation should not be suscessfull with FIPS enabled.'); |
| 44 | + exit.process(1); |
| 45 | + } |
| 46 | + } catch (e) { |
| 47 | + if (!fipsMode) { |
| 48 | + console.error('Error: MD5 generation should pass without FIPS mode.'); |
| 49 | + exit.process(1); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | + * Verify usage of FIPS supported ciphers |
| 56 | + * AES is FIPS supported |
| 57 | + * 3DES with only two keys isn't FIPS supported |
| 58 | +*/ |
| 59 | +function verifySupportedCiphers(fipsMode) { |
| 60 | + try { |
| 61 | + const key = crypto.randomBytes(32); |
| 62 | + const iv = crypto.randomBytes(16); |
| 63 | + const plaintext = 'Test of AES encryption.'; |
| 64 | + const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); |
| 65 | + let encrypted = cipher.update(plaintext, 'utf8', 'hex'); |
| 66 | + encrypted += cipher.final('hex'); |
| 67 | + } catch (e) { |
| 68 | + console.error('Error: AES-256 generation should be supported with FIPS'); |
| 69 | + process.exit(1); |
| 70 | + } |
| 71 | + try { |
| 72 | + const key = crypto.randomBytes(16); |
| 73 | + const iv = crypto.randomBytes(8); |
| 74 | + crypto.createCipheriv('des-ede-cbc', key, iv); |
| 75 | + |
| 76 | + if (fipsMode) { |
| 77 | + console.error("Error: 3DES generation shoud not be succesfull with FIPS mode.") |
| 78 | + process.exit(1); |
| 79 | + } |
| 80 | + } catch (e) { |
| 81 | + if (!fipsMode) { |
| 82 | + console.error('Error: 3DES generation should be successfull without FIPS mode.',e.message); |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +} |
0 commit comments