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 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <gmssl/mem.h>
14 #include <gmssl/sm2.h>
15
16
main(void)17 int main(void)
18 {
19 SM2_KEY sm2_key;
20 char *password = "123456";
21 unsigned char buf[512];
22 unsigned char *p;
23 size_t len;
24
25 printf("Read SM2 private key file (PEM) from stdin ...\n");
26 if (sm2_private_key_info_decrypt_from_pem(&sm2_key, password, stdin) != 1) {
27 fprintf(stderr, "error\n");
28 return 1;
29 }
30
31 p = buf;
32 len = 0;
33 if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1) {
34 fprintf(stderr, "error\n");
35 return 1;
36 }
37 fwrite(buf, 1, len, stdout);
38
39 gmssl_secure_clear(&sm2_key, sizeof(sm2_key));
40 return 0;
41 }
42