1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdint.h>
16 #include <gmssl/rsa.h>
17 #include <gmssl/asn1.h>
18 #include <gmssl/error.h>
19
20
rsa_public_key_print(FILE * fp,int fmt,int ind,const char * label,const uint8_t * a,size_t alen)21 int rsa_public_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen)
22 {
23 const uint8_t *d;
24 size_t dlen;
25 const uint8_t *p;
26 size_t len;
27 int val;
28
29 format_print(fp, fmt, ind, "%s\n", label);
30 ind += 4;
31 if (asn1_sequence_from_der(&d, &dlen, &a, &alen) != 1) goto err;
32 if (asn1_integer_from_der(&p, &len, &d, &dlen) != 1) goto err;
33 format_bytes(fp, fmt, ind, "modulus", p, len);
34 if (asn1_int_from_der(&val, &d, &dlen) != 1) goto err;
35 format_print(fp, fmt, ind, "publicExponent: %d\n",val);
36 if (asn1_length_is_zero(dlen) != 1) goto err;
37 if (asn1_length_is_zero(alen) != 1) goto err;
38 return 1;
39 err:
40 error_print();
41 return -1;
42 }
43