Add nameConstraints commonName checking.
[openssl.git] / crypto / asn1 / x_bignum.c
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 BIGNUM handling. This reads in an ASN1_INTEGER
17  * as a BIGNUM directly. Currently it ignores the sign which isn't a problem
18  * since all BIGNUMs used are non negative and anything that looks negative
19  * is normally due to an encoding error.
20  */
21
22 #define BN_SENSITIVE    1
23
24 static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
25 static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
26 static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
27
28 static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
29                   const ASN1_ITEM *it);
30 static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
31                   int utype, char *free_cont, const ASN1_ITEM *it);
32 static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
33                          int utype, char *free_cont, const ASN1_ITEM *it);
34
35 static ASN1_PRIMITIVE_FUNCS bignum_pf = {
36     NULL, 0,
37     bn_new,
38     bn_free,
39     0,
40     bn_c2i,
41     bn_i2c
42 };
43
44 static ASN1_PRIMITIVE_FUNCS cbignum_pf = {
45     NULL, 0,
46     bn_secure_new,
47     bn_free,
48     0,
49     bn_secure_c2i,
50     bn_i2c
51 };
52
53 ASN1_ITEM_start(BIGNUM)
54         ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM"
55 ASN1_ITEM_end(BIGNUM)
56
57 ASN1_ITEM_start(CBIGNUM)
58         ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &cbignum_pf, BN_SENSITIVE, "CBIGNUM"
59 ASN1_ITEM_end(CBIGNUM)
60
61 static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
62 {
63     *pval = (ASN1_VALUE *)BN_new();
64     if (*pval != NULL)
65         return 1;
66     else
67         return 0;
68 }
69
70 static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
71 {
72     *pval = (ASN1_VALUE *)BN_secure_new();
73     if (*pval != NULL)
74         return 1;
75     else
76         return 0;
77 }
78
79 static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
80 {
81     if (!*pval)
82         return;
83     if (it->size & BN_SENSITIVE)
84         BN_clear_free((BIGNUM *)*pval);
85     else
86         BN_free((BIGNUM *)*pval);
87     *pval = NULL;
88 }
89
90 static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
91                   const ASN1_ITEM *it)
92 {
93     BIGNUM *bn;
94     int pad;
95     if (!*pval)
96         return -1;
97     bn = (BIGNUM *)*pval;
98     /* If MSB set in an octet we need a padding byte */
99     if (BN_num_bits(bn) & 0x7)
100         pad = 0;
101     else
102         pad = 1;
103     if (cont) {
104         if (pad)
105             *cont++ = 0;
106         BN_bn2bin(bn, cont);
107     }
108     return pad + BN_num_bytes(bn);
109 }
110
111 static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
112                   int utype, char *free_cont, const ASN1_ITEM *it)
113 {
114     BIGNUM *bn;
115
116     if (*pval == NULL && !bn_new(pval, it))
117         return 0;
118     bn = (BIGNUM *)*pval;
119     if (!BN_bin2bn(cont, len, bn)) {
120         bn_free(pval, it);
121         return 0;
122     }
123     return 1;
124 }
125
126 static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
127                          int utype, char *free_cont, const ASN1_ITEM *it)
128 {
129     if (!*pval)
130         bn_secure_new(pval, it);
131     return bn_c2i(pval, cont, len, utype, free_cont, it);
132 }