Node.js Https 서버 구성
1. 암호화 해시 알고리즘
암호화 해시 알고리즘은 다음과 같다.
- MD5는 보안용도로 권장하지 않음
- SHA(secure hash algorithm) 사용
md5 - q 파일 이름 => md5 해쉬값을 비교가능 (파일 다운로드 변조 확인)
Hash값에 Salt (임의 문자를 추가)해서 해킹에 대한 방지
대칭 알고리즘의 종류는 다음과 같다.
- DES (Data Encryption Standard)는 보안에 취약
- AES(Advanced Encryption Standard)로 대체
대칭 암호화 : crypto. cipher/Decipher 사용
비대칭 알고리즘은 개인키와 공개키를 사용하여 비대칭으로 암호화 한다.
사설 인증서는 openssl이 있고 사용 시 경고가 발생한다.
openssl을 설치한다. 다음 명령을 이용하여 인증서를 발급한다.
- openssl genrsa -out key.pem 2048 (Private Key 생성)
- openssl req -new -key key.pem -out req.csr (인증서 발급 요청)
- openssl x509 -req -in req.csr -signkey key.pem -out cert.pem -days 365 (인증서 발급 승인)
두번째 명령어 실행 중 openssl.cnf파일을 찾을 수 없다는 오류가 발생하면 해당 파일의 위치를 명령어에 지정해야 한다.
C:\Users\jungwonkun7\내 프로그램\openssl-0.9.8k_WIN32\bin>openssl req -config ../openssl.cnf -new -key key.pem -out req.csrLoading 'screen' into random state - doneYou are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [AU]:KRState or Province Name (full name) [Some-State]:SEOULLocality Name (eg, city) []:SeoulOrganization Name (eg, company) [Internet Widgits Pty Ltd]:StormboyOrganizational Unit Name (eg, section) []:Common Name (eg, YOUR name) []:WKEmail Address []:stormboy@hanmail.netPlease enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:An optional company name []:
영리 목적으로 하는 개인 정보 수집 서비스의 경우는 보안 서버(https)가 의무화 되어 있다.
무료로 인증을 제공하는 사이트는 다음과 같다.
- https://www.startssl.com
- https://letsencrypt.org
2. https 서버 프로그램 작성
SSL 인증서와 함께 다음 코드를 작성한 후 node로 실행한다.- let http=require('http'),
- https = require('https'),
- express = require('express'),
- fs = require('fs');
- let options = {
- key: fs.readFileSync('./key.pem'),
- cert: fs.readFileSync('./cert.pem')
- };
- let port1 = 80;
- let port2 = 443;
- let app = express();
- app.use(express.urlencoded({extended: false}));
- http.createServer(app).listen(port1, function(){
- console.log("Http server listening on port " + port1);
- });
- https.createServer(options, app).listen(port2, function(){
- console.log("Https server listening on port " + port2);
- });
- app.get('/', function (req, res) {
- res.writeHead(200, {'Content-Type' : 'text/html'});
- res.write('<h3>Welcome</h3>');
- res.write('<a href="/login">Please login</a>');
- res.end();
- });
- app.get('/login', function (req, res){
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.write('<h3>Login</h3>');
- res.write('<form method="POST" action="/login">');
- res.write('<label name="userId">UserId : </label>')
- res.write('<input type="text" name="userId"><br/>');
- res.write('<label name="password">Password : </label>')
- res.write('<input type="password" name="password"><br/>');
- res.write('<input type="submit" name="login" value="Login">');
- res.write('</form>');
- res.end();
- })
- app.post('/login', function (req, res){
- let userId = req.params("userId");
- let password = req.params("password")
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.write('Thank you, '+userId+', you are now logged in.');
- res.write('<p><a href="/"> back home</a>');
- res.end();
- });
[참고 사이트]
https://www.youtube.com/watch?v=gks3CbpgtmI (Node 보안 강의)http://blog.saltfactory.net/implements-nodejs-based-https-server/ (openssl과 node.js로 https서버 만들기)
https://code.google.com/archive/p/openssl-for-windows/downloads (window용 openssl설치파일)
댓글 없음:
댓글 쓰기