xref: /openbsd-src/lib/libcrypto/man/PKCS12_newpass.3 (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1.\"	$OpenBSD: PKCS12_newpass.3,v 1.2 2018/03/22 16:06:33 schwarze Exp $
2.\"	OpenSSL c95a8b4e May 5 14:26:26 2016 +0100
3.\"
4.\" This file was written by Jeffrey Walton <noloader@gmail.com>.
5.\" Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\"
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\"
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in
16.\"    the documentation and/or other materials provided with the
17.\"    distribution.
18.\"
19.\" 3. All advertising materials mentioning features or use of this
20.\"    software must display the following acknowledgment:
21.\"    "This product includes software developed by the OpenSSL Project
22.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23.\"
24.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25.\"    endorse or promote products derived from this software without
26.\"    prior written permission. For written permission, please contact
27.\"    openssl-core@openssl.org.
28.\"
29.\" 5. Products derived from this software may not be called "OpenSSL"
30.\"    nor may "OpenSSL" appear in their names without prior written
31.\"    permission of the OpenSSL Project.
32.\"
33.\" 6. Redistributions of any form whatsoever must retain the following
34.\"    acknowledgment:
35.\"    "This product includes software developed by the OpenSSL Project
36.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37.\"
38.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49.\" OF THE POSSIBILITY OF SUCH DAMAGE.
50.\"
51.Dd $Mdocdate: March 22 2018 $
52.Dt PKCS12_NEWPASS 3
53.Os
54.Sh NAME
55.Nm PKCS12_newpass
56.Nd change the password of a PKCS#12 structure
57.Sh SYNOPSIS
58.In openssl/pkcs12.h
59.Ft int
60.Fo PKCS12_newpass
61.Fa "PKCS12 *p12"
62.Fa "const char *oldpass"
63.Fa "const char *newpass"
64.Fc
65.Sh DESCRIPTION
66.Fn PKCS12_newpass
67changes the password of a PKCS#12 structure.
68.Pp
69.Fa p12
70is a pointer to a PKCS#12 structure.
71.Fa oldpass
72is the existing password and
73.Fa newpass
74is the new password.
75.Pp
76If the PKCS#12 structure does not have a password, use the empty
77string
78.Qq \&
79for
80.Fa oldpass .
81Passing
82.Dv NULL
83for
84.Fa oldpass
85results in a
86.Fn PKCS12_newpass
87failure.
88.Pp
89If the wrong password is used for
90.Fa oldpass ,
91the function will fail with a MAC verification error.
92In rare cases, the PKCS#12 structure does not contain a MAC:
93in this case it will usually fail with a decryption padding error.
94.Sh RETURN VALUES
95.Fn PKCS12_newpass
96returns 1 on success or 0 on failure.
97.Pp
98Applications can retrieve the most recent error from
99.Fn PKCS12_newpass
100with
101.Xr ERR_get_error 3 .
102.Sh EXAMPLES
103This example loads a PKCS#12 file, changes its password,
104and writes out the result to a new file.
105.Bd -literal
106#include <stdio.h>
107#include <stdlib.h>
108#include <openssl/pem.h>
109#include <openssl/err.h>
110#include <openssl/pkcs12.h>
111
112int main(int argc, char **argv)
113{
114	FILE *fp;
115	PKCS12 *p12;
116	if (argc != 5) {
117		fprintf(stderr,
118		    "Usage: pkread p12file password newpass opfile\en");
119		return 1;
120	}
121	if ((fp = fopen(argv[1], "rb")) == NULL) {
122		fprintf(stderr, "Error opening file %s\en", argv[1]);
123		return 1;
124	}
125	p12 = d2i_PKCS12_fp(fp, NULL);
126	fclose(fp);
127	if (p12 == NULL) {
128		fprintf(stderr, "Error reading PKCS#12 file\en");
129		ERR_print_errors_fp(stderr);
130		return 1;
131	}
132	if (PKCS12_newpass(p12, argv[2], argv[3]) == 0) {
133		fprintf(stderr, "Error changing password\en");
134		ERR_print_errors_fp(stderr);
135		PKCS12_free(p12);
136		return 1;
137	}
138	if ((fp = fopen(argv[4], "wb")) == NULL) {
139		fprintf(stderr, "Error opening file %s\en", argv[4]);
140		PKCS12_free(p12);
141		return 1;
142	}
143	i2d_PKCS12_fp(fp, p12);
144	PKCS12_free(p12);
145	fclose(fp);
146	return 0;
147}
148.Ed
149.Sh SEE ALSO
150.Xr ERR_get_error 3 ,
151.Xr PKCS12_create 3
152.Sh HISTORY
153.Fn PKCS12_newpass
154first appeared in OpenSSL 0.9.5 and has been available since
155.Ox 2.7 .
156.Sh BUGS
157The password format is a NUL terminated ASCII string which is
158converted to Unicode form internally.
159As a result, some passwords cannot be supplied to this function.
160