Blame crypto/rsa/rsa_asn1.c

Packit Service 084de1
/*
Packit Service 084de1
 * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
Packit Service 084de1
 *
Packit Service 084de1
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit Service 084de1
 * this file except in compliance with the License.  You can obtain a copy
Packit Service 084de1
 * in the file LICENSE in the source distribution or at
Packit Service 084de1
 * https://www.openssl.org/source/license.html
Packit Service 084de1
 */
Packit Service 084de1
Packit Service 084de1
#include <stdio.h>
Packit Service 084de1
#include "internal/cryptlib.h"
Packit Service 084de1
#include <openssl/bn.h>
Packit Service 084de1
#include <openssl/x509.h>
Packit Service 084de1
#include <openssl/asn1t.h>
Packit Service 084de1
#include "rsa_local.h"
Packit Service 084de1
Packit Service 084de1
/*
Packit Service 084de1
 * Override the default free and new methods,
Packit Service 084de1
 * and calculate helper products for multi-prime
Packit Service 084de1
 * RSA keys.
Packit Service 084de1
 */
Packit Service 084de1
static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
Packit Service 084de1
                  void *exarg)
Packit Service 084de1
{
Packit Service 084de1
    if (operation == ASN1_OP_NEW_PRE) {
Packit Service 084de1
        *pval = (ASN1_VALUE *)RSA_new();
Packit Service 084de1
        if (*pval != NULL)
Packit Service 084de1
            return 2;
Packit Service 084de1
        return 0;
Packit Service 084de1
    } else if (operation == ASN1_OP_FREE_PRE) {
Packit Service 084de1
        RSA_free((RSA *)*pval);
Packit Service 084de1
        *pval = NULL;
Packit Service 084de1
        return 2;
Packit Service 084de1
    } else if (operation == ASN1_OP_D2I_POST) {
Packit Service 084de1
        if (((RSA *)*pval)->version != RSA_ASN1_VERSION_MULTI) {
Packit Service 084de1
            /* not a multi-prime key, skip */
Packit Service 084de1
            return 1;
Packit Service 084de1
        }
Packit Service 084de1
        return (rsa_multip_calc_product((RSA *)*pval) == 1) ? 2 : 0;
Packit Service 084de1
    }
Packit Service 084de1
    return 1;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
/* Based on definitions in RFC 8017 appendix A.1.2 */
Packit Service 084de1
ASN1_SEQUENCE(RSA_PRIME_INFO) = {
Packit Service 084de1
        ASN1_SIMPLE(RSA_PRIME_INFO, r, CBIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA_PRIME_INFO, d, CBIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA_PRIME_INFO, t, CBIGNUM),
Packit Service 084de1
} ASN1_SEQUENCE_END(RSA_PRIME_INFO)
Packit Service 084de1
Packit Service 084de1
ASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = {
Packit Service 084de1
        ASN1_EMBED(RSA, version, INT32),
Packit Service 084de1
        ASN1_SIMPLE(RSA, n, BIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, e, BIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, d, CBIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, p, CBIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, q, CBIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, dmp1, CBIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, dmq1, CBIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, iqmp, CBIGNUM),
Packit Service 084de1
        ASN1_SEQUENCE_OF_OPT(RSA, prime_infos, RSA_PRIME_INFO)
Packit Service 084de1
} ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey)
Packit Service 084de1
Packit Service 084de1
Packit Service 084de1
ASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = {
Packit Service 084de1
        ASN1_SIMPLE(RSA, n, BIGNUM),
Packit Service 084de1
        ASN1_SIMPLE(RSA, e, BIGNUM),
Packit Service 084de1
} ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey)
Packit Service 084de1
Packit Service 084de1
/* Free up maskHash */
Packit Service 084de1
static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
Packit Service 084de1
                      void *exarg)
Packit Service 084de1
{
Packit Service 084de1
    if (operation == ASN1_OP_FREE_PRE) {
Packit Service 084de1
        RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval;
Packit Service 084de1
        X509_ALGOR_free(pss->maskHash);
Packit Service 084de1
    }
Packit Service 084de1
    return 1;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = {
Packit Service 084de1
        ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR,0),
Packit Service 084de1
        ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR,1),
Packit Service 084de1
        ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER,2),
Packit Service 084de1
        ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER,3)
Packit Service 084de1
} ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS)
Packit Service 084de1
Packit Service 084de1
IMPLEMENT_ASN1_FUNCTIONS(RSA_PSS_PARAMS)
Packit Service 084de1
Packit Service 084de1
/* Free up maskHash */
Packit Service 084de1
static int rsa_oaep_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
Packit Service 084de1
                       void *exarg)
Packit Service 084de1
{
Packit Service 084de1
    if (operation == ASN1_OP_FREE_PRE) {
Packit Service 084de1
        RSA_OAEP_PARAMS *oaep = (RSA_OAEP_PARAMS *)*pval;
Packit Service 084de1
        X509_ALGOR_free(oaep->maskHash);
Packit Service 084de1
    }
Packit Service 084de1
    return 1;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
ASN1_SEQUENCE_cb(RSA_OAEP_PARAMS, rsa_oaep_cb) = {
Packit Service 084de1
        ASN1_EXP_OPT(RSA_OAEP_PARAMS, hashFunc, X509_ALGOR, 0),
Packit Service 084de1
        ASN1_EXP_OPT(RSA_OAEP_PARAMS, maskGenFunc, X509_ALGOR, 1),
Packit Service 084de1
        ASN1_EXP_OPT(RSA_OAEP_PARAMS, pSourceFunc, X509_ALGOR, 2),
Packit Service 084de1
} ASN1_SEQUENCE_END_cb(RSA_OAEP_PARAMS, RSA_OAEP_PARAMS)
Packit Service 084de1
Packit Service 084de1
IMPLEMENT_ASN1_FUNCTIONS(RSA_OAEP_PARAMS)
Packit Service 084de1
Packit Service 084de1
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey)
Packit Service 084de1
Packit Service 084de1
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey)
Packit Service 084de1
Packit Service 084de1
RSA *RSAPublicKey_dup(RSA *rsa)
Packit Service 084de1
{
Packit Service 084de1
    return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
RSA *RSAPrivateKey_dup(RSA *rsa)
Packit Service 084de1
{
Packit Service 084de1
    return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
Packit Service 084de1
}