]>
Commit | Line | Data |
---|---|---|
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 | ||
c5f87134 P |
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 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
ec577822 BM |
18 | #include <openssl/bn.h> |
19 | #include <openssl/rsa.h> | |
20 | #include <openssl/objects.h> | |
21 | #include <openssl/x509.h> | |
d02b48c6 | 22 | |
29c1f061 | 23 | int RSA_sign_ASN1_OCTET_STRING(int type, |
0f113f3e MC |
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; | |
d02b48c6 | 31 | |
0f113f3e MC |
32 | sig.type = V_ASN1_OCTET_STRING; |
33 | sig.length = m_len; | |
34 | sig.data = (unsigned char *)m; | |
d02b48c6 | 35 | |
0f113f3e MC |
36 | i = i2d_ASN1_OCTET_STRING(&sig, NULL); |
37 | j = RSA_size(rsa); | |
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); | |
8686c474 | 41 | return 0; |
0f113f3e | 42 | } |
b196e7d9 | 43 | s = OPENSSL_malloc((unsigned int)j + 1); |
0f113f3e MC |
44 | if (s == NULL) { |
45 | RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE); | |
8686c474 | 46 | return 0; |
0f113f3e MC |
47 | } |
48 | p = s; | |
49 | i2d_ASN1_OCTET_STRING(&sig, &p); | |
50 | i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING); | |
51 | if (i <= 0) | |
52 | ret = 0; | |
53 | else | |
54 | *siglen = i; | |
d02b48c6 | 55 | |
4b45c6e5 | 56 | OPENSSL_clear_free(s, (unsigned int)j + 1); |
8686c474 | 57 | return ret; |
0f113f3e | 58 | } |
d02b48c6 | 59 | |
29c1f061 | 60 | int RSA_verify_ASN1_OCTET_STRING(int dtype, |
0f113f3e MC |
61 | const unsigned char *m, |
62 | unsigned int m_len, unsigned char *sigbuf, | |
63 | unsigned int siglen, RSA *rsa) | |
64 | { | |
65 | int i, ret = 0; | |
66 | unsigned char *s; | |
67 | const unsigned char *p; | |
68 | ASN1_OCTET_STRING *sig = NULL; | |
d02b48c6 | 69 | |
0f113f3e MC |
70 | if (siglen != (unsigned int)RSA_size(rsa)) { |
71 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, | |
72 | RSA_R_WRONG_SIGNATURE_LENGTH); | |
8686c474 | 73 | return 0; |
0f113f3e | 74 | } |
d02b48c6 | 75 | |
b196e7d9 | 76 | s = OPENSSL_malloc((unsigned int)siglen); |
0f113f3e MC |
77 | if (s == NULL) { |
78 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE); | |
79 | goto err; | |
80 | } | |
81 | i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING); | |
d02b48c6 | 82 | |
0f113f3e MC |
83 | if (i <= 0) |
84 | goto err; | |
d02b48c6 | 85 | |
0f113f3e MC |
86 | p = s; |
87 | sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i); | |
88 | if (sig == NULL) | |
89 | goto err; | |
d02b48c6 | 90 | |
0f113f3e MC |
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); | |
90862ab4 | 94 | } else { |
0f113f3e | 95 | ret = 1; |
90862ab4 | 96 | } |
0f113f3e | 97 | err: |
0dfb9398 | 98 | ASN1_OCTET_STRING_free(sig); |
4b45c6e5 | 99 | OPENSSL_clear_free(s, (unsigned int)siglen); |
8686c474 | 100 | return ret; |
0f113f3e | 101 | } |