'소프트웨어/보안 프로그램밍 관련'에 해당되는 글 4건

 

블로그 이미지

맨오브파워

한계를 뛰어 넘어서..........

,

다음 내용은 Windows 환경에서 OpenSSL을 직접 컴파일 하여 설치하는 방법에 대해서 설명하고 있습니다. 


※ 들어가기에 앞서 ...

    설명의 편의상 아래와 같은 가정을 전제하고 읽어주세요~ ^^

    openssl-0.9.8k.tar.gz 파일은 H:\openssl-0.9.8k 디렉토리에 압축이 풀렸음.
    OpenSSL 설치 디렉토리는 C:\OpenSSL 이라고 가정함.

1. OpenSSL 다운로드

    http://www.openssl.org/source/openssl-0.9.8k.tar.gz


2. ActivePerl 설치

    openssl 라이브러리를 컴파일 하기 위해서는 perl 이 필요합니다.

    http://downloads.activestate.com/ActivePerl/Windows/5.10/ActivePerl-5.10.0.1004-MSWin32-x86-287188.msi

    다운로드 받은 ActivePerl을 설치합니다. (※ 설치시 Next 버튼만 계속 누르면 됩니다. ^^)


3. 전반적인 컴파일 환경 구축

    압축이 해제된 디렉토리로 이동하여, 아래와 같이 명령어를 입력합니다.

 

    1) x86 static library

        perl Configure VC-WIN32 --openssldir=C:\OpenSSL-x86 no-shared no-asm threads no-idea no-mdc2 no-rc5

 

    2) x86 static debug library

        perl Configure debug-VC-WIN32 --openssldir=C:\OpenSSL-x86-debug no-shared no-asm threads no-idea no-mdc2 no-rc5

 

    3) x64 static library

        perl Configure VC-WIN64A --openssldir=C:\OpenSSL-x64 no-shared no-asm threads no-idea no-mdc2 no-rc5

 

    4) x64 static debug library

        perl Configure debug-VC-WIN64A --openssldir=C:\OpenSSL-x64-debug no-shared no-asm threads no-idea no-mdc2 no-rc5

 

    ※ 참고로 --openssldir=C:\OpenSSL 과 같이 입력하면 "C:\OpenSSL" 디렉토리에 라이브러리가 설치됩니다.

    ※ no-idea no-mdc2 no-rc5 는 암호화 알고리즘 라이센스 문제로 인하여 빌드에서 제외시킵니다.

   

    


4. 구체적인 컴파일 환경 구축

    컴파일 환경은 다음과 같이 3가지 유형중 한 가지를 선택하여 사용이 가능합니다.

 

    1) x86

        - 어셈블리어를 사용하지 않는 경우       ms\do_ms.bat          

        - NASM 어셈블리어를 사용하는 경우    ms\do_nasm.bat    

        - MASM 어셈블리어를 사용하는 경우   ms\do_masm.bat

 

    2) x64

        - 어셈블리어를 사용하지 않는 경우     ms\do_win64a.bat

 

    3) Itanium
        - 어셈블리어를 사용하지 않는 경우 ms\do_win64i.bat

 

 

    만약, 어셈블리어를 사용하지 않을 경우에는, 아래와 같이 명령어를 입력합니다.

   


5. 소스 수정

    윈도우 환경에서는 아래와 같은 파일을, OpenSSL을 컴파일 하기 이전에 수정해 주어야 합니다.

    (아마 다음 OpenSSL 버전에서는 필요없는 과정이 될 수도 있겠군요.. ^^;;)


    1) openssl-0.9.8k\crypto\x509v3\v3_pci.c

        소스파일 최상단에 위치한 주석문에 특수문자가 포함되어 있어서 컴파일 오류가 발생하기 때문에,

        소스파일 가장 위에 주석문을 제거하고 저장함.


    2) openssl-0.9.8k\crypto\x509v3\v3_pcia.c

        소스파일 최상단에 위치한 주석문에 특수문자가 포함되어 있어서 컴파일 오류가 발생하기 때문에,

        소스파일 가장 위에 주석문을 제거하고 저장함.


    3) openssl-0.9.8k\crypto\cversion.c

        cversion.c 105 번째 라인을 아래와 같이 수정함.

        [수정 전] return "OPENSSLDIR: \"" OPENSSLDIR "\"";

        [수정 후] return "OPENSSLDIR: \" OPENSSLDIR \"";


    4) openssl-0.9.8k\crypto\cryptlib.h

        cryptlib.h 84~86번째 라인을 아래와 같이 수정함.

        


    5) openssl-0.9.8k\crypto\opensslconf.h 

        opensslconf.h 107~108번째 라인을 아래와 같이 수정함.

       

 

 

6. Makefile 수정

    디버그 모드로 빌드할 때만..

   

    


 

 

7. 컴파일 및 설치

    정적 라이브러리 빌드 인 경우 : nmake -f ms\nt.mak install

    동적 라이브러리 빌드 인 경우 : nmake -f ms\ntdll.mak install

    만약, 동적 라이브러리 컴파일 및 설치를 위해서는 아래와 같이 명령어를 입력합니다.

   

 

 

8. 설치 결과

    첨부된 "OpenSSL.zip"파일은 윈도우 환경에서 OpenSSL을 컴파일한 결과물을 압축한 파일입니다.

 

     OpenSSL.zip

 

 

