2016-05-17 18:51:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-11-06 22:34:17 +00:00
|
|
|
int RSA_sign_ASN1_OCTET_STRING(int type,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *m, unsigned int m_len,
|
|
|
|
unsigned char *sigret, unsigned int *siglen,
|
|
|
|
RSA *rsa)
|
|
|
|
{
|
|
|
|
ASN1_OCTET_STRING sig;
|
|
|
|
int i, j, ret = 1;
|
|
|
|
unsigned char *p, *s;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
sig.type = V_ASN1_OCTET_STRING;
|
|
|
|
sig.length = m_len;
|
|
|
|
sig.data = (unsigned char *)m;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
i = i2d_ASN1_OCTET_STRING(&sig, NULL);
|
|
|
|
j = RSA_size(rsa);
|
|
|
|
if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
|
|
|
|
RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,
|
|
|
|
RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
|
|
|
|
return (0);
|
|
|
|
}
|
2015-04-28 19:28:14 +00:00
|
|
|
s = OPENSSL_malloc((unsigned int)j + 1);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
p = s;
|
|
|
|
i2d_ASN1_OCTET_STRING(&sig, &p);
|
|
|
|
i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
|
|
|
|
if (i <= 0)
|
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
*siglen = i;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(s, (unsigned int)j + 1);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-11-06 22:34:17 +00:00
|
|
|
int RSA_verify_ASN1_OCTET_STRING(int dtype,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *m,
|
|
|
|
unsigned int m_len, unsigned char *sigbuf,
|
|
|
|
unsigned int siglen, RSA *rsa)
|
|
|
|
{
|
|
|
|
int i, ret = 0;
|
|
|
|
unsigned char *s;
|
|
|
|
const unsigned char *p;
|
|
|
|
ASN1_OCTET_STRING *sig = NULL;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (siglen != (unsigned int)RSA_size(rsa)) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,
|
|
|
|
RSA_R_WRONG_SIGNATURE_LENGTH);
|
|
|
|
return (0);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-04-28 19:28:14 +00:00
|
|
|
s = OPENSSL_malloc((unsigned int)siglen);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (i <= 0)
|
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
p = s;
|
|
|
|
sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i);
|
|
|
|
if (sig == NULL)
|
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (((unsigned int)sig->length != m_len) ||
|
|
|
|
(memcmp(m, sig->data, m_len) != 0)) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, RSA_R_BAD_SIGNATURE);
|
|
|
|
} else
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2015-03-24 11:52:24 +00:00
|
|
|
ASN1_OCTET_STRING_free(sig);
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(s, (unsigned int)siglen);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (ret);
|
|
|
|
}
|