source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/asn1/a_time.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 4.1 KB
Line 
1/*
2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/*-
11 * This is an implementation of the ASN1 Time structure which is:
12 * Time ::= CHOICE {
13 * utcTime UTCTime,
14 * generalTime GeneralizedTime }
15 */
16
17#include <stdio.h>
18#include <time.h>
19#include "internal/cryptlib.h"
20#include <openssl/asn1t.h>
21#include "asn1_locl.h"
22
23IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
24
25IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
26
27ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
28{
29 return ASN1_TIME_adj(s, t, 0, 0);
30}
31
32ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
33 int offset_day, long offset_sec)
34{
35 struct tm *ts;
36 struct tm data;
37
38 ts = OPENSSL_gmtime(&t, &data);
39 if (ts == NULL) {
40 ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
41 return NULL;
42 }
43 if (offset_day || offset_sec) {
44 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
45 return NULL;
46 }
47 if ((ts->tm_year >= 50) && (ts->tm_year < 150))
48 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
49 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
50}
51
52int ASN1_TIME_check(const ASN1_TIME *t)
53{
54 if (t->type == V_ASN1_GENERALIZEDTIME)
55 return ASN1_GENERALIZEDTIME_check(t);
56 else if (t->type == V_ASN1_UTCTIME)
57 return ASN1_UTCTIME_check(t);
58 return 0;
59}
60
61/* Convert an ASN1_TIME structure to GeneralizedTime */
62ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
63 ASN1_GENERALIZEDTIME **out)
64{
65 ASN1_GENERALIZEDTIME *ret;
66 char *str;
67 int newlen;
68
69 if (!ASN1_TIME_check(t))
70 return NULL;
71
72 if (out == NULL || *out == NULL) {
73 if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL)
74 return NULL;
75 if (out)
76 *out = ret;
77 } else
78 ret = *out;
79
80 /* If already GeneralizedTime just copy across */
81 if (t->type == V_ASN1_GENERALIZEDTIME) {
82 if (!ASN1_STRING_set(ret, t->data, t->length))
83 return NULL;
84 return ret;
85 }
86
87 /* grow the string */
88 if (!ASN1_STRING_set(ret, NULL, t->length + 2))
89 return NULL;
90 /* ASN1_STRING_set() allocated 'len + 1' bytes. */
91 newlen = t->length + 2 + 1;
92 str = (char *)ret->data;
93 /* Work out the century and prepend */
94 if (t->data[0] >= '5')
95 OPENSSL_strlcpy(str, "19", newlen);
96 else
97 OPENSSL_strlcpy(str, "20", newlen);
98
99 OPENSSL_strlcat(str, (char *)t->data, newlen);
100
101 return ret;
102}
103
104int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
105{
106 ASN1_TIME t;
107
108 t.length = strlen(str);
109 t.data = (unsigned char *)str;
110 t.flags = 0;
111
112 t.type = V_ASN1_UTCTIME;
113
114 if (!ASN1_TIME_check(&t)) {
115 t.type = V_ASN1_GENERALIZEDTIME;
116 if (!ASN1_TIME_check(&t))
117 return 0;
118 }
119
120 if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
121 return 0;
122
123 return 1;
124}
125
126static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t)
127{
128 if (t == NULL) {
129 time_t now_t;
130 time(&now_t);
131 if (OPENSSL_gmtime(&now_t, tm))
132 return 1;
133 return 0;
134 }
135
136 if (t->type == V_ASN1_UTCTIME)
137 return asn1_utctime_to_tm(tm, t);
138 else if (t->type == V_ASN1_GENERALIZEDTIME)
139 return asn1_generalizedtime_to_tm(tm, t);
140
141 return 0;
142}
143
144int ASN1_TIME_diff(int *pday, int *psec,
145 const ASN1_TIME *from, const ASN1_TIME *to)
146{
147 struct tm tm_from, tm_to;
148 if (!asn1_time_to_tm(&tm_from, from))
149 return 0;
150 if (!asn1_time_to_tm(&tm_to, to))
151 return 0;
152 return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
153}
154
155int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
156{
157 if (tm->type == V_ASN1_UTCTIME)
158 return ASN1_UTCTIME_print(bp, tm);
159 if (tm->type == V_ASN1_GENERALIZEDTIME)
160 return ASN1_GENERALIZEDTIME_print(bp, tm);
161 BIO_write(bp, "Bad time value", 14);
162 return (0);
163}
Note: See TracBrowser for help on using the repository browser.