9. 주의사항

 

    I've compiled a program under Windows and it crashes: why?


    This is usually because you've missed the comment in INSTALL.W32.

    Your application must link against the same version of the Win32 C-Runtime against which your openssl libraries were linked.

    The default version for OpenSSL is /MD - "Multithreaded DLL".

 

    If you are using Microsoft Visual C++'s IDE (Visual Studio), in many cases,

    your new project most likely defaulted to "Debug Singlethreaded" - /ML.

 

    This is NOT interchangeable with /MD and your program will crash, typically on the first BIO related read or write operation.

 

    For each of the six possible link stage configurations within Win32,

    your application must link against the same by which OpenSSL was built.

 

    If you are using MS Visual C++ (Studio) this can be changed by:


    1. Select Settings... from the Project Menu.
    2. Select the C/C++ Tab.
    3. Select "Code Generation from the "Category" drop down list box
    4. Select the Appropriate library (see table below) from the "Use run-time library" drop down list box. 

       Perform this step for both your debug and release versions of your application

       (look at the top left of the settings panel to change between the two)

       Single Threaded               /ML       -  MS VC++ often defaults to this for the release version of a new project.
       Debug Single Threaded     /MLd     -  MS VC++ often defaults to this for the debug version of a new project.
       Multithreaded                   /MT
       Debug Multithreaded         /MTd
       Multithreaded DLL             /MD      -  OpenSSL defaults to this.
       Debug Multithreaded DLL   /MDd

 

       ※ Note that debug and release libraries are NOT interchangeable.

       If you built OpenSSL with /MD your application must use /MD and cannot use /MDd.


       As per 0.9.8 the above limitation is eliminated for .DLLs.

       OpenSSL .DLLs compiled with some specific run-time option [we insist on the default /MD] can be deployed with application   

       compiled with different option or even different compiler.

 

       But there is a catch! Instead of re-compiling OpenSSL toolkit, as you would have to with prior versions,

       you have to compile small C snippet with compiler and/or options of your choice.

 

       The snippet gets installed as <install-root>/include/openssl/applink.c and

       should be either added to your application project or simply #include-d in one [and only one] of your application source files.

 

       Failure to link this shim module into your application manifests itself as fatal "no OPENSSL_Applink" run-time error.

 

       An explicit reminder is due that in this situation [mixing compiler options]

       it is as important to add CRYPTO_malloc_init prior first call to OpenSSL.

블로그 이미지

맨오브파워

한계를 뛰어 넘어서..........

,

OpenSSL 관련 자료

 

> Linux Journal - An Introduction To Openssl Programming.pdf

> Openssl Command-Line Howto.pdf

> OpenSSL.pdf

 

 

OpenSSL.pdf

 

Linux Journal - An Introduction To Openssl Programming.pdf

 

Openssl Command-Line Howto.pdf

블로그 이미지

맨오브파워

한계를 뛰어 넘어서..........

,
#include "stdio.h"
#include "string.h"

#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"

int password_callback(char *buf, int size, int rwflag, void *userdata)
{
    /* For the purposes of this demonstration, the password is "ibmdw" */

    printf("*** Callback function called\n");
    strcpy(buf, "ibmdw");
    return 1;
}

int main()
{
    SSL_CTX *ctx;
    SSL *ssl;
    BIO *bio, *abio, *out, *sbio;

    int (*callback)(char *, int, int, void *) = &password_callback;

    printf("Secure Programming with the OpenSSL API, Part 4:\n");
    printf("Serving it up in a secure manner\n\n");

    SSL_load_error_strings();
    ERR_load_BIO_strings();
    ERR_load_SSL_strings();
    OpenSSL_add_all_algorithms();

    printf("Attempting to create SSL context... ");
    ctx = SSL_CTX_new(SSLv23_server_method());
    if(ctx == NULL)
    {
        printf("Failed. Aborting.\n");
        return 0;
    }

    printf("\nLoading certificates...\n");
    SSL_CTX_set_default_passwd_cb(ctx, callback);
    if(!SSL_CTX_use_certificate_file(ctx, "certificate.pem", SSL_FILETYPE_PEM))
    {
        ERR_print_errors_fp(stdout);
        SSL_CTX_free(ctx);
        return 0;
    }
    if(!SSL_CTX_use_PrivateKey_file(ctx, "private.key", SSL_FILETYPE_PEM))
    {
        ERR_print_errors_fp(stdout);
        SSL_CTX_free(ctx);
        return 0;
    }

    printf("Attempting to create BIO object... ");
    bio = BIO_new_ssl(ctx, 0);
    if(bio == NULL)
    {
        printf("Failed. Aborting.\n");
        ERR_print_errors_fp(stdout);
        SSL_CTX_free(ctx);
        return 0;
    }

    printf("\nAttempting to set up BIO for SSL...\n");
    BIO_get_ssl(bio, &ssl);
    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
    
    abio = BIO_new_accept("4422");
    BIO_set_accept_bios(abio, bio);

    printf("Waiting for incoming connection...\n");

    if(BIO_do_accept(abio) <= 0)
    {
        ERR_print_errors_fp(stdout);
        SSL_CTX_free(ctx);
        BIO_free_all(bio);
        BIO_free_all(abio);
        return;
    }

    out = BIO_pop(abio);

    if(BIO_do_handshake(out) <= 0)
    {
        printf("Handshake failed.\n");
        ERR_print_errors_fp(stdout);
        SSL_CTX_free(ctx);
        BIO_free_all(bio);
        BIO_free_all(abio);
        return;
    }

    BIO_puts(out, "Hello\n");
    BIO_flush(out);

    BIO_free_all(out);
    BIO_free_all(bio);
    BIO_free_all(abio);

    SSL_CTX_free(ctx);
}
블로그 이미지

맨오브파워

한계를 뛰어 넘어서..........

,