2017-02-26 6 views
1

저는 실제로 암호화 된 캘린더를 프로그래밍하고 종료합니다. 이를 위해 cryptlib을 사용하고 있습니다. manual에서 코드를 더 많이 또는 덜 복사했습니다. 그러나 항상 루트 CA를 생성하려고 할 때. cryptSignCert()에서 오류 코드 -2로 실패합니다. (즉, 설명서에 따라 두 번째 매개 변수에 문제가 있다는 의미입니다.)
다음은 문제를 재현하는 코드입니다. 사전에
cryptlib cryptSignCert가 실패했습니다.

#include <iostream> 
#include <cstring> 

#include "cryptlib.h" 

/*Generating a root ca*/ 
auto genRootCA(const char* commonName,const char* keyLabel,const char* country) -> int 
{ 
    int status; 
    CRYPT_CONTEXT cryptContext; 

    cryptCreateContext(&cryptContext, CRYPT_UNUSED, CRYPT_ALGO_RSA); 
    cryptSetAttributeString(cryptContext, CRYPT_CTXINFO_LABEL, keyLabel, strlen(keyLabel)); 
    cryptGenerateKey(cryptContext); 

    CRYPT_CERTIFICATE cryptCertificate; 
    cryptCreateCert(&cryptCertificate,CRYPT_UNUSED,CRYPT_CERTTYPE_CERTIFICATE); 
    cryptSetAttributeString(cryptCertificate,CRYPT_CERTINFO_COUNTRYNAME,country,strlen(country)); 
    cryptSetAttributeString(cryptCertificate,CRYPT_CERTINFO_COMMONNAME,commonName,strlen(commonName)); 

    //Set to self-signed 
    cryptSetAttribute(cryptCertificate,CRYPT_CERTINFO_SELFSIGNED,1); 
    cryptSetAttribute(cryptCertificate,CRYPT_CERTINFO_CA,1); 

    //Sign certificate 
    status = cryptSignCert(cryptCertificate,cryptContext); //This is, what is actually not working 
    if(cryptStatusError(status)) 
    { 
     cryptDestroyContext(cryptContext); 
     cryptDestroyCert(cryptCertificate); 
     return(status); 
    } 

    //Save data to disk....(cut out) 
} 

int main() 
{ 
    cryptInit(); 
    cryptAddRandom(NULL,CRYPT_RANDOM_FASTPOLL); 
    std::cout << "Generating root ca.\n"; 
    int r = genRootCA("[email protected]","Private key","DE"); 
    std::cout << "Returned value " << r << std::endl; 
    cryptEnd(); 
} 

감사합니다, 데이비드.

+0

* "매뉴얼에서 코드를 더 많이 또는 덜 복사했습니다."* 이제 어떻게 될까요? 어떤 차이점을 발견하기 위해 코드와 설명서를 검토해야합니까? 질문을 [편집]하여 [mcve]를 제공해주십시오. –

+0

또한 더 나은 제목을 시도해보십시오. * "작동하지 않는다"*는 가장 유용한 유용한 문제에 대한 설명입니다. –

+0

죄송 합니다만 귀하의 요지는 보이지 않습니다. 나는 전에 cryptlib을 사용한 적이 없기 때문에 어떻게 올바르게 작동하는지 전혀 모릅니다. 그리고 매뉴얼을 보면, 복사하고 붙여 넣을 수있는 코드가 없다는 것을 알 수 있습니다. –

답변

1

마침내 문제의 해결책을 발견했습니다. 공개 키를 인증서에 추가하는 것을 잊었습니다. 다음은 작동하는 예제 코드입니다.

#include <iostream> 
#include <cstring> 

#include "cryptlib.h" 

/* generating the root ca */ 
auto genRootCA(const char* commonName,const char* keyLabel, const char* country,const char* path, const char* password) -> int 
{ 
    int status; 
    CRYPT_CONTEXT cryptContext; 

    cryptCreateContext(&cryptContext, CRYPT_UNUSED, CRYPT_ALGO_RSA); 

    cryptSetAttributeString(cryptContext, CRYPT_CTXINFO_LABEL, keyLabel, strlen(keyLabel)); 

    cryptGenerateKey(cryptContext); 

    CRYPT_CERTIFICATE cryptCertificate; 
    cryptCreateCert(&cryptCertificate,CRYPT_UNUSED,CRYPT_CERTTYPE_CERTIFICATE); 

    /* Add the public key */ 
    status = cryptSetAttribute(cryptCertificate, 
    CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO, cryptContext); 

    cryptSetAttributeString(cryptCertificate,CRYPT_CERTINFO_COUNTRYNAME,country,strlen(country)); 

    cryptSetAttributeString(cryptCertificate,CRYPT_CERTINFO_COMMONNAME,commonName,strlen(commonName)); 

    //Set to self-signed 
    cryptSetAttribute(cryptCertificate,CRYPT_CERTINFO_SELFSIGNED,1); 
    cryptSetAttribute(cryptCertificate,CRYPT_CERTINFO_CA,1); 

    //Sign certificate 
    status = cryptSignCert(cryptCertificate,cryptContext); //Works now 
    if(cryptStatusError(status)) 
    { 
     cryptDestroyContext(cryptContext); 
     cryptDestroyCert(cryptCertificate); 
     return(status); 
    } 

    //Saving data to disk (cut out) 

    return CRYPT_OK; 
} 

int main() 
{ 
    cryptInit(); 
    cryptAddRandom(NULL,CRYPT_RANDOM_FASTPOLL); 
    std::cout << "Generating root ca.\n"; 
    int r = genRootCA("[email protected]","Private key","DE","key.pem","abc"); 
    std::cout << "Returned value " << r << std::endl; 
    cryptEnd(); 
} 

나는 이것이 동일한 문제를 가진 다른 사람들을 돕기를 바랍니다.