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