1PKCS12_NEWPASS(3) OpenSSL PKCS12_NEWPASS(3)
2
3
4
6 PKCS12_newpass - change the password of a PKCS12 structure
7
9 #include <openssl/pkcs12.h>
10
11 int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass);
12
14 PKCS12_newpass() changes the password of a PKCS12 structure.
15
16 p12 is a pointer to a PKCS12 structure. oldpass is the existing
17 password and newpass is the new password.
18
20 Each of oldpass and 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
22 ISO8859-1 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 UI_OpenSSL(3), for example.
29
31 PKCS12_newpass() returns 1 on success or 0 on failure. Applications can
32 retrieve the most recent error from PKCS12_newpass() with
33 ERR_get_error().
34
36 This example loads a PKCS#12 file, changes its password and writes out
37 the result to a new file.
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <openssl/pem.h>
42 #include <openssl/err.h>
43 #include <openssl/pkcs12.h>
44
45 int main(int argc, char **argv)
46 {
47 FILE *fp;
48 PKCS12 *p12;
49
50 if (argc != 5) {
51 fprintf(stderr, "Usage: pkread p12file password newpass opfile\n");
52 return 1;
53 }
54 if ((fp = fopen(argv[1], "rb")) == NULL) {
55 fprintf(stderr, "Error opening file %s\n", argv[1]);
56 return 1;
57 }
58 p12 = d2i_PKCS12_fp(fp, NULL);
59 fclose(fp);
60 if (p12 == NULL) {
61 fprintf(stderr, "Error reading PKCS#12 file\n");
62 ERR_print_errors_fp(stderr);
63 return 1;
64 }
65 if (PKCS12_newpass(p12, argv[2], argv[3]) == 0) {
66 fprintf(stderr, "Error changing password\n");
67 ERR_print_errors_fp(stderr);
68 PKCS12_free(p12);
69 return 1;
70 }
71 if ((fp = fopen(argv[4], "wb")) == NULL) {
72 fprintf(stderr, "Error opening file %s\n", argv[4]);
73 PKCS12_free(p12);
74 return 1;
75 }
76 i2d_PKCS12_fp(fp, p12);
77 PKCS12_free(p12);
78 fclose(fp);
79 return 0;
80 }
81
83 If the PKCS#12 structure does not have a password, then you must use
84 the empty string "" for oldpass. Using NULL for oldpass will result in
85 a PKCS12_newpass() failure.
86
87 If the wrong password is used for oldpass then the function will fail,
88 with a MAC verification error. In rare cases the PKCS12 structure does
89 not contain a MAC: in this case it will usually fail with a decryption
90 padding error.
91
93 The password format is a NULL terminated ASCII string which is
94 converted to Unicode form internally. As a result some passwords cannot
95 be supplied to this function.
96
98 PKCS12_create(3), ERR_get_error(3), passphrase-encoding(7)
99
101 Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
102
103 Licensed under the OpenSSL license (the "License"). You may not use
104 this file except in compliance with the License. You can obtain a copy
105 in the file LICENSE in the source distribution or at
106 <https://www.openssl.org/source/license.html>.
107
108
109
1101.1.1q 2022-07-21 PKCS12_NEWPASS(3)