Exemple #1
0
/**
 * Get all the RSA private key specifics from an ASN.1 encoded file 
 */
int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx)
{
    int offset = 7;
    uint8_t *modulus = NULL, *priv_exp = NULL, *pub_exp = NULL;
    int mod_len, priv_len, pub_len;
#ifdef CONFIG_BIGINT_CRT
    uint8_t *p = NULL, *q = NULL, *dP = NULL, *dQ = NULL, *qInv = NULL;
    int p_len, q_len, dP_len, dQ_len, qInv_len;
#endif

    /* not in der format */
    if (buf[0] != ASN1_SEQUENCE) /* basic sanity check */
    {
#ifdef CONFIG_SSL_FULL_MODE
        printf("Error: This is not a valid ASN.1 file\n");
#endif
        return X509_INVALID_PRIV_KEY;
    }

    /* initialise the RNG */
    RNG_initialize(buf, len);

    mod_len = asn1_get_int(buf, &offset, &modulus);
    pub_len = asn1_get_int(buf, &offset, &pub_exp);
    priv_len = asn1_get_int(buf, &offset, &priv_exp);

    if (mod_len <= 0 || pub_len <= 0 || priv_len <= 0)
        return X509_INVALID_PRIV_KEY;

#ifdef CONFIG_BIGINT_CRT
    p_len = asn1_get_int(buf, &offset, &p);
    q_len = asn1_get_int(buf, &offset, &q);
    dP_len = asn1_get_int(buf, &offset, &dP);
    dQ_len = asn1_get_int(buf, &offset, &dQ);
    qInv_len = asn1_get_int(buf, &offset, &qInv);

    if (p_len <= 0 || q_len <= 0 || dP_len <= 0 || dQ_len <= 0 || qInv_len <= 0)
        return X509_INVALID_PRIV_KEY;

    RSA_priv_key_new(rsa_ctx, 
            modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len,
            p, p_len, q, p_len, dP, dP_len, dQ, dQ_len, qInv, qInv_len);

    free(p);
    free(q);
    free(dP);
    free(dQ);
    free(qInv);
#else
    RSA_priv_key_new(rsa_ctx, 
            modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len);
#endif

    free(modulus);
    free(priv_exp);
    free(pub_exp);
    return X509_OK;
}
Exemple #2
0
/**
 * Get RSA key info from the private DER file. The public elements are saved
 * into the Key Ring buffer.
 * @param buf - pointer to DER file contents
 * @return - 0 if successful
 */
int get_key(const uint8_t *buf)
{
    KeyRing *pKeyfile = (KeyRing *)keyarray;
    uint8_t *pData = (uint8_t *)(pKeyfile + 1);
    int i;
    int index = 0;
    int offset = 7;
    uint8_t *modulus = NULL, *priv_exp = NULL, *pub_exp = NULL;
    int mod_len, priv_len, pub_len;

    /* not in der format */
    if (buf[0] != ASN1_SEQUENCE) /* basic sanity check */
    {
        printf("Error: This is not a valid ASN.1 file\n");
        return X509_INVALID_PRIV_KEY;
    }
    mod_len = asn1_get_int(buf, &offset, &modulus);
    pub_len = asn1_get_int(buf, &offset, &pub_exp);
    priv_len = asn1_get_int(buf, &offset, &priv_exp);

    if (mod_len <= 0 || pub_len <= 0 || priv_len <= 0)
        return X509_INVALID_PRIV_KEY;

    RSA_priv_key_new(&rsa_context,
            modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len);
    pKeyfile->rsa_modulus_size = mod_len;
    pKeyfile->rsa_exponent_size = pub_len;
    printf("mod_len=%ld pub_len=%ld priv_len=%d\n", pKeyfile->rsa_modulus_size,
            pKeyfile->rsa_exponent_size, priv_len);

    /* Copy modulus to keyfile */
    pKeyfile->rsa_modulus_offset = index;
    memcpy(&pData[index], modulus, pKeyfile->rsa_modulus_size);
    index += pKeyfile->rsa_modulus_size;
    pKeyfile->rsa_exponent_offset = index;
    /* Copy exponent to keyfile */
    memcpy(&pData[index], pub_exp, pKeyfile->rsa_exponent_size);

    printf("modulus: ");
    for (i = 0; i < pKeyfile->rsa_modulus_size; i++)
    {
        printf("%02x:", pData[pKeyfile->rsa_modulus_offset + i]);
    }
    printf("\n");
    printf("exponent: ");
    for (i = 0; i < pKeyfile->rsa_exponent_size; i++)
    {
        printf("%02x:", pData[pKeyfile->rsa_exponent_offset + i]);
    }
    printf("\n");

    free(modulus);
    free(priv_exp);
    free(pub_exp);
    return X509_OK;
}