| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/x509.h> |
| 16 |
| 17 | #include <stdio.h> |
| 18 | #include <sys/types.h> |
| 19 |
| 20 | #include <openssl/bn.h> |
| 21 | #include <openssl/digest.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/evp.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/obj.h> |
| 26 |
| 27 | #include "internal.h" |
| 28 |
| 29 | int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *a, |
| 30 | const ASN1_BIT_STRING *signature, void *asn, |
| 31 | EVP_PKEY *pkey) { |
| 32 | if (!pkey) { |
| 33 | OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); |
| 34 | return 0; |
| 35 | } |
| 36 |
| 37 | size_t sig_len; |
| 38 | if (signature->type == V_ASN1_BIT_STRING) { |
| 39 | if (!ASN1_BIT_STRING_num_bytes(signature, &sig_len)) { |
| 40 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_BIT_STRING_BITS_LEFT); |
| 41 | return 0; |
| 42 | } |
| 43 | } else { |
| 44 | sig_len = (size_t)ASN1_STRING_length(signature); |
| 45 | } |
| 46 |
| 47 | EVP_MD_CTX ctx; |
| 48 | uint8_t *buf_in = NULL; |
| 49 | int ret = 0, inl = 0; |
| 50 | EVP_MD_CTX_init(&ctx); |
| 51 |
| 52 | if (!x509_digest_verify_init(&ctx, a, pkey)) { |
| 53 | goto err; |
| 54 | } |
| 55 |
| 56 | inl = ASN1_item_i2d(reinterpret_cast<ASN1_VALUE *>(asn), &buf_in, it); |
| 57 |
| 58 | if (buf_in == NULL) { |
| 59 | goto err; |
| 60 | } |
| 61 |
| 62 | if (!EVP_DigestVerify(&ctx, ASN1_STRING_get0_data(signature), sig_len, buf_in, |
| 63 | inl)) { |
| 64 | OPENSSL_PUT_ERROR(X509, ERR_R_EVP_LIB); |
| 65 | goto err; |
| 66 | } |
| 67 |
| 68 | ret = 1; |
| 69 |
| 70 | err: |
| 71 | OPENSSL_free(buf_in); |
| 72 | EVP_MD_CTX_cleanup(&ctx); |
| 73 | return ret; |
| 74 | } |
| 75 |