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