2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.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>
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,
28 ASN1_OCTET_STRING sig;
32 sig.type = V_ASN1_OCTET_STRING;
34 sig.data = (unsigned char *)m;
36 i = i2d_ASN1_OCTET_STRING(&sig, NULL);
38 if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
39 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,
40 RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
43 s = OPENSSL_malloc((unsigned int)j + 1);
45 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
49 i2d_ASN1_OCTET_STRING(&sig, &p);
50 i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
56 OPENSSL_clear_free(s, (unsigned int)j + 1);
60 int RSA_verify_ASN1_OCTET_STRING(int dtype,
61 const unsigned char *m,
62 unsigned int m_len, unsigned char *sigbuf,
63 unsigned int siglen, RSA *rsa)
67 const unsigned char *p;
68 ASN1_OCTET_STRING *sig = NULL;
70 if (siglen != (unsigned int)RSA_size(rsa)) {
71 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,
72 RSA_R_WRONG_SIGNATURE_LENGTH);
76 s = OPENSSL_malloc((unsigned int)siglen);
78 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
81 i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
87 sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i);
91 if (((unsigned int)sig->length != m_len) ||
92 (memcmp(m, sig->data, m_len) != 0)) {
93 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, RSA_R_BAD_SIGNATURE);
98 ASN1_OCTET_STRING_free(sig);
99 OPENSSL_clear_free(s, (unsigned int)siglen);