PKCS12_newpass.pod 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. =pod
  2. =head1 NAME
  3. PKCS12_newpass - change the password of a PKCS12 structure
  4. =head1 SYNOPSIS
  5. #include <openssl/pkcs12.h>
  6. int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass);
  7. =head1 DESCRIPTION
  8. PKCS12_newpass() changes the password of a PKCS12 structure.
  9. B<p12> is a pointer to a PKCS12 structure. B<oldpass> is the existing password
  10. and B<newpass> is the new password.
  11. =head1 NOTES
  12. Each of B<oldpass> and B<newpass> is independently interpreted as a string in
  13. the UTF-8 encoding. If it is not valid UTF-8, it is assumed to be ISO8859-1
  14. instead.
  15. In particular, this means that passwords in the locale character set
  16. (or code page on Windows) must potentially be converted to UTF-8 before
  17. use. This may include passwords from local text files, or input from
  18. the terminal or command line. Refer to the documentation of
  19. L<UI_OpenSSL(3)>, for example.
  20. =head1 RETURN VALUES
  21. PKCS12_newpass() returns 1 on success or 0 on failure. Applications can
  22. retrieve the most recent error from PKCS12_newpass() with ERR_get_error().
  23. =head1 EXAMPLE
  24. This example loads a PKCS#12 file, changes its password and writes out
  25. the result to a new file.
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <openssl/pem.h>
  29. #include <openssl/err.h>
  30. #include <openssl/pkcs12.h>
  31. int main(int argc, char **argv)
  32. {
  33. FILE *fp;
  34. PKCS12 *p12;
  35. if (argc != 5) {
  36. fprintf(stderr, "Usage: pkread p12file password newpass opfile\n");
  37. return 1;
  38. }
  39. if ((fp = fopen(argv[1], "rb")) == NULL) {
  40. fprintf(stderr, "Error opening file %s\n", argv[1]);
  41. return 1;
  42. }
  43. p12 = d2i_PKCS12_fp(fp, NULL);
  44. fclose(fp);
  45. if (p12 == NULL) {
  46. fprintf(stderr, "Error reading PKCS#12 file\n");
  47. ERR_print_errors_fp(stderr);
  48. return 1;
  49. }
  50. if (PKCS12_newpass(p12, argv[2], argv[3]) == 0) {
  51. fprintf(stderr, "Error changing password\n");
  52. ERR_print_errors_fp(stderr);
  53. PKCS12_free(p12);
  54. return 1;
  55. }
  56. if ((fp = fopen(argv[4], "wb")) == NULL) {
  57. fprintf(stderr, "Error opening file %s\n", argv[4]);
  58. PKCS12_free(p12);
  59. return 1;
  60. }
  61. i2d_PKCS12_fp(fp, p12);
  62. PKCS12_free(p12);
  63. fclose(fp);
  64. return 0;
  65. }
  66. =head1 NOTES
  67. If the PKCS#12 structure does not have a password, then you must use the empty
  68. string "" for B<oldpass>. Using NULL for B<oldpass> will result in a
  69. PKCS12_newpass() failure.
  70. If the wrong password is used for B<oldpass> then the function will fail,
  71. with a MAC verification error. In rare cases the PKCS12 structure does not
  72. contain a MAC: in this case it will usually fail with a decryption padding
  73. error.
  74. =head1 BUGS
  75. The password format is a NULL terminated ASCII string which is converted to
  76. Unicode form internally. As a result some passwords cannot be supplied to
  77. this function.
  78. =head1 SEE ALSO
  79. L<PKCS12_create(3)>, L<ERR_get_error(3)>,
  80. L<passphrase-encoding(7)>
  81. =head1 COPYRIGHT
  82. Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  83. Licensed under the OpenSSL license (the "License"). You may not use
  84. this file except in compliance with the License. You can obtain a copy
  85. in the file LICENSE in the source distribution or at
  86. L<https://www.openssl.org/source/license.html>.
  87. =cut