]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_saos.c
Following the license change, modify the boilerplates in crypto/rsa/
[thirdparty/openssl.git] / crypto / rsa / rsa_saos.c
CommitLineData
2039c421 1/*
8686c474 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
2a7b6f39 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/bn.h>
13#include <openssl/rsa.h>
14#include <openssl/objects.h>
15#include <openssl/x509.h>
d02b48c6 16
29c1f061 17int RSA_sign_ASN1_OCTET_STRING(int type,
0f113f3e
MC
18 const unsigned char *m, unsigned int m_len,
19 unsigned char *sigret, unsigned int *siglen,
20 RSA *rsa)
21{
22 ASN1_OCTET_STRING sig;
23 int i, j, ret = 1;
24 unsigned char *p, *s;
d02b48c6 25
0f113f3e
MC
26 sig.type = V_ASN1_OCTET_STRING;
27 sig.length = m_len;
28 sig.data = (unsigned char *)m;
d02b48c6 29
0f113f3e
MC
30 i = i2d_ASN1_OCTET_STRING(&sig, NULL);
31 j = RSA_size(rsa);
32 if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
33 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,
34 RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
8686c474 35 return 0;
0f113f3e 36 }
b196e7d9 37 s = OPENSSL_malloc((unsigned int)j + 1);
0f113f3e
MC
38 if (s == NULL) {
39 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
8686c474 40 return 0;
0f113f3e
MC
41 }
42 p = s;
43 i2d_ASN1_OCTET_STRING(&sig, &p);
44 i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
45 if (i <= 0)
46 ret = 0;
47 else
48 *siglen = i;
d02b48c6 49
4b45c6e5 50 OPENSSL_clear_free(s, (unsigned int)j + 1);
8686c474 51 return ret;
0f113f3e 52}
d02b48c6 53
29c1f061 54int RSA_verify_ASN1_OCTET_STRING(int dtype,
0f113f3e
MC
55 const unsigned char *m,
56 unsigned int m_len, unsigned char *sigbuf,
57 unsigned int siglen, RSA *rsa)
58{
59 int i, ret = 0;
60 unsigned char *s;
61 const unsigned char *p;
62 ASN1_OCTET_STRING *sig = NULL;
d02b48c6 63
0f113f3e
MC
64 if (siglen != (unsigned int)RSA_size(rsa)) {
65 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,
66 RSA_R_WRONG_SIGNATURE_LENGTH);
8686c474 67 return 0;
0f113f3e 68 }
d02b48c6 69
b196e7d9 70 s = OPENSSL_malloc((unsigned int)siglen);
0f113f3e
MC
71 if (s == NULL) {
72 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
73 goto err;
74 }
75 i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
d02b48c6 76
0f113f3e
MC
77 if (i <= 0)
78 goto err;
d02b48c6 79
0f113f3e
MC
80 p = s;
81 sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i);
82 if (sig == NULL)
83 goto err;
d02b48c6 84
0f113f3e
MC
85 if (((unsigned int)sig->length != m_len) ||
86 (memcmp(m, sig->data, m_len) != 0)) {
87 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, RSA_R_BAD_SIGNATURE);
90862ab4 88 } else {
0f113f3e 89 ret = 1;
90862ab4 90 }
0f113f3e 91 err:
0dfb9398 92 ASN1_OCTET_STRING_free(sig);
4b45c6e5 93 OPENSSL_clear_free(s, (unsigned int)siglen);
8686c474 94 return ret;
0f113f3e 95}