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