authme-unix/src/mpass_openssl.c
authme_err_t
mpass_openssl_gen_and_save_master_password(char * outfile, char * password, mpass_handle * out)
{

	EVP_PKEY_CTX *ctx;
	EVP_PKEY *pkey = NULL;
    EVP_PKEY *pkey_pub = NULL;
    RSA * rsa = NULL;
    RSA * rsa_pub = NULL;
    FILE * outfp;
    authme_master_password_t * amp;

    /* First we generate the new key */
	if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)) == NULL)
	{
		return AUTHME_ERR_CRYPTO_OPERATION;
	}

	if (EVP_PKEY_keygen_init(ctx) <= 0 ||
		EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0 ||
		EVP_PKEY_keygen(ctx, &pkey) <= 0)
	{
		EVP_PKEY_CTX_free(ctx);
		return AUTHME_ERR_CRYPTO_OPERATION;
	}

    /* Save in the output file */
    if ((outfp = fopen(outfile, "wb")) == NULL)
    {
        EVP_PKEY_free(pkey);
        EVP_PKEY_CTX_free(ctx);
        return AUTHME_ERR_INVALID_FILENAME;
    }

    int res;

    /* Do we have a password? */
    if (password == NULL || password[0] == '\0')
    {
        /* Have no password */
        res = i2d_PKCS8PrivateKey_fp(
            outfp,
            pkey,
            NULL,
            NULL, 0,   /* kstr arg */
            NULL,
            NULL);
    }
    else
    {
        int nid = OBJ_txt2nid("PBE-SHA1-3DES");
        res = i2d_PKCS8PrivateKey_nid_fp(
            outfp,
            pkey,
            nid,
            NULL,
            0,
            NULL,
            password);
    }

    fclose(outfp);

    if (!res)
    {
        /* Something went wrong */
        EVP_PKEY_free(pkey);
        EVP_PKEY_CTX_free(ctx);
        return AUTHME_ERR_CRYPTO_OPERATION;
    }

    /* All Good! */
	amp = (authme_master_password_t *)malloc(sizeof(authme_master_password_t));
	amp->amp_service_key = pkey;
	amp->amp_last_error = NULL;
	amp->amp_service_public_key = NULL;

    /* Create the pub key just in case */
    pkey_pub = EVP_PKEY_new();

    rsa = EVP_PKEY_get1_RSA(pkey);
    rsa_pub = RSAPublicKey_dup(rsa);
    RSA_free(rsa);

    EVP_PKEY_set1_RSA(pkey_pub, rsa_pub);

    amp->amp_service_public_key = pkey_pub;

    *out = amp;
    return AUTHME_ERR_OK;
}
/* ---------------------------------------------------------------- *
* Generate a new RSA key and store in a PKCS8 format file
* ---------------------------------------------------------------- */
Generate a new RSA key and store in a PKCS8 format file
Generate a new RSA key and store in a PKCS8 format file
authme_err_t
mpass_openssl_gen_and_save_master_password(char * outfile, char * password, mpass_handle * out)
{
	EVP_PKEY_CTX *ctx;
	EVP_PKEY *pkey = NULL;
    EVP_PKEY *pkey_pub = NULL;
    RSA * rsa = NULL;
    RSA * rsa_pub = NULL;
    FILE * outfp;
    authme_master_password_t * amp;
	if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)) == NULL)
	{
		return AUTHME_ERR_CRYPTO_OPERATION;
	}
	if (EVP_PKEY_keygen_init(ctx) <= 0 ||
		EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0 ||
		EVP_PKEY_keygen(ctx, &pkey) <= 0)
	{
		EVP_PKEY_CTX_free(ctx);
		return AUTHME_ERR_CRYPTO_OPERATION;
	}
    if ((outfp = fopen(outfile, "wb")) == NULL)
    {
        EVP_PKEY_free(pkey);
        EVP_PKEY_CTX_free(ctx);
        return AUTHME_ERR_INVALID_FILENAME;
    }
    int res;
    if (password == NULL || password[0] == '\0')
    {
        res = i2d_PKCS8PrivateKey_fp(
            outfp,
            pkey,
            NULL,
            NULL, 0,   
            NULL,
            NULL);
    }
    else
    {
        int nid = OBJ_txt2nid("PBE-SHA1-3DES");
        res = i2d_PKCS8PrivateKey_nid_fp(
            outfp,
            pkey,
            nid,
            NULL,
            0,
            NULL,
            password);
    }
    fclose(outfp);
    if (!res)
    {
        EVP_PKEY_free(pkey);
        EVP_PKEY_CTX_free(ctx);
        return AUTHME_ERR_CRYPTO_OPERATION;
    }
	amp = (authme_master_password_t *)malloc(sizeof(authme_master_password_t));
	amp->amp_service_key = pkey;
	amp->amp_last_error = NULL;
	amp->amp_service_public_key = NULL;
    pkey_pub = EVP_PKEY_new();
    rsa = EVP_PKEY_get1_RSA(pkey);
    rsa_pub = RSAPublicKey_dup(rsa);
    RSA_free(rsa);
    EVP_PKEY_set1_RSA(pkey_pub, rsa_pub);
    amp->amp_service_public_key = pkey_pub;
    *out = amp;
    return AUTHME_ERR_OK;
}
authme_err_t mpass_openssl_gen_and_save_master_password ( char * outfile , char * password , mpass_handle * out ) { EVP_PKEY_CTX * ctx ; EVP_PKEY * pkey = NULL ; EVP_PKEY * pkey_pub = NULL ; RSA * rsa = NULL ; RSA * rsa_pub = NULL ; FILE * outfp ; authme_master_password_t * amp ; if ( ( ctx = EVP_PKEY_CTX_new_id ( EVP_PKEY_RSA , NULL ) ) == NULL ) { return AUTHME_ERR_CRYPTO_OPERATION ; } if ( EVP_PKEY_keygen_init ( ctx ) <= 0 || EVP_PKEY_CTX_set_rsa_keygen_bits ( ctx , 2048 ) <= 0 || EVP_PKEY_keygen ( ctx , & pkey ) <= 0 ) { EVP_PKEY_CTX_free ( ctx ) ; return AUTHME_ERR_CRYPTO_OPERATION ; } if ( ( outfp = fopen ( outfile , " " ) ) == NULL ) { EVP_PKEY_free ( pkey ) ; EVP_PKEY_CTX_free ( ctx ) ; return AUTHME_ERR_INVALID_FILENAME ; } int res ; if ( password == NULL || password [ 0 ] == ' \0 ' ) { res = i2d_PKCS8PrivateKey_fp ( outfp , pkey , NULL , NULL , 0 , NULL , NULL ) ; } else { int nid = OBJ_txt2nid ( " " ) ; res = i2d_PKCS8PrivateKey_nid_fp ( outfp , pkey , nid , NULL , 0 , NULL , password ) ; } fclose ( outfp ) ; if ( ! res ) { EVP_PKEY_free ( pkey ) ; EVP_PKEY_CTX_free ( ctx ) ; return AUTHME_ERR_CRYPTO_OPERATION ; } amp = ( authme_master_password_t * ) malloc ( sizeof ( authme_master_password_t ) ) ; amp -> amp_service_key = pkey ; amp -> amp_last_error = NULL ; amp -> amp_service_public_key = NULL ; pkey_pub = EVP_PKEY_new ( ) ; rsa = EVP_PKEY_get1_RSA ( pkey ) ; rsa_pub = RSAPublicKey_dup ( rsa ) ; RSA_free ( rsa ) ; EVP_PKEY_set1_RSA ( pkey_pub , rsa_pub ) ; amp -> amp_service_public_key = pkey_pub ; * out = amp ; return AUTHME_ERR_OK ; }
Generate a new RSA key and store in a PKCS8 format file
Generate a new RSA key and store in a PKCS8 format file
/* First we generate the new key */ /* Save in the output file */ /* Do we have a password? */ /* Have no password */ /* kstr arg */ /* Something went wrong */ /* All Good! */ /* Create the pub key just in case */