blob: da57e77a7aa829f0671c4c03fbb02a39e74a9aee [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Rich Salz2039c422016-05-17 14:51:34 -04002 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +00003 *
Rich Salz2039c422016-05-17 14:51:34 -04004 * 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
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +00008 */
9
10#include <stdio.h>
Richard Levitteb39fc562015-05-14 16:56:48 +020011#include "internal/cryptlib.h"
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000012#include <openssl/asn1t.h>
Geoff Thorpe0f814682004-05-17 19:14:22 +000013#include <openssl/bn.h>
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000014
Matt Caswell0f113f32015-01-22 03:40:55 +000015/*
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.
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000020 */
21
Matt Caswell0f113f32015-01-22 03:40:55 +000022#define BN_SENSITIVE 1
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000023
24static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
Rich Salz74924dc2015-04-24 16:39:40 -040025static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000026static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
27
Matt Caswell0f113f32015-01-22 03:40:55 +000028static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
29 const ASN1_ITEM *it);
30static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
31 int utype, char *free_cont, const ASN1_ITEM *it);
Rich Salz74924dc2015-04-24 16:39:40 -040032static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
33 int utype, char *free_cont, const ASN1_ITEM *it);
Dr. Stephen Henson3cea73a2016-07-18 17:52:56 +010034static int bn_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
35 int indent, const ASN1_PCTX *pctx);
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000036
37static ASN1_PRIMITIVE_FUNCS bignum_pf = {
Matt Caswell0f113f32015-01-22 03:40:55 +000038 NULL, 0,
39 bn_new,
40 bn_free,
41 0,
42 bn_c2i,
Dr. Stephen Henson3cea73a2016-07-18 17:52:56 +010043 bn_i2c,
44 bn_print
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000045};
46
Rich Salz74924dc2015-04-24 16:39:40 -040047static ASN1_PRIMITIVE_FUNCS cbignum_pf = {
48 NULL, 0,
49 bn_secure_new,
50 bn_free,
51 0,
52 bn_secure_c2i,
Dr. Stephen Henson3cea73a2016-07-18 17:52:56 +010053 bn_i2c,
54 bn_print
Rich Salz74924dc2015-04-24 16:39:40 -040055};
56
Dr. Stephen Hensonbb5ea362001-02-23 03:16:09 +000057ASN1_ITEM_start(BIGNUM)
Matt Caswell0f113f32015-01-22 03:40:55 +000058 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM"
Dr. Stephen Hensond3391872001-02-23 12:47:06 +000059ASN1_ITEM_end(BIGNUM)
Dr. Stephen Hensonbb5ea362001-02-23 03:16:09 +000060
61ASN1_ITEM_start(CBIGNUM)
Rich Salz74924dc2015-04-24 16:39:40 -040062 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &cbignum_pf, BN_SENSITIVE, "CBIGNUM"
Dr. Stephen Hensond3391872001-02-23 12:47:06 +000063ASN1_ITEM_end(CBIGNUM)
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000064
65static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
66{
Dr. Stephen Henson437b14b2015-03-02 13:26:29 +000067 *pval = (ASN1_VALUE *)BN_new();
Matt Caswell90945fa2015-10-30 11:12:26 +000068 if (*pval != NULL)
Dr. Stephen Henson437b14b2015-03-02 13:26:29 +000069 return 1;
70 else
71 return 0;
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000072}
73
Rich Salz74924dc2015-04-24 16:39:40 -040074static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
75{
76 *pval = (ASN1_VALUE *)BN_secure_new();
Matt Caswell90945fa2015-10-30 11:12:26 +000077 if (*pval != NULL)
Rich Salz74924dc2015-04-24 16:39:40 -040078 return 1;
79 else
80 return 0;
81}
82
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000083static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
84{
Dr. Stephen Henson437b14b2015-03-02 13:26:29 +000085 if (!*pval)
86 return;
87 if (it->size & BN_SENSITIVE)
88 BN_clear_free((BIGNUM *)*pval);
89 else
90 BN_free((BIGNUM *)*pval);
91 *pval = NULL;
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000092}
93
Dr. Stephen Henson437b14b2015-03-02 13:26:29 +000094static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
95 const ASN1_ITEM *it)
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000096{
Dr. Stephen Henson437b14b2015-03-02 13:26:29 +000097 BIGNUM *bn;
98 int pad;
99 if (!*pval)
100 return -1;
101 bn = (BIGNUM *)*pval;
102 /* If MSB set in an octet we need a padding byte */
103 if (BN_num_bits(bn) & 0x7)
104 pad = 0;
105 else
106 pad = 1;
107 if (cont) {
108 if (pad)
109 *cont++ = 0;
110 BN_bn2bin(bn, cont);
111 }
112 return pad + BN_num_bytes(bn);
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000113}
114
Richard Levitte875a6442004-03-15 23:15:26 +0000115static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
Matt Caswell0f113f32015-01-22 03:40:55 +0000116 int utype, char *free_cont, const ASN1_ITEM *it)
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000117{
Dr. Stephen Henson437b14b2015-03-02 13:26:29 +0000118 BIGNUM *bn;
Rich Salz74924dc2015-04-24 16:39:40 -0400119
mrprea7e974c2015-08-28 16:12:51 +0800120 if (*pval == NULL && !bn_new(pval, it))
121 return 0;
Dr. Stephen Henson437b14b2015-03-02 13:26:29 +0000122 bn = (BIGNUM *)*pval;
123 if (!BN_bin2bn(cont, len, bn)) {
124 bn_free(pval, it);
125 return 0;
126 }
127 return 1;
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000128}
Rich Salz74924dc2015-04-24 16:39:40 -0400129
130static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
131 int utype, char *free_cont, const ASN1_ITEM *it)
132{
133 if (!*pval)
134 bn_secure_new(pval, it);
135 return bn_c2i(pval, cont, len, utype, free_cont, it);
136}
Dr. Stephen Henson3cea73a2016-07-18 17:52:56 +0100137
138static int bn_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
139 int indent, const ASN1_PCTX *pctx)
140{
141 if (!BN_print(out, *(BIGNUM **)pval))
142 return 0;
143 if (BIO_puts(out, "\n") <= 0)
144 return 0;
145 return 1;
146}