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