src/LLC/x509_cert.cpp
bool X509Cert::init_cert()
    {
        int32_t ret = 0;

        /* Create the EVP_PKEY structure. */
        pkey = EVP_PKEY_new();
        if(pkey == nullptr)
            return debug::error(FUNCTION, "EVP_PKEY_new() failed");

        /* Create the RSA structure. */
        pRSA = RSA_new();
        if(pRSA == nullptr)
            return debug::error(FUNCTION, "RSA_new() failed");

        /* Generate the exponent and the RSA key. */
        BIGNUM *pBNE = BN_new();
        BN_set_word(pBNE, RSA_F4);
        ret = RSA_generate_key_ex(pRSA, nBits, pBNE, nullptr);
        BN_clear_free(pBNE);
        if(ret != 1)
            return debug::error(FUNCTION, "Unable to generate ", nBits, "-Bit RSA key.");

        /* Assign RSA to the EVP key. */
        if(!EVP_PKEY_assign_RSA(pkey, pRSA))
            return debug::error(FUNCTION, "Unable to assign ", nBits, "-Bit RSA key.");

        /* Create the X509 structure. */
        px509 = X509_new();
        if(px509 == nullptr)
            return debug::error(FUNCTION, "X509_new() failed");

        /* Set the serial number of certificate to '1'. Some open-source HTTP servers refuse to accept a certificate with a
           serial number of '0', which is the default. */
        ASN1_INTEGER_set(X509_get_serialNumber(px509), 1);

        /* Set the notBefore property to the current time and notAfter property to current time plus 365 days.*/
        X509_gmtime_adj(X509_get_notBefore(px509), 0);
        X509_gmtime_adj(X509_get_notAfter(px509), 31536000L);

        /*Set the public key for the certificate. */
        X509_set_pubkey(px509, pkey);

        /* Self-signed certificate, set the name of issuer to the name of the subject. */
        X509_NAME *name = X509_get_subject_name(px509);

        /* Provide country code "C", organization "O", and common name "CN" */
        X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (uint8_t *)"US", -1, -1, 0);
        X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (uint8_t *)"Nexus", -1, -1, 0);
        X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (uint8_t *)"localhost", -1, -1, 0);

        /* Set the issuer name. */
        X509_set_issuer_name(px509, name);

        /* Peform the sign. */
        X509_sign(px509, pkey, EVP_sha1());

        return true;
    }
/* Initializes and creates a new certificate signed with a unique RSA private key. */
Initializes and creates a new certificate signed with a unique RSA private key.
Initializes and creates a new certificate signed with a unique RSA private key .
bool X509Cert::init_cert()
    {
        int32_t ret = 0;
        pkey = EVP_PKEY_new();
        if(pkey == nullptr)
            return debug::error(FUNCTION, "EVP_PKEY_new() failed");
        pRSA = RSA_new();
        if(pRSA == nullptr)
            return debug::error(FUNCTION, "RSA_new() failed");
        BIGNUM *pBNE = BN_new();
        BN_set_word(pBNE, RSA_F4);
        ret = RSA_generate_key_ex(pRSA, nBits, pBNE, nullptr);
        BN_clear_free(pBNE);
        if(ret != 1)
            return debug::error(FUNCTION, "Unable to generate ", nBits, "-Bit RSA key.");
        if(!EVP_PKEY_assign_RSA(pkey, pRSA))
            return debug::error(FUNCTION, "Unable to assign ", nBits, "-Bit RSA key.");
        px509 = X509_new();
        if(px509 == nullptr)
            return debug::error(FUNCTION, "X509_new() failed");
        ASN1_INTEGER_set(X509_get_serialNumber(px509), 1);
        X509_gmtime_adj(X509_get_notBefore(px509), 0);
        X509_gmtime_adj(X509_get_notAfter(px509), 31536000L);
        X509_set_pubkey(px509, pkey);
        X509_NAME *name = X509_get_subject_name(px509);
        X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (uint8_t *)"US", -1, -1, 0);
        X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (uint8_t *)"Nexus", -1, -1, 0);
        X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (uint8_t *)"localhost", -1, -1, 0);
        X509_set_issuer_name(px509, name);
        X509_sign(px509, pkey, EVP_sha1());
        return true;
    }
bool X509Cert :: init_cert ( ) { int32_t ret = 0 ; pkey = EVP_PKEY_new ( ) ; if ( pkey == nullptr ) return debug :: error ( FUNCTION , " " ) ; pRSA = RSA_new ( ) ; if ( pRSA == nullptr ) return debug :: error ( FUNCTION , " " ) ; BIGNUM * pBNE = BN_new ( ) ; BN_set_word ( pBNE , RSA_F4 ) ; ret = RSA_generate_key_ex ( pRSA , nBits , pBNE , nullptr ) ; BN_clear_free ( pBNE ) ; if ( ret != 1 ) return debug :: error ( FUNCTION , " " , nBits , " " ) ; if ( ! EVP_PKEY_assign_RSA ( pkey , pRSA ) ) return debug :: error ( FUNCTION , " " , nBits , " " ) ; px509 = X509_new ( ) ; if ( px509 == nullptr ) return debug :: error ( FUNCTION , " " ) ; ASN1_INTEGER_set ( X509_get_serialNumber ( px509 ) , 1 ) ; X509_gmtime_adj ( X509_get_notBefore ( px509 ) , 0 ) ; X509_gmtime_adj ( X509_get_notAfter ( px509 ) , 31536000L ) ; X509_set_pubkey ( px509 , pkey ) ; X509_NAME * name = X509_get_subject_name ( px509 ) ; X509_NAME_add_entry_by_txt ( name , " " , MBSTRING_ASC , ( uint8_t * ) " " , -1 , -1 , 0 ) ; X509_NAME_add_entry_by_txt ( name , " " , MBSTRING_ASC , ( uint8_t * ) " " , -1 , -1 , 0 ) ; X509_NAME_add_entry_by_txt ( name , " " , MBSTRING_ASC , ( uint8_t * ) " " , -1 , -1 , 0 ) ; X509_set_issuer_name ( px509 , name ) ; X509_sign ( px509 , pkey , EVP_sha1 ( ) ) ; return true ; }
Initializes and creates a new certificate signed with a unique RSA private key.
Initializes and creates a new certificate signed with a unique RSA private key .
/* Create the EVP_PKEY structure. */ /* Create the RSA structure. */ /* Generate the exponent and the RSA key. */ /* Assign RSA to the EVP key. */ /* Create the X509 structure. */ /* Set the serial number of certificate to '1'. Some open-source HTTP servers refuse to accept a certificate with a
           serial number of '0', which is the default. */ /* Set the notBefore property to the current time and notAfter property to current time plus 365 days.*/ /*Set the public key for the certificate. */ /* Self-signed certificate, set the name of issuer to the name of the subject. */ /* Provide country code "C", organization "O", and common name "CN" */ /* Set the issuer name. */ /* Peform the sign. */