Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
| 57 | #include <openssl/asn1.h> |
| 58 | |
| 59 | #include <string.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 60 | #include <time.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 61 | |
| 62 | #include <openssl/err.h> |
| 63 | #include <openssl/mem.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 64 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 65 | #include "asn1_locl.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 66 | |
| 67 | int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 68 | { |
| 69 | static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 }; |
| 70 | static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 }; |
| 71 | char *a; |
| 72 | int n, i, l, o; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 73 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 74 | if (d->type != V_ASN1_GENERALIZEDTIME) |
| 75 | return (0); |
| 76 | l = d->length; |
| 77 | a = (char *)d->data; |
| 78 | o = 0; |
| 79 | /* |
| 80 | * GENERALIZEDTIME is similar to UTCTIME except the year is represented |
| 81 | * as YYYY. This stuff treats everything as a two digit field so make |
| 82 | * first two fields 00 to 99 |
| 83 | */ |
| 84 | if (l < 13) |
| 85 | goto err; |
| 86 | for (i = 0; i < 7; i++) { |
| 87 | if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) { |
| 88 | i++; |
| 89 | if (tm) |
| 90 | tm->tm_sec = 0; |
| 91 | break; |
| 92 | } |
| 93 | if ((a[o] < '0') || (a[o] > '9')) |
| 94 | goto err; |
| 95 | n = a[o] - '0'; |
| 96 | if (++o > l) |
| 97 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 98 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 99 | if ((a[o] < '0') || (a[o] > '9')) |
| 100 | goto err; |
| 101 | n = (n * 10) + a[o] - '0'; |
| 102 | if (++o > l) |
| 103 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 104 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 105 | if ((n < min[i]) || (n > max[i])) |
| 106 | goto err; |
| 107 | if (tm) { |
| 108 | switch (i) { |
| 109 | case 0: |
| 110 | tm->tm_year = n * 100 - 1900; |
| 111 | break; |
| 112 | case 1: |
| 113 | tm->tm_year += n; |
| 114 | break; |
| 115 | case 2: |
| 116 | tm->tm_mon = n - 1; |
| 117 | break; |
| 118 | case 3: |
| 119 | tm->tm_mday = n; |
| 120 | break; |
| 121 | case 4: |
| 122 | tm->tm_hour = n; |
| 123 | break; |
| 124 | case 5: |
| 125 | tm->tm_min = n; |
| 126 | break; |
| 127 | case 6: |
| 128 | tm->tm_sec = n; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | /* |
| 134 | * Optional fractional seconds: decimal point followed by one or more |
| 135 | * digits. |
| 136 | */ |
| 137 | if (a[o] == '.') { |
| 138 | if (++o > l) |
| 139 | goto err; |
| 140 | i = o; |
| 141 | while ((a[o] >= '0') && (a[o] <= '9') && (o <= l)) |
| 142 | o++; |
| 143 | /* Must have at least one digit after decimal point */ |
| 144 | if (i == o) |
| 145 | goto err; |
| 146 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 147 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 148 | if (a[o] == 'Z') |
| 149 | o++; |
| 150 | else if ((a[o] == '+') || (a[o] == '-')) { |
| 151 | int offsign = a[o] == '-' ? -1 : 1, offset = 0; |
| 152 | o++; |
| 153 | if (o + 4 > l) |
| 154 | goto err; |
| 155 | for (i = 7; i < 9; i++) { |
| 156 | if ((a[o] < '0') || (a[o] > '9')) |
| 157 | goto err; |
| 158 | n = a[o] - '0'; |
| 159 | o++; |
| 160 | if ((a[o] < '0') || (a[o] > '9')) |
| 161 | goto err; |
| 162 | n = (n * 10) + a[o] - '0'; |
| 163 | if ((n < min[i]) || (n > max[i])) |
| 164 | goto err; |
| 165 | if (tm) { |
| 166 | if (i == 7) |
| 167 | offset = n * 3600; |
| 168 | else if (i == 8) |
| 169 | offset += n * 60; |
| 170 | } |
| 171 | o++; |
| 172 | } |
| 173 | if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign)) |
| 174 | return 0; |
| 175 | } else if (a[o]) { |
| 176 | /* Missing time zone information. */ |
| 177 | goto err; |
| 178 | } |
| 179 | return (o == l); |
| 180 | err: |
| 181 | return (0); |
| 182 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 183 | |
| 184 | int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 185 | { |
| 186 | return asn1_generalizedtime_to_tm(NULL, d); |
| 187 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 188 | |
| 189 | int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 190 | { |
| 191 | ASN1_GENERALIZEDTIME t; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 192 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 193 | t.type = V_ASN1_GENERALIZEDTIME; |
| 194 | t.length = strlen(str); |
| 195 | t.data = (unsigned char *)str; |
| 196 | if (ASN1_GENERALIZEDTIME_check(&t)) { |
| 197 | if (s != NULL) { |
| 198 | if (!ASN1_STRING_set((ASN1_STRING *)s, |
| 199 | (unsigned char *)str, t.length)) |
| 200 | return 0; |
| 201 | s->type = V_ASN1_GENERALIZEDTIME; |
| 202 | } |
| 203 | return (1); |
| 204 | } else |
| 205 | return (0); |
| 206 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 207 | |
| 208 | ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 209 | time_t t) |
| 210 | { |
| 211 | return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0); |
| 212 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 213 | |
| 214 | ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 215 | time_t t, int offset_day, |
| 216 | long offset_sec) |
| 217 | { |
| 218 | char *p; |
| 219 | struct tm *ts; |
| 220 | struct tm data; |
| 221 | size_t len = 20; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 222 | ASN1_GENERALIZEDTIME *tmps = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 223 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 224 | if (s == NULL) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 225 | tmps = ASN1_GENERALIZEDTIME_new(); |
| 226 | else |
| 227 | tmps = s; |
| 228 | if (tmps == NULL) |
| 229 | return NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 230 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 231 | ts = OPENSSL_gmtime(&t, &data); |
| 232 | if (ts == NULL) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 233 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 234 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 235 | if (offset_day || offset_sec) { |
| 236 | if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 237 | goto err; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 238 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 239 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 240 | p = (char *)tmps->data; |
| 241 | if ((p == NULL) || ((size_t)tmps->length < len)) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 242 | p = OPENSSL_malloc(len); |
| 243 | if (p == NULL) { |
| 244 | OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 245 | goto err; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 246 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 247 | OPENSSL_free(tmps->data); |
| 248 | tmps->data = (unsigned char *)p; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 249 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 250 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 251 | BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900, |
| 252 | ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, |
| 253 | ts->tm_sec); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 254 | tmps->length = strlen(p); |
| 255 | tmps->type = V_ASN1_GENERALIZEDTIME; |
| 256 | return tmps; |
| 257 | err: |
| 258 | if (s == NULL) |
| 259 | ASN1_GENERALIZEDTIME_free(tmps); |
| 260 | return NULL; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 261 | } |