demo_sm2_private_key_parse.c 886 B

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