source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/asn1/x_long.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: 3.9 KB
Line 
1/*
2 * Copyright 2000-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#include <stdio.h>
11#include "internal/cryptlib.h"
12#include <openssl/asn1t.h>
13#include <openssl/bn.h>
14
15/*
16 * Custom primitive type for long handling. This converts between an
17 * ASN1_INTEGER and a long directly.
18 */
19
20static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
21static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
22
23static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
24 const ASN1_ITEM *it);
25static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
26 int utype, char *free_cont, const ASN1_ITEM *it);
27static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
28 int indent, const ASN1_PCTX *pctx);
29
30static ASN1_PRIMITIVE_FUNCS long_pf = {
31 NULL, 0,
32 long_new,
33 long_free,
34 long_free, /* Clear should set to initial value */
35 long_c2i,
36 long_i2c,
37 long_print
38};
39
40ASN1_ITEM_start(LONG)
41 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG"
42ASN1_ITEM_end(LONG)
43
44ASN1_ITEM_start(ZLONG)
45 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG"
46ASN1_ITEM_end(ZLONG)
47
48static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
49{
50 *(long *)pval = it->size;
51 return 1;
52}
53
54static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
55{
56 *(long *)pval = it->size;
57}
58
59static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
60 const ASN1_ITEM *it)
61{
62 long ltmp;
63 unsigned long utmp;
64 int clen, pad, i;
65 /* this exists to bypass broken gcc optimization */
66 char *cp = (char *)pval;
67
68 /* use memcpy, because we may not be long aligned */
69 memcpy(&ltmp, cp, sizeof(long));
70
71 if (ltmp == it->size)
72 return -1;
73 /*
74 * Convert the long to positive: we subtract one if negative so we can
75 * cleanly handle the padding if only the MSB of the leading octet is
76 * set.
77 */
78 if (ltmp < 0)
79 utmp = 0 - (unsigned long)ltmp - 1;
80 else
81 utmp = ltmp;
82 clen = BN_num_bits_word(utmp);
83 /* If MSB of leading octet set we need to pad */
84 if (!(clen & 0x7))
85 pad = 1;
86 else
87 pad = 0;
88
89 /* Convert number of bits to number of octets */
90 clen = (clen + 7) >> 3;
91
92 if (cont) {
93 if (pad)
94 *cont++ = (ltmp < 0) ? 0xff : 0;
95 for (i = clen - 1; i >= 0; i--) {
96 cont[i] = (unsigned char)(utmp & 0xff);
97 if (ltmp < 0)
98 cont[i] ^= 0xff;
99 utmp >>= 8;
100 }
101 }
102 return clen + pad;
103}
104
105static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
106 int utype, char *free_cont, const ASN1_ITEM *it)
107{
108 int neg, i;
109 long ltmp;
110 unsigned long utmp = 0;
111 char *cp = (char *)pval;
112 if (len > (int)sizeof(long)) {
113 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
114 return 0;
115 }
116 /* Is it negative? */
117 if (len && (cont[0] & 0x80))
118 neg = 1;
119 else
120 neg = 0;
121 utmp = 0;
122 for (i = 0; i < len; i++) {
123 utmp <<= 8;
124 if (neg)
125 utmp |= cont[i] ^ 0xff;
126 else
127 utmp |= cont[i];
128 }
129 ltmp = (long)utmp;
130 if (neg) {
131 ltmp = -ltmp;
132 ltmp--;
133 }
134 if (ltmp == it->size) {
135 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
136 return 0;
137 }
138 memcpy(cp, &ltmp, sizeof(long));
139 return 1;
140}
141
142static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
143 int indent, const ASN1_PCTX *pctx)
144{
145 return BIO_printf(out, "%ld\n", *(long *)pval);
146}
Note: See TracBrowser for help on using the repository browser.