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 #include <stdlib.h>
33 #define mbedtls_snprintf        snprintf
34 #define mbedtls_printf          printf
35 #define mbedtls_exit            exit
36 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39 
40 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) ||  \
41     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) ||        \
42     !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) ||    \
43     !defined(MBEDTLS_CTR_DRBG_C)
main(void)44 int main( void )
45 {
46     mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
47            "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
48            "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
49            "MBEDTLS_CTR_DRBG_C not defined.\n");
50     return( 0 );
51 }
52 #else
53 
54 #include "mbedtls/md.h"
55 #include "mbedtls/pem.h"
56 #include "mbedtls/pk.h"
57 #include "mbedtls/md.h"
58 #include "mbedtls/x509.h"
59 
60 #include <stdio.h>
61 #include <string.h>
62 
63 #if defined(MBEDTLS_CHECK_PARAMS)
64 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)65 void mbedtls_param_failed( const char *failure_condition,
66                            const char *file,
67                            int line )
68 {
69     mbedtls_printf( "%s:%i: Input param failed - %s\n",
70                     file, line, failure_condition );
71     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
72 }
73 #endif
74 
main(int argc,char * argv[])75 int main( int argc, char *argv[] )
76 {
77     FILE *f;
78     int ret = 1;
79     int exit_code = MBEDTLS_EXIT_FAILURE;
80     size_t i;
81     mbedtls_pk_context pk;
82     unsigned char hash[32];
83     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
84     char filename[512];
85 
86     mbedtls_pk_init( &pk );
87 
88     if( argc != 3 )
89     {
90         mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
91 
92 #if defined(_WIN32)
93         mbedtls_printf( "\n" );
94 #endif
95 
96         goto exit;
97     }
98 
99     mbedtls_printf( "\n  . Reading public key from '%s'", argv[1] );
100     fflush( stdout );
101 
102     if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
103     {
104         mbedtls_printf( " failed\n  ! Could not read key from '%s'\n", argv[1] );
105         mbedtls_printf( "  ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
106         goto exit;
107     }
108 
109     if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
110     {
111         mbedtls_printf( " failed\n  ! Key is not an RSA key\n" );
112         goto exit;
113     }
114 
115     mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
116 
117     /*
118      * Extract the RSA signature from the file
119      */
120     mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
121 
122     if( ( f = fopen( filename, "rb" ) ) == NULL )
123     {
124         mbedtls_printf( "\n  ! Could not open %s\n\n", filename );
125         goto exit;
126     }
127 
128 
129     i = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
130 
131     fclose( f );
132 
133     /*
134      * Compute the SHA-256 hash of the input file and
135      * verify the signature
136      */
137     mbedtls_printf( "\n  . Verifying the RSA/SHA-256 signature" );
138     fflush( stdout );
139 
140     if( ( ret = mbedtls_md_file(
141                     mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
142                     argv[2], hash ) ) != 0 )
143     {
144         mbedtls_printf( " failed\n  ! Could not open or read %s\n\n", argv[2] );
145         goto exit;
146     }
147 
148     if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
149                            buf, i ) ) != 0 )
150     {
151         mbedtls_printf( " failed\n  ! mbedtls_pk_verify returned %d\n\n", ret );
152         goto exit;
153     }
154 
155     mbedtls_printf( "\n  . OK (the signature is valid)\n\n" );
156 
157     exit_code = MBEDTLS_EXIT_SUCCESS;
158 
159 exit:
160     mbedtls_pk_free( &pk );
161 
162 #if defined(_WIN32)
163     mbedtls_printf( "  + Press Enter to exit this program.\n" );
164     fflush( stdout ); getchar();
165 #endif
166 
167     return( exit_code );
168 }
169 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
170           MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
171