155682Smarkm/* 2233294Sstas * Copyright (c) 1997 Kungliga Tekniska H��gskolan 3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4233294Sstas * All rights reserved. 555682Smarkm * 6233294Sstas * Redistribution and use in source and binary forms, with or without 7233294Sstas * modification, are permitted provided that the following conditions 8233294Sstas * are met: 955682Smarkm * 10233294Sstas * 1. Redistributions of source code must retain the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer. 1255682Smarkm * 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 1655682Smarkm * 17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors 18233294Sstas * may be used to endorse or promote products derived from this software 19233294Sstas * without specific prior written permission. 2055682Smarkm * 21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31233294Sstas * SUCH DAMAGE. 3255682Smarkm */ 3355682Smarkm 3455682Smarkm#include "der_locl.h" 3555682Smarkm 36233294Sstas#define ASN1_MAX_YEAR 2000 3755682Smarkm 3855682Smarkmstatic int 3955682Smarkmis_leap(unsigned y) 4055682Smarkm{ 4155682Smarkm y += 1900; 4255682Smarkm return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0); 4355682Smarkm} 4455682Smarkm 45233294Sstasstatic const unsigned ndays[2][12] ={ 46233294Sstas {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 47233294Sstas {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; 48233294Sstas 49233294Sstas/* 50178825Sdfr * This is a simplifed version of timegm(3) that doesn't accept out of 51178825Sdfr * bound values that timegm(3) normally accepts but those are not 52178825Sdfr * valid in asn1 encodings. 53178825Sdfr */ 54178825Sdfr 5555682Smarkmtime_t 56178825Sdfr_der_timegm (struct tm *tm) 5755682Smarkm{ 5855682Smarkm time_t res = 0; 59233294Sstas int i; 6055682Smarkm 61233294Sstas /* 62233294Sstas * See comment in _der_gmtime 63233294Sstas */ 64233294Sstas if (tm->tm_year > ASN1_MAX_YEAR) 65233294Sstas return 0; 66233294Sstas 67233294Sstas if (tm->tm_year < 0) 68178825Sdfr return -1; 69233294Sstas if (tm->tm_mon < 0 || tm->tm_mon > 11) 70178825Sdfr return -1; 71233294Sstas if (tm->tm_mday < 1 || tm->tm_mday > (int)ndays[is_leap(tm->tm_year)][tm->tm_mon]) 72178825Sdfr return -1; 73233294Sstas if (tm->tm_hour < 0 || tm->tm_hour > 23) 74178825Sdfr return -1; 75233294Sstas if (tm->tm_min < 0 || tm->tm_min > 59) 76178825Sdfr return -1; 77233294Sstas if (tm->tm_sec < 0 || tm->tm_sec > 59) 78178825Sdfr return -1; 79178825Sdfr 8055682Smarkm for (i = 70; i < tm->tm_year; ++i) 8155682Smarkm res += is_leap(i) ? 366 : 365; 8255682Smarkm 8355682Smarkm for (i = 0; i < tm->tm_mon; ++i) 8455682Smarkm res += ndays[is_leap(tm->tm_year)][i]; 8555682Smarkm res += tm->tm_mday - 1; 8655682Smarkm res *= 24; 8755682Smarkm res += tm->tm_hour; 8855682Smarkm res *= 60; 8955682Smarkm res += tm->tm_min; 9055682Smarkm res *= 60; 9155682Smarkm res += tm->tm_sec; 9255682Smarkm return res; 9355682Smarkm} 94233294Sstas 95233294Sstasstruct tm * 96233294Sstas_der_gmtime(time_t t, struct tm *tm) 97233294Sstas{ 98233294Sstas time_t secday = t % (3600 * 24); 99233294Sstas time_t days = t / (3600 * 24); 100233294Sstas 101233294Sstas memset(tm, 0, sizeof(*tm)); 102233294Sstas 103233294Sstas tm->tm_sec = secday % 60; 104233294Sstas tm->tm_min = (secday % 3600) / 60; 105233294Sstas tm->tm_hour = secday / 3600; 106233294Sstas 107233294Sstas /* 108233294Sstas * Refuse to calculate time ~ 2000 years into the future, this is 109233294Sstas * not possible for systems where time_t is a int32_t, however, 110233294Sstas * when time_t is a int64_t, that can happen, and this becomes a 111233294Sstas * denial of sevice. 112233294Sstas */ 113233294Sstas if (days > (ASN1_MAX_YEAR * 365)) 114233294Sstas return NULL; 115233294Sstas 116233294Sstas tm->tm_year = 70; 117233294Sstas while(1) { 118233294Sstas unsigned dayinyear = (is_leap(tm->tm_year) ? 366 : 365); 119233294Sstas if (days < dayinyear) 120233294Sstas break; 121233294Sstas tm->tm_year += 1; 122233294Sstas days -= dayinyear; 123233294Sstas } 124233294Sstas tm->tm_mon = 0; 125233294Sstas 126233294Sstas while (1) { 127233294Sstas unsigned daysinmonth = ndays[is_leap(tm->tm_year)][tm->tm_mon]; 128233294Sstas if (days < daysinmonth) 129233294Sstas break; 130233294Sstas days -= daysinmonth; 131233294Sstas tm->tm_mon++; 132233294Sstas } 133233294Sstas tm->tm_mday = days + 1; 134233294Sstas 135233294Sstas return tm; 136233294Sstas} 137