PKCS12_newpass.pod 3.2 KB

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