1 /*
2  *  RSASSA-PSS/SHA-256 signature verification program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #define mbedtls_snprintf   snprintf
33 #define mbedtls_printf     printf
34 #endif
35 
36 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) ||  \
37     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) ||        \
38     !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) ||    \
39     !defined(MBEDTLS_CTR_DRBG_C)
main(void)40 int main( void )
41 {
42     mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
43            "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
44            "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
45            "MBEDTLS_CTR_DRBG_C not defined.\n");
46     return( 0 );
47 }
48 #else
49 
50 #include "mbedtls/md.h"
51 #include "mbedtls/pem.h"
52 #include "mbedtls/pk.h"
53 #include "mbedtls/md.h"
54 #include "mbedtls/x509.h"
55 
56 #include <stdio.h>
57 #include <string.h>
58 
main(int argc,char * argv[])59 int main( int argc, char *argv[] )
60 {
61     FILE *f;
62     int ret = 1;
63     size_t i;
64     mbedtls_pk_context pk;
65     unsigned char hash[32];
66     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
67     char filename[512];
68 
69     mbedtls_pk_init( &pk );
70 
71     if( argc != 3 )
72     {
73         mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
74 
75 #if defined(_WIN32)
76         mbedtls_printf( "\n" );
77 #endif
78 
79         goto exit;
80     }
81 
82     mbedtls_printf( "\n  . Reading public key from '%s'", argv[1] );
83     fflush( stdout );
84 
85     if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
86     {
87         mbedtls_printf( " failed\n  ! Could not read key from '%s'\n", argv[1] );
88         mbedtls_printf( "  ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
89         goto exit;
90     }
91 
92     if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
93     {
94         ret = 1;
95         mbedtls_printf( " failed\n  ! Key is not an RSA key\n" );
96         goto exit;
97     }
98 
99     mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
100 
101     /*
102      * Extract the RSA signature from the file
103      */
104     ret = 1;
105     mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
106 
107     if( ( f = fopen( filename, "rb" ) ) == NULL )
108     {
109         mbedtls_printf( "\n  ! Could not open %s\n\n", filename );
110         goto exit;
111     }
112 
113 
114     i = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
115 
116     fclose( f );
117 
118     /*
119      * Compute the SHA-256 hash of the input file and
120      * verify the signature
121      */
122     mbedtls_printf( "\n  . Verifying the RSA/SHA-256 signature" );
123     fflush( stdout );
124 
125     if( ( ret = mbedtls_md_file(
126                     mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
127                     argv[2], hash ) ) != 0 )
128     {
129         mbedtls_printf( " failed\n  ! Could not open or read %s\n\n", argv[2] );
130         goto exit;
131     }
132 
133     if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
134                            buf, i ) ) != 0 )
135     {
136         mbedtls_printf( " failed\n  ! mbedtls_pk_verify returned %d\n\n", ret );
137         goto exit;
138     }
139 
140     mbedtls_printf( "\n  . OK (the signature is valid)\n\n" );
141 
142     ret = 0;
143 
144 exit:
145     mbedtls_pk_free( &pk );
146 
147 #if defined(_WIN32)
148     mbedtls_printf( "  + Press Enter to exit this program.\n" );
149     fflush( stdout ); getchar();
150 #endif
151 
152     return( ret );
153 }
154 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
155           MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
156