rsa_encode.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*-
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/decoder.h>
  11. #include <openssl/encoder.h>
  12. #include <openssl/evp.h>
  13. /*
  14. * Example showing the encoding and decoding of RSA public and private keys. A
  15. * PEM-encoded RSA key is read in from stdin, decoded, and then re-encoded and
  16. * output for demonstration purposes. Both public and private keys are accepted.
  17. *
  18. * This can be used to load RSA keys from a file or save RSA keys to a file.
  19. */
  20. /* A property query used for selecting algorithm implementations. */
  21. static const char *propq = NULL;
  22. /*
  23. * Load a PEM-encoded RSA key from a file, optionally decrypting it with a
  24. * supplied passphrase.
  25. */
  26. static EVP_PKEY *load_key(OSSL_LIB_CTX *libctx, FILE *f, const char *passphrase)
  27. {
  28. int ret = 0;
  29. EVP_PKEY *pkey = NULL;
  30. OSSL_DECODER_CTX *dctx = NULL;
  31. int selection = 0;
  32. /*
  33. * Create PEM decoder context expecting an RSA key.
  34. *
  35. * For raw (non-PEM-encoded) keys, change "PEM" to "DER".
  36. *
  37. * The selection argument here specifies whether we are willing to accept a
  38. * public key, private key, or either. If it is set to zero, either will be
  39. * accepted. If set to EVP_PKEY_KEYPAIR, a private key will be required, and
  40. * if set to EVP_PKEY_PUBLIC_KEY, a public key will be required.
  41. */
  42. dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, "RSA",
  43. selection,
  44. libctx, propq);
  45. if (dctx == NULL) {
  46. fprintf(stderr, "OSSL_DECODER_CTX_new_for_pkey() failed\n");
  47. goto cleanup;
  48. }
  49. /*
  50. * Set passphrase if provided; needed to decrypt encrypted PEM files.
  51. * If the input is not encrypted, any passphrase provided is ignored.
  52. *
  53. * Alternative methods for specifying passphrases exist, such as a callback
  54. * (see OSSL_DECODER_CTX_set_passphrase_cb(3)), which may be more useful for
  55. * interactive applications which do not know if a passphrase should be
  56. * prompted for in advance, or for GUI applications.
  57. */
  58. if (passphrase != NULL) {
  59. if (OSSL_DECODER_CTX_set_passphrase(dctx,
  60. (const unsigned char *)passphrase,
  61. strlen(passphrase)) == 0) {
  62. fprintf(stderr, "OSSL_DECODER_CTX_set_passphrase() failed\n");
  63. goto cleanup;
  64. }
  65. }
  66. /* Do the decode, reading from file. */
  67. if (OSSL_DECODER_from_fp(dctx, f) == 0) {
  68. fprintf(stderr, "OSSL_DECODER_from_fp() failed\n");
  69. goto cleanup;
  70. }
  71. ret = 1;
  72. cleanup:
  73. OSSL_DECODER_CTX_free(dctx);
  74. /*
  75. * pkey is created by OSSL_DECODER_CTX_new_for_pkey, but we
  76. * might fail subsequently, so ensure it's properly freed
  77. * in this case.
  78. */
  79. if (ret == 0) {
  80. EVP_PKEY_free(pkey);
  81. pkey = NULL;
  82. }
  83. return pkey;
  84. }
  85. /*
  86. * Store an RSA public or private key to a file using PEM encoding.
  87. *
  88. * If a passphrase is supplied, the file is encrypted, otherwise
  89. * it is unencrypted.
  90. */
  91. static int store_key(EVP_PKEY *pkey, FILE *f, const char *passphrase)
  92. {
  93. int ret = 0;
  94. int selection;
  95. OSSL_ENCODER_CTX *ectx = NULL;
  96. /*
  97. * Create a PEM encoder context.
  98. *
  99. * For raw (non-PEM-encoded) output, change "PEM" to "DER".
  100. *
  101. * The selection argument controls whether the private key is exported
  102. * (EVP_PKEY_KEYPAIR), or only the public key (EVP_PKEY_PUBLIC_KEY). The
  103. * former will fail if we only have a public key.
  104. *
  105. * Note that unlike the decode API, you cannot specify zero here.
  106. *
  107. * Purely for the sake of demonstration, here we choose to export the whole
  108. * key if a passphrase is provided and the public key otherwise.
  109. */
  110. selection = (passphrase != NULL)
  111. ? EVP_PKEY_KEYPAIR
  112. : EVP_PKEY_PUBLIC_KEY;
  113. ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "PEM", NULL, propq);
  114. if (ectx == NULL) {
  115. fprintf(stderr, "OSSL_ENCODER_CTX_new_for_pkey() failed\n");
  116. goto cleanup;
  117. }
  118. /*
  119. * Set passphrase if provided; the encoded output will then be encrypted
  120. * using the passphrase.
  121. *
  122. * Alternative methods for specifying passphrases exist, such as a callback
  123. * (see OSSL_ENCODER_CTX_set_passphrase_cb(3), just as for OSSL_DECODER_CTX;
  124. * however you are less likely to need them as you presumably know whether
  125. * encryption is desired in advance.
  126. *
  127. * Note that specifying a passphrase alone is not enough to cause the
  128. * key to be encrypted. You must set both a cipher and a passphrase.
  129. */
  130. if (passphrase != NULL) {
  131. /* Set cipher. AES-128-CBC is a reasonable default. */
  132. if (OSSL_ENCODER_CTX_set_cipher(ectx, "AES-128-CBC", propq) == 0) {
  133. fprintf(stderr, "OSSL_ENCODER_CTX_set_cipher() failed\n");
  134. goto cleanup;
  135. }
  136. /* Set passphrase. */
  137. if (OSSL_ENCODER_CTX_set_passphrase(ectx,
  138. (const unsigned char *)passphrase,
  139. strlen(passphrase)) == 0) {
  140. fprintf(stderr, "OSSL_ENCODER_CTX_set_passphrase() failed\n");
  141. goto cleanup;
  142. }
  143. }
  144. /* Do the encode, writing to the given file. */
  145. if (OSSL_ENCODER_to_fp(ectx, f) == 0) {
  146. fprintf(stderr, "OSSL_ENCODER_to_fp() failed\n");
  147. goto cleanup;
  148. }
  149. ret = 1;
  150. cleanup:
  151. OSSL_ENCODER_CTX_free(ectx);
  152. return ret;
  153. }
  154. int main(int argc, char **argv)
  155. {
  156. int ret = EXIT_FAILURE;
  157. OSSL_LIB_CTX *libctx = NULL;
  158. EVP_PKEY *pkey = NULL;
  159. const char *passphrase_in = NULL, *passphrase_out = NULL;
  160. /* usage: rsa_encode <passphrase-in> <passphrase-out> */
  161. if (argc > 1 && argv[1][0])
  162. passphrase_in = argv[1];
  163. if (argc > 2 && argv[2][0])
  164. passphrase_out = argv[2];
  165. /* Decode PEM key from stdin and then PEM encode it to stdout. */
  166. pkey = load_key(libctx, stdin, passphrase_in);
  167. if (pkey == NULL) {
  168. fprintf(stderr, "Failed to decode key\n");
  169. goto cleanup;
  170. }
  171. if (store_key(pkey, stdout, passphrase_out) == 0) {
  172. fprintf(stderr, "Failed to encode key\n");
  173. goto cleanup;
  174. }
  175. ret = EXIT_SUCCESS;
  176. cleanup:
  177. EVP_PKEY_free(pkey);
  178. OSSL_LIB_CTX_free(libctx);
  179. return ret;
  180. }