1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * RSA low level APIs are deprecated for public use, but still ok for
12e1051a39Sopenharmony_ci * internal use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <stdio.h>
17e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
18e1051a39Sopenharmony_ci#include <openssl/bn.h>
19e1051a39Sopenharmony_ci#include <openssl/rsa.h>
20e1051a39Sopenharmony_ci#include <openssl/objects.h>
21e1051a39Sopenharmony_ci#include <openssl/x509.h>
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ciint RSA_sign_ASN1_OCTET_STRING(int type,
24e1051a39Sopenharmony_ci                               const unsigned char *m, unsigned int m_len,
25e1051a39Sopenharmony_ci                               unsigned char *sigret, unsigned int *siglen,
26e1051a39Sopenharmony_ci                               RSA *rsa)
27e1051a39Sopenharmony_ci{
28e1051a39Sopenharmony_ci    ASN1_OCTET_STRING sig;
29e1051a39Sopenharmony_ci    int i, j, ret = 1;
30e1051a39Sopenharmony_ci    unsigned char *p, *s;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci    sig.type = V_ASN1_OCTET_STRING;
33e1051a39Sopenharmony_ci    sig.length = m_len;
34e1051a39Sopenharmony_ci    sig.data = (unsigned char *)m;
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    i = i2d_ASN1_OCTET_STRING(&sig, NULL);
37e1051a39Sopenharmony_ci    j = RSA_size(rsa);
38e1051a39Sopenharmony_ci    if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
39e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
40e1051a39Sopenharmony_ci        return 0;
41e1051a39Sopenharmony_ci    }
42e1051a39Sopenharmony_ci    s = OPENSSL_malloc((unsigned int)j + 1);
43e1051a39Sopenharmony_ci    if (s == NULL) {
44e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
45e1051a39Sopenharmony_ci        return 0;
46e1051a39Sopenharmony_ci    }
47e1051a39Sopenharmony_ci    p = s;
48e1051a39Sopenharmony_ci    i2d_ASN1_OCTET_STRING(&sig, &p);
49e1051a39Sopenharmony_ci    i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
50e1051a39Sopenharmony_ci    if (i <= 0)
51e1051a39Sopenharmony_ci        ret = 0;
52e1051a39Sopenharmony_ci    else
53e1051a39Sopenharmony_ci        *siglen = i;
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci    OPENSSL_clear_free(s, (unsigned int)j + 1);
56e1051a39Sopenharmony_ci    return ret;
57e1051a39Sopenharmony_ci}
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_ciint RSA_verify_ASN1_OCTET_STRING(int dtype,
60e1051a39Sopenharmony_ci                                 const unsigned char *m,
61e1051a39Sopenharmony_ci                                 unsigned int m_len, unsigned char *sigbuf,
62e1051a39Sopenharmony_ci                                 unsigned int siglen, RSA *rsa)
63e1051a39Sopenharmony_ci{
64e1051a39Sopenharmony_ci    int i, ret = 0;
65e1051a39Sopenharmony_ci    unsigned char *s;
66e1051a39Sopenharmony_ci    const unsigned char *p;
67e1051a39Sopenharmony_ci    ASN1_OCTET_STRING *sig = NULL;
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci    if (siglen != (unsigned int)RSA_size(rsa)) {
70e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
71e1051a39Sopenharmony_ci        return 0;
72e1051a39Sopenharmony_ci    }
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci    s = OPENSSL_malloc((unsigned int)siglen);
75e1051a39Sopenharmony_ci    if (s == NULL) {
76e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
77e1051a39Sopenharmony_ci        goto err;
78e1051a39Sopenharmony_ci    }
79e1051a39Sopenharmony_ci    i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci    if (i <= 0)
82e1051a39Sopenharmony_ci        goto err;
83e1051a39Sopenharmony_ci
84e1051a39Sopenharmony_ci    p = s;
85e1051a39Sopenharmony_ci    sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i);
86e1051a39Sopenharmony_ci    if (sig == NULL)
87e1051a39Sopenharmony_ci        goto err;
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    if (((unsigned int)sig->length != m_len) ||
90e1051a39Sopenharmony_ci        (memcmp(m, sig->data, m_len) != 0)) {
91e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
92e1051a39Sopenharmony_ci    } else {
93e1051a39Sopenharmony_ci        ret = 1;
94e1051a39Sopenharmony_ci    }
95e1051a39Sopenharmony_ci err:
96e1051a39Sopenharmony_ci    ASN1_OCTET_STRING_free(sig);
97e1051a39Sopenharmony_ci    OPENSSL_clear_free(s, (unsigned int)siglen);
98e1051a39Sopenharmony_ci    return ret;
99e1051a39Sopenharmony_ci}
100