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