4f44c34eded0a6f247aed22ca3a2d2c7983aeb51
[openssl.git] / doc / crypto / PKCS12_newpass.pod
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 =head1 RETURN VALUES
21
22 PKCS12_newpass() returns 1 on success or 0 on failure. Applications can
23 retrieve the most recent error from PKCS12_newpass() with ERR_get_error().
24
25 =head1 EXAMPLE
26
27 This example loads a PKCS#12 file, changes its password and writes out
28 the result to a new file.
29
30  #include <stdio.h>
31  #include <stdlib.h>
32  #include <openssl/pem.h>
33  #include <openssl/err.h>
34  #include <openssl/pkcs12.h>
35
36  int main(int argc, char **argv)
37  {
38     FILE *fp;
39     PKCS12 *p12;
40     if (argc != 5) {
41         fprintf(stderr, "Usage: pkread p12file password newpass opfile\n");
42         return 1;
43     }
44     if ((fp = fopen(argv[1], "rb")) == NULL) {
45         fprintf(stderr, "Error opening file %s\n", argv[1]);
46         return 1;
47     }
48     p12 = d2i_PKCS12_fp(fp, NULL);
49     fclose(fp);
50     if (p12 == NULL) {
51         fprintf(stderr, "Error reading PKCS#12 file\n");
52         ERR_print_errors_fp(stderr);
53         return 1;
54     }
55     if (PKCS12_newpass(p12, argv[2], argv[3]) == 0) {
56         fprintf(stderr, "Error changing password\n");
57         ERR_print_errors_fp(stderr);
58         PKCS12_free(p12);
59         return 1;
60     }
61     if ((fp = fopen(argv[4], "wb")) == NULL) {
62         fprintf(stderr, "Error opening file %s\n", argv[4]);
63         PKCS12_free(p12);
64         return 1;
65     }
66     i2d_PKCS12_fp(fp, p12);
67     PKCS12_free(p12);
68     fclose(fp);
69     return 0;
70  }
71
72
73 =head1 NOTES
74
75 If the PKCS#12 structure does not have a password, then you must use the empty
76 string "" for B<oldpass>. Using NULL for B<oldpass> will result in a
77 PKCS12_newpass() failure.
78
79 If the wrong password is used for B<oldpass> then the function will fail,
80 with a MAC verification error. In rare cases the PKCS12 structure does not
81 contain a MAC: in this case it will usually fail with a decryption padding
82 error.
83
84 =head1 BUGS
85
86 The password format is a NULL terminated ASCII string which is converted to
87 Unicode form internally. As a result some passwords cannot be supplied to
88 this function.
89
90 =head1 SEE ALSO
91
92 L<PKCS12_create(3)>, L<ERR_get_error(3)>
93
94 =cut