기본 콘텐츠로 건너뛰기

ProxySQL 에서 Unknown system variable 'query_cache_size' 오류

ASN.1 시간을 time_t로 변환

OpenSSL에서 인증서 만료일을 ASN.1로 표현하므로 기존 time_t와 비교하기 영 껄끄럽다. 물론 단순히 X509_cmp_time이라는 함수를 제공하긴 하지만, 역시나 time_t를 알고 싶은 열망이 가득하여 뜯기 시작했다.

RFC-2459(http://tools.ietf.org/html/rfc2459)에 따르면 UTC Time과 Generalized Time으로 나뉘는데 크게 다를 바는 없다.

UTC는 YYMMDDHHMMSS{Z,{+,-}hhmm} 형태이고, G뭐시기 타임은 YYYYMMDDHHMMSS{Z,{+,-}hhmm}와 같다. UTC에서 YY는 연도인데 50이상이면 1900년대, 미만이면 2000년대이다.

전체적으로 해당 포맷이 고정적이라는 가정(!)하에 적당히 파싱해서, mktime으로 만드는데, mktime은 입력값이 지역시간 기반이라는 점을 유의해서 제거하면 된다. 적당히 offset 계산해서 최종 mktime 값에 더해주기만 하면 끝~*

static
inline
int
_b2i(const unsigned char* s, size_t len)
{
    int ret(0);
    int pow(1);

    for ( size_t i(0); i<len; i++ )
    {
        ret += ((s[len-i-1]-'0')*pow);
        pow *= 10;
    }

    return ret;
}

static
inline
int
_b2off(const unsigned char* s)
{
    return (_b2i(s, 2) * 3600 + _b2i(s+2, 2) * 60);
}

static
inline
time_t
_asn1_to_time_t(const char* str, size_t len, bool utc)
{
    if ( (size_t)-1 == len ) len = strlen(str);

    //UTC
    //YYMMDDHHMMSS{Z,{+,-}hhmm}

    //Generalize
    //YYYYMMDDHHMMSS{Z,{+,-}hhmm}
    struct tm tm;
    memset(&tm, 0x00, sizeof(tm));

    const unsigned char* p((unsigned char*)str);

    if ( utc )
    {
        if ( len < 13 ) return (time_t)-1;

        tm.tm_year = _b2i(p, 2); p += 2;
        if ( tm.tm_year < 50 ) tm.tm_year += 100;
        tm.tm_mon = _b2i(p, 2) - 1; p += 2;
        tm.tm_mday = _b2i(p, 2); p += 2;
        tm.tm_hour = _b2i(p, 2); p += 2;
        tm.tm_min = _b2i(p, 2); p += 2;
        tm.tm_sec = _b2i(p, 2); p += 2;
    }
    else
    {
        if ( len < 15 ) return (time_t)-1;

        tm.tm_year = _b2i(p, 4) - 1900; p += 4;
        tm.tm_mon = _b2i(p, 2) - 1; p += 2;
        tm.tm_mday = _b2i(p, 2); p += 2;
        tm.tm_hour = _b2i(p, 2); p += 2;
        tm.tm_min = _b2i(p, 2); p += 2;
        tm.tm_sec = _b2i(p, 2); p += 2;
    }

    const unsigned char e(*p); ++p;
    switch(e)
    {
    case 'Z': break;
    case '+': tm.tm_gmtoff = _b2off(p); break;
    case '-': tm.tm_gmtoff = 0 - _b2off(p); break;
    default: return (time_t)-1;
    }

    time_t t(time(NULL));
    time_t off(tm.tm_gmtoff + localtime(&t)->tm_gmtoff);
    tm.tm_gmtoff = 0;

    return mktime(&tm) + off;
}

int
main(int argc, char* argv[])
{
    // load from certificate...
    time_t t = _asn1_to_time_t((char*)atm->data, atm->length, atm->type == V_ASN1_UTCTIME);
    printf("%s", asctime(&t));
}

댓글

이 블로그의 인기 게시물

Bash Array, Map 정리

Bash에서 Array, Map에 대한 정리. (매번 찾기 귀찮) 찾아보진 않았지만, Bash에서 Array든 Map이든 동일하게 Map(C++에서 Unordered Map)으로 동작하는 것 같다. 왜냐하면, Array의 Index가 연속하지 않아도 동작한다. 그저 Key가 0 이상의 정수인 Map이랑 비슷하게 동작한다. 예) 1, 2, 3, 9, 10 Array # 생성 declare -a empty_array declare -a ar=(haha hoho baba "long string haha hoho") # 접근 echo "ar[0]=${ar[0]}" echo "all as array=${ar[@]}" # 큰따옴표 안에서 각 원소를 따로따로 전달한다. echo "all as one=${ar[*]}" # 큰따옴표 안에서 각 원소를 문자열 하나로 합쳐 전달한다. echo "indexes=${!ar[@]}" echo "indexes=${!ar[*]}" echo "length=${#ar[@]}" echo "length=${#ar[*]}" echo "last=${ar[-1]}" echo "last=${ar[@]: -1}" # 콜론 뒤에 빈 칸이 꼭 필요하다. 옛 방식 # 현재 상황 declare -p ar #(출력) declare -a ar=([0]="haha" [1]="hoho" [2]="baba" [3]="long string haha hoho") ar[100]=hello # 인덱스를 건너 뛰어도 동작한다. declare -p ar #(출력) declare -a ar=([0]="haha" [1]="hoho" [2]="baba" [3]=&

설치한 패키지에서 RPM 추출하기

오래된 패키지를 관리할 저장소가 없어졌고, 기존 패키지로 다른 서버를 세팅해야할 일이 생겼다면 RPM의 리패키지 기능을 이용해보자. $ rpm -e --repackage [PACKAGE_NAME] 위와 같이 리패키지하면, /var/spool/repackage/ 에 생성한 RPM파일이 있다. :-)