Home | History | Annotate | Line # | Download | only in src
      1 /*
      2  * Copyright (c) 2021 Yubico AB. All rights reserved.
      3  * Use of this source code is governed by a BSD-style
      4  * license that can be found in the LICENSE file.
      5  * SPDX-License-Identifier: BSD-2-Clause
      6  */
      7 
      8 #include <openssl/rsa.h>
      9 #include <openssl/obj_mac.h>
     10 
     11 #include "fido.h"
     12 
     13 #if defined(__GNUC__)
     14 #define PRAGMA(s) _Pragma(s)
     15 #else
     16 #define PRAGMA(s)
     17 #endif
     18 
     19 static EVP_MD *
     20 rs1_get_EVP_MD(void)
     21 {
     22 PRAGMA("GCC diagnostic push")
     23 PRAGMA("GCC diagnostic ignored \"-Wcast-qual\"")
     24 	return ((EVP_MD *)EVP_sha1());
     25 PRAGMA("GCC diagnostic pop")
     26 }
     27 
     28 int
     29 rs1_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey,
     30     const fido_blob_t *sig)
     31 {
     32 	EVP_PKEY_CTX	*pctx = NULL;
     33 	EVP_MD		*md = NULL;
     34 	int		 ok = -1;
     35 
     36 	if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
     37 		fido_log_debug("%s: EVP_PKEY_base_id", __func__);
     38 		goto fail;
     39 	}
     40 
     41 	if ((md = rs1_get_EVP_MD()) == NULL) {
     42 		fido_log_debug("%s: rs1_get_EVP_MD", __func__);
     43 		goto fail;
     44 	}
     45 
     46 	if ((pctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL ||
     47 	    EVP_PKEY_verify_init(pctx) != 1 ||
     48 	    EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PADDING) != 1 ||
     49 	    EVP_PKEY_CTX_set_signature_md(pctx, md) != 1) {
     50 		fido_log_debug("%s: EVP_PKEY_CTX", __func__);
     51 		goto fail;
     52 	}
     53 
     54 	if (EVP_PKEY_verify(pctx, sig->ptr, sig->len, dgst->ptr,
     55 	    dgst->len) != 1) {
     56 		fido_log_debug("%s: EVP_PKEY_verify", __func__);
     57 		goto fail;
     58 	}
     59 
     60 	ok = 0;
     61 fail:
     62 	EVP_PKEY_CTX_free(pctx);
     63 
     64 	return (ok);
     65 }
     66