1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Module: security.c
* Description:
* Module for handling certificates and various
* utilities to access their data.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <pkgstrct.h>
#include <pkginfo.h>
#include <locale.h>
#include <libintl.h>
#include <unistd.h>
#include <stdlib.h>
#include <openssl/bio.h>
#include <openssl/pkcs12.h>
#include <openssl/pkcs7.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include "pkgerr.h"
#include "pkglib.h"
#include "pkglibmsgs.h"
#include "pkglocale.h"
#include "p12lib.h"
/* length of allowable passwords */
#define MAX_PASSLEN 128
/*
* Name: init_security
* Description: Initializes structures, libraries, for security operations
* Arguments: none
* Returns: 0 if we couldn't initialize, non-zero otherwise
*/
void
sec_init(void)
{
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_SUNW_strings();
(void) SSL_library_init();
}
/*
* get_cert_chain - Builds a chain of certificates, from a given
* user certificate to a trusted certificate.
*
* Arguments:
* err - Error object to add errors to
* cert - User cert to start with
* cas - Trusted certs to use as trust anchors
* chain - The resulting chain of certs (in the form of an
* ordered set) is placed here.
*
* Returns:
* 0 - Success - chain is stored in 'chain'.
* non-zero - Failure, errors recorded in err
*/
int
get_cert_chain(PKG_ERR *err, X509 *cert, STACK_OF(X509) *clcerts,
STACK_OF(X509) *cas, STACK_OF(X509) **chain)
{
X509_STORE_CTX *store_ctx = NULL;
X509_STORE *ca_store = NULL;
X509 *ca_cert = NULL;
int i;
int ret = 0;
if ((ca_store = X509_STORE_new()) == NULL) {
pkgerr_add(err, PKGERR_NOMEM,
gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
/* add all ca certs into the store */
for (i = 0; i < sk_X509_num(cas); i++) {
ca_cert = sk_X509_value(cas, i);
if (X509_STORE_add_cert(ca_store, ca_cert) == 0) {
pkgerr_add(err, PKGERR_NOMEM, gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
}
/* initialize context object used during the chain resolution */
if ((store_ctx = X509_STORE_CTX_new()) == NULL) {
pkgerr_add(err, PKGERR_NOMEM, gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
(void) X509_STORE_CTX_init(store_ctx, ca_store, cert, clcerts);
/* attempt to verify the cert, which builds the cert chain */
if (X509_verify_cert(store_ctx) <= 0) {
pkgerr_add(err, PKGERR_CHAIN,
gettext(ERR_CERTCHAIN),
get_subject_display_name(cert),
X509_verify_cert_error_string(store_ctx->error));
ret = 1;
goto cleanup;
}
*chain = X509_STORE_CTX_get1_chain(store_ctx);
cleanup:
if (ca_store != NULL)
(void) X509_STORE_free(ca_store);
if (store_ctx != NULL) {
(void) X509_STORE_CTX_cleanup(store_ctx);
(void) X509_STORE_CTX_free(store_ctx);
}
return (ret);
}
/*
* Name: get_subject_name
* Description: Retrieves a name used for identifying a certificate's subject.
*
* Arguments: cert - The certificate to get the name from
*
* Returns : A static buffer containing the common name (CN) of the
* subject of the cert.
*
* if the CN is not available, returns a string with the entire
* X509 distinguished name.
*/
char
*get_subject_display_name(X509 *cert)
{
X509_NAME *xname;
static char sname[ATTR_MAX];
xname = X509_get_subject_name(cert);
if (X509_NAME_get_text_by_NID(xname,
NID_commonName, sname,
ATTR_MAX) <= 0) {
(void) strncpy(sname,
X509_NAME_oneline(xname, NULL, 0), ATTR_MAX);
sname[ATTR_MAX - 1] = '\0';
}
return (sname);
}
/*
* Name: get_display_name
* Description: Retrieves a name used for identifying a certificate's issuer.
*
* Arguments: cert - The certificate to get the name from
*
* Returns : A static buffer containing the common name (CN)
* of the issuer of the cert.
*
* if the CN is not available, returns a string with the entire
* X509 distinguished name.
*/
char
*get_issuer_display_name(X509 *cert)
{
X509_NAME *xname;
static char sname[ATTR_MAX];
xname = X509_get_issuer_name(cert);
if (X509_NAME_get_text_by_NID(xname,
NID_commonName, sname,
ATTR_MAX) <= 0) {
(void) strncpy(sname,
X509_NAME_oneline(xname, NULL, 0), ATTR_MAX);
sname[ATTR_MAX - 1] = '\0';
}
return (sname);
}
/*
* Name: get_serial_num
* Description: Retrieves the serial number of an X509 cert
*
* Arguments: cert - The certificate to get the data from
*
* Returns : A static buffer containing the serial number
* of the cert
*
* if the SN is not available, returns NULL
*/
char
*get_serial_num(X509 *cert)
{
static char sn_str[ATTR_MAX];
ASN1_INTEGER *sn;
if ((sn = X509_get_serialNumber(cert)) != 0) {
return (NULL);
} else {
(void) snprintf(sn_str, ATTR_MAX, "%ld",
ASN1_INTEGER_get(sn));
}
return (sn_str);
}
/*
* Name: get_fingerprint
* Description: Generates a fingerprint string given
* a digest algorithm with which to calculate
* the fingerprint
*
* Arguments: cert - The certificate to get the data from
* Arguments: alg - The algorithm to use to calculate the fingerprint
*
* Returns : A static buffer containing the digest
* NULL if cert is NULL, or digest cannot be calculated
*/
char
*get_fingerprint(X509 *cert, const EVP_MD *alg)
{
static char fp_str[ATTR_MAX];
char tmp[ATTR_MAX] = "";
unsigned int n;
unsigned char md[EVP_MAX_MD_SIZE];
int i;
if (!X509_digest(cert, alg, md, &n)) {
return (NULL);
}
/* start with empty string */
fp_str[0] = '\0';
for (i = 0; i < (int)n; i++) {
/* form a byte of the fingerprint */
(void) snprintf(tmp, ATTR_MAX, "%02X:", md[i]);
/* cat it onto the end of the result */
(void) strlcat(fp_str, tmp, ATTR_MAX);
}
/* nuke trailing ':' */
fp_str[strlen(fp_str) - 1] = '\0';
return (fp_str);
}
|