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