Coverage Report

Created: 2018-08-29 13:53

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