]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/subversion/subversion/libsvn_subr/x509info.c
Update svn-1.9.7 to 1.10.0.
[FreeBSD/FreeBSD.git] / contrib / subversion / subversion / libsvn_subr / x509info.c
1 /*
2  * x509info.c:  Accessors for svn_x509_certinfo_t
3  *
4  * ====================================================================
5  *    Licensed to the Apache Software Foundation (ASF) under one
6  *    or more contributor license agreements.  See the NOTICE file
7  *    distributed with this work for additional information
8  *    regarding copyright ownership.  The ASF licenses this file
9  *    to you under the Apache License, Version 2.0 (the
10  *    "License"); you may not use this file except in compliance
11  *    with the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *    Unless required by applicable law or agreed to in writing,
16  *    software distributed under the License is distributed on an
17  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  *    KIND, either express or implied.  See the License for the
19  *    specific language governing permissions and limitations
20  *    under the License.
21  * ====================================================================
22  */
23
24
25 \f
26 #include <string.h>
27
28 #include <apr_pools.h>
29 #include <apr_tables.h>
30
31 #include "svn_string.h"
32 #include "svn_hash.h"
33 #include "x509.h"
34
35 \f
36
37 svn_x509_name_attr_t *
38 svn_x509_name_attr_dup(const svn_x509_name_attr_t *attr,
39                        apr_pool_t *result_pool,
40                        apr_pool_t *scratch_pool)
41 {
42   svn_x509_name_attr_t *result = apr_palloc(result_pool, sizeof(*result));
43   result->oid_len = attr->oid_len;
44   result->oid = apr_pmemdup(result_pool, attr->oid, attr->oid_len);
45   result->utf8_value = apr_pstrdup(result_pool, attr->utf8_value);
46
47   return result;
48 }
49
50 const unsigned char *
51 svn_x509_name_attr_get_oid(const svn_x509_name_attr_t *attr, apr_size_t *len)
52 {
53   *len = attr->oid_len;
54   return attr->oid;
55 }
56
57 const char *
58 svn_x509_name_attr_get_value(const svn_x509_name_attr_t *attr)
59 {
60   return attr->utf8_value;
61 }
62
63 /* Array elements are assumed to be nul-terminated C strings. */
64 static apr_array_header_t *
65 deep_copy_array(apr_array_header_t *s, apr_pool_t *result_pool)
66 {
67   int i;
68   apr_array_header_t *d;
69
70   if (!s)
71     return NULL;
72
73   d = apr_array_copy(result_pool, s);
74
75   /* Make a deep copy of the strings in the array. */
76   for (i = 0; i < s->nelts; ++i)
77     {
78       APR_ARRAY_IDX(d, i, const char *) =
79         apr_pstrdup(result_pool, APR_ARRAY_IDX(s, i, const char *));
80     }
81
82   return d;
83 }
84
85 /* Copy an array with elements that are svn_x509_name_attr_t's */
86 static apr_array_header_t *
87 deep_copy_name_attrs(apr_array_header_t *s, apr_pool_t *result_pool)
88 {
89   int i;
90   apr_array_header_t *d;
91
92   if (!s)
93     return NULL;
94
95   d = apr_array_copy(result_pool, s);
96
97   /* Make a deep copy of the svn_x509_name_attr_t's in the array. */
98   for (i = 0; i < s->nelts; ++i)
99     {
100       APR_ARRAY_IDX(d, i, const svn_x509_name_attr_t *) =
101         svn_x509_name_attr_dup(APR_ARRAY_IDX(s, i, svn_x509_name_attr_t *),
102                                result_pool, result_pool);
103     }
104
105   return d;
106 }
107
108 svn_x509_certinfo_t *
109 svn_x509_certinfo_dup(const svn_x509_certinfo_t *certinfo,
110                       apr_pool_t *result_pool,
111                       apr_pool_t *scratch_pool)
112 {
113   svn_x509_certinfo_t *result = apr_palloc(result_pool, sizeof(*result));
114   result->subject = deep_copy_name_attrs(certinfo->subject, result_pool);
115   result->issuer = deep_copy_name_attrs(certinfo->issuer, result_pool);
116   result->valid_from = certinfo->valid_from;
117   result->valid_to = certinfo->valid_to;
118   result->digest = svn_checksum_dup(certinfo->digest, result_pool);
119   result->hostnames = deep_copy_array(certinfo->hostnames, result_pool);
120
121   return result;
122 }
123
124 typedef struct asn1_oid {
125   const unsigned char *oid;
126   const apr_size_t oid_len;
127   const char *short_label;
128   const char *long_label;
129 } asn1_oid;
130
131 #define CONSTANT_PAIR(c) (unsigned char *)(c), sizeof((c)) - 1
132
133 static const asn1_oid asn1_oids[] = {
134   { CONSTANT_PAIR(SVN_X509_OID_COMMON_NAME),  "CN", "commonName" },
135   { CONSTANT_PAIR(SVN_X509_OID_COUNTRY),      "C",  "countryName" },
136   { CONSTANT_PAIR(SVN_X509_OID_LOCALITY),     "L",  "localityName" },
137   { CONSTANT_PAIR(SVN_X509_OID_STATE),        "ST", "stateOrProvinceName" },
138   { CONSTANT_PAIR(SVN_X509_OID_ORGANIZATION), "O",  "organizationName" },
139   { CONSTANT_PAIR(SVN_X509_OID_ORG_UNIT),     "OU", "organizationalUnitName"},
140   { CONSTANT_PAIR(SVN_X509_OID_EMAIL),        NULL, "emailAddress" },
141   { NULL },
142 };
143
144 /* Given an OID return a null-terminated C string representation.
145  * For example an OID with the bytes "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
146  * would be converted to the string "1.2.840.113549.1.9.1". */
147 const char *
148 svn_x509_oid_to_string(const unsigned char *oid, apr_size_t oid_len,
149                        apr_pool_t *scratch_pool, apr_pool_t *result_pool)
150 {
151   svn_stringbuf_t *out = svn_stringbuf_create_empty(result_pool);
152   const unsigned char *p = oid;
153   const unsigned char *end = p + oid_len;
154   const char *temp;
155
156   while (p != end) {
157     if (p == oid)
158       {
159         /* Handle decoding the first two values of the OID.  These values
160          * are encoded by taking the first value and adding 40 to it and
161          * adding the result to the second value, then placing this single
162          * value in the first byte of the output.  This is unambiguous since
163          * the first value is apparently limited to 0, 1 or 2 and the second
164          * is limited to 0 to 39. */
165         temp = apr_psprintf(scratch_pool, "%d.%d", *p / 40, *p % 40);
166         p++;
167       }
168     else if (*p < 128)
169       {
170         /* The remaining values if they're less than 128 are just
171          * the number one to one encoded */
172         temp = apr_psprintf(scratch_pool, ".%d", *p);
173         p++;
174       }
175     else
176       {
177         /* Values greater than 128 are encoded as a series of 7 bit values
178          * with the left most bit set to indicate this encoding with the
179          * last octet missing the left most bit to finish out the series.. */
180         unsigned int collector = 0;
181         svn_boolean_t dot = FALSE;
182
183         do {
184           if (collector == 0 && *p == 0x80)
185             {
186               /* include leading zeros in the string representation
187                  technically not legal, but this seems nicer than just
188                  returning NULL */
189               if (!dot)
190                 {
191                   svn_stringbuf_appendbyte(out, '.');
192                   dot = TRUE;
193                 }
194               svn_stringbuf_appendbyte(out, '0');
195             }
196           else if (collector > UINT_MAX >> 7)
197             {
198               /* overflow */
199               return NULL;
200             }
201           collector = collector << 7 | (*(p++) & 0x7f);
202         } while (p != end && *p > 127);
203         if (collector > UINT_MAX >> 7)
204           return NULL; /* overflow */
205         collector = collector << 7 | *(p++);
206         temp = apr_psprintf(scratch_pool, "%s%d", dot ? "" : ".", collector);
207       }
208     svn_stringbuf_appendcstr(out, temp);
209   }
210
211   if (svn_stringbuf_isempty(out))
212     return NULL;
213
214   return out->data;
215 }
216
217 static const asn1_oid *oid_to_asn1_oid(unsigned char *oid, apr_size_t oid_len)
218 {
219   const asn1_oid *entry;
220
221   for (entry = asn1_oids; entry->oid; entry++)
222     {
223       if (oid_len == entry->oid_len &&
224           memcmp(oid, entry->oid, oid_len) == 0)
225         return entry;
226     }
227
228   return NULL;
229 }
230
231 static const char *oid_to_best_label(unsigned char *oid, apr_size_t oid_len,
232                                      apr_pool_t *result_pool)
233 {
234   const asn1_oid *entry = oid_to_asn1_oid(oid, oid_len);
235
236   if (entry)
237     {
238       if (entry->short_label)
239         return entry->short_label;
240
241       if (entry->long_label)
242         return entry->long_label;
243     }
244   else
245     {
246       const char *oid_string = svn_x509_oid_to_string(oid, oid_len,
247                                                       result_pool, result_pool);
248       if (oid_string)
249         return oid_string;
250     }
251
252   return "??";
253 }
254
255 /*
256  * Store the name from dn in printable form into buf,
257  * using scratch_pool for any temporary allocations.
258  * If CN is not NULL, return any common name in CN
259  */
260 static const char *
261 get_dn(apr_array_header_t *name,
262        apr_pool_t *result_pool)
263 {
264   svn_stringbuf_t *buf = svn_stringbuf_create_empty(result_pool);
265   int n;
266
267   for (n = 0; n < name->nelts; n++)
268     {
269       const svn_x509_name_attr_t *attr = APR_ARRAY_IDX(name, n, svn_x509_name_attr_t *);
270
271       if (n > 0)
272         svn_stringbuf_appendcstr(buf, ", ");
273
274       svn_stringbuf_appendcstr(buf, oid_to_best_label(attr->oid, attr->oid_len, result_pool));
275       svn_stringbuf_appendbyte(buf, '=');
276       svn_stringbuf_appendcstr(buf, attr->utf8_value);
277     }
278
279   return buf->data;
280 }
281
282 const char *
283 svn_x509_certinfo_get_subject(const svn_x509_certinfo_t *certinfo,
284                               apr_pool_t *result_pool)
285 {
286   return get_dn(certinfo->subject, result_pool);
287 }
288
289 const apr_array_header_t *
290 svn_x509_certinfo_get_subject_attrs(const svn_x509_certinfo_t *certinfo)
291 {
292   return certinfo->subject;
293 }
294
295 const char *
296 svn_x509_certinfo_get_issuer(const svn_x509_certinfo_t *certinfo,
297                              apr_pool_t *result_pool)
298 {
299   return get_dn(certinfo->issuer, result_pool);
300 }
301
302 const apr_array_header_t *
303 svn_x509_certinfo_get_issuer_attrs(const svn_x509_certinfo_t *certinfo)
304 {
305   return certinfo->issuer;
306 }
307
308 apr_time_t
309 svn_x509_certinfo_get_valid_from(const svn_x509_certinfo_t *certinfo)
310 {
311   return certinfo->valid_from;
312 }
313
314 apr_time_t
315 svn_x509_certinfo_get_valid_to(const svn_x509_certinfo_t *certinfo)
316 {
317   return certinfo->valid_to;
318 }
319
320 const svn_checksum_t *
321 svn_x509_certinfo_get_digest(const svn_x509_certinfo_t *certinfo)
322 {
323   return certinfo->digest;
324 }
325
326 const apr_array_header_t *
327 svn_x509_certinfo_get_hostnames(const svn_x509_certinfo_t *certinfo)
328 {
329   return certinfo->hostnames;
330 }
